Suppose we have html pages (body output):

<div>Добро пожаловать $name.</div> 

For now, let us omit the rules for text output, using htmlspecialchars and other rules ... For the question is in mechanics.

Filling html page with such content is not good, and not very convenient. In fact - you need to get rid of the php code in html, as I understood.

If I also correctly understood - the parser deals with such cases. You can search through DOM or through regular expressions ...

I'm looking for a slightly different approach, maybe it is. For example:

 <!-- START: text --> <div>Добро пожаловать {name}.</div> <!-- END: text --> 

In turn, {name} is the same user name that needs to be displayed using output methods in php.

Or, if suddenly we have a table, we will have a similar look:

 <!-- START: text --> <div> <span>Вывод {text.name}</span> <!-- START: table--> <table> <tr> <th>Имя</th> <th>Фамилия</th> </tr> <!-- START: row--> <tr> <td>{table.firstname}</td> <td>{table.lastname}</td> </tr> <!-- END: row--> </table> <!-- END: table--> </div> <!-- END: text --> 

In this construction, we already store the output in an array and have a hierarchical output structure - we output the rows of the table in a loop. If we need to display 2 identical tables on the pages - there will be no conflict in such a case.

In connection with the above, the question is: are there any similar parsers that produce html data output in a similar form? You can write, the algorithm is there, but it makes sense to invent a bicycle? :)

    1 answer 1

    What you are writing about is called a template engine. It happens in js and in php and in other languages.

    There is an approach in application development called MVC . It is customary to share applications. The model (data), the controller (logic) and the presentation, and here are the presentations of different types of template engines. They are designed to remove the influence of php code in the site templates.

    Here are template engines for php.

    Here on js

    Article on Habré about template engines in php.