Question

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?

Was it helpful?

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>

OTHER TIPS

Alternatively, you could use an MVC framework (Zend Framework, Codeigniter or similar) which does a lot of separation of concerns for you.

As suggested, SMARTY is a template engine that does pretty much what you're after, but that's really only one part of the puzzle. Abstracting your data access layer is also a good idea.

You can also enable short tags on your server config, then you can even tidy up your presentation code with something like...

<html>
    <head>
    ...
    </head>
    <body>
        <?= "Hello, world!" ?>
    </body>
</html>

You could try SimpleHTMLDOM. You can then run something like...

$html = new simple_html_dom();
$html->load_file(fileToParse.html');
foreach($html->find('tagToFind') as $element){
    $element->tag='span';
}

That should work.

You may want to have a look at Creating custom html tags for cms

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top