I decided to rewrite my template engine, introduce global codes, elseif.

What templating engine do I see in the future:

$SOME_CODE$ — обычный код.
<if{$SOME_CODE$}>some code is true</if> — простое условие
  <if{$ALIAS_1$ == 123 && $QUERY$ == 'search'}>
<elseif{$ALIAS_0$ == '/'}>
</if> — сложное условие

The question is, what do you think should be added to the functionality?

The template engine that is mentioned in the link of the post has been used on many of my projects for a long time, so it is not necessary to say that I am creating a bicycle. I have more or less straight arms, a head, and I want to develop, get new experience.

UPD:What is the best way to parse conditions and loops? Regular expressions or string functions?

Answer 1, authority 100%

No. What are you… Of course it won’t be a bike. Normal templating engines are hard to find right now.

Well, for example, you can add a foreach loop, so that you can, for example, display an array of data. Let’s put it this way:

Массив состоит из следующих значений:

Well, you get the point. You can even bind the array key somehow there (i.e. foreach($array as $key => $value)). Like this:

Массив состоит из следующих значений:
    {$VAR_1_KEY$}: {$VAR_1$} 

You can add execution of functions. Well, although you have aliases for it like. In general, look at here. Pretty good template. I like it. The only thing I would add on my own is the prohibition of adding the usual php code (well, I just really need this feature!).

Answer 2, authority 100%

<if{$ALIAS_1$ == 123 && $QUERY$ == ‘search’}>

It will be difficult to parse this block without an eval (but in general it is possible). And eating is always a risk, especially if you’re not managing the content. Actually, I mean that you would come up with a simpler syntax in which this could be implemented.

Oops, but how to make associative arrays…


After you make a template engine, try using it in some simple but real task. And you will immediately understand whether everything is working correctly and what is really missing.

+I would also recommend adding converters.


where toTimeStr is a kelback function that converts the var156 variable.

After experimenting with this, you may want to add parameters to this converter, for example to set the time format, so you need to take into account the parameters too:

{$var156|toTimeStr 'Y-m-d H:i:s'}

or even named parameters:

{$var156|toTimeStr format='Y-m-d H:i:s'}

In general, there is a big flight of fancy here. But first of all, think about what you really need.

Answer 3, authority 100%

As for me – not the best format, in fact, I don’t see any reason to use something like that, pure PHP templating only in profile. If you want to make a really good templating engine, then it must be fast and beautiful. In your “concept” I see neither convenience nor beauty (I speak strictly for myself).

And for parsing, most likely you will have to use a little bit of everything.

And, perhaps, make the variable humanly visible, something like [ someVar ], { someVar }etc. because I don’t know about the others, but as for me, the readability of such code $var$specifically suffers. In general, ideally, it would be nice to write the code in such a way that users could choose their own delimiters, i.e. even if they do {{{{ var }}}}or OLOLOLOvarOLOLOLOif they want. Well, it’s quite important to implement convenient basic helpers and the ability to write your own. Helpers can be, for example, for recursive output, filtering, formatting, etc. etc.

And implement caching.

If you write the code correctly, you can easily port it to other programming languages.

PS: how to form a concept – look at existing template engines and think about whether your idea is significantly different, whether you can offer a better product, etc.
Of course, this is only if you do not write exclusively for educational purposes.

Leave a reply

Please enter your comment!
Please enter your name here