Search code examples
phpasp.netcustom-tags

Custom HTML Tags in PHP


When I was playing around with Joomla! source code, I saw such as code (or similar to this one, I am not pretty sure since it was a while back then):

<juc:place name="module" some-other-attributes/>

I am not telling it was exactly like the one above but I was surprised actually when I saw Asp.NET-like custom tags:

<asp:Label ID="lblLabel" runat="server"/>

I am now wondering how to accomplish same thing or similar in PHP like they did in Asp.NET. Is there any library for framework for this or so forth?

Do I need to write a parser which parses my php code and searches for such tags and replaces them with what's corresponding like Asp.NET replaces <asp:Label/> with <span/>

My personal preference:

I like what Asp.NET did by separating code-behind from design-view or html from c# code. And using asp.net's server-side controls enable developer to access html control from code-behind easily. It sounds like separation of concerns and I am wondering if there is any project or way which has done same thing already?


Solution

  • If you use an approach, where presentation layer is stored separately from execution logic (for instance, a templating engine), then it is quite easy to create a parser which looks for specific code in HTML and replaces it with shomething else. You could make your own "SMARTY" language.

    However, if your code is just "executable PHP" with no layer separation, you can't do such replacements (as it is directly executed by PHP scripting engine). By "executable php" I mean something like this:

    <html>
      <head>
      ...
      </head>
      <body>
      <?php echo "Hello world"?>
      </body>
     </html>