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

What template I see in the future:

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

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

The template engine that is specified in the post link has long been used on many of my projects, so it’s not necessary to say that I create a bicycle. I have less straight arms, a head, and I want to develop, get a new experience.


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

  • one
    As for the bike, I also dashed off my Templater and never thought about the fact that it was a bike! So if the brain allows you to do this, why not do it ?! And let them say to themselves :) - Palmervan
  • A real scientist will not ask "why cross a crocodile with a suitcase?", He will ask "how?" © - Sh4dow
  • @ Sh4dow, did not understand. - Oleg

3 answers 3

Not. What are you ... Of course it won't be a bicycle. Normal template engines in general it is difficult to find.

Well, for example, you can add a foreach loop, so that you can, for example, output an array of data. So to speak:

 Массив состоит из следующих значений: <FOREACH($VAR_1)> {$VAR_1} <END FOREACH> 

Well, you understand the meaning. You can even bind the array key somehow (i.e., foreach($array as $key => $value) ). For example:

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

You can add performance functions. Well, although you have aliases like this. Generally look here . Pretty good template maker. I like this. The only thing that I would add there from myself is the prohibition of adding a regular php-code (well, I just really need this feature!).

  • This is an idea, I will try to make cycles. Thank. - Oleg
  • Opa, but what about associative arrays to do ... - Oleg
  • Well, make some variable in this block and let it denote the key of the array. This is my suggestion. Those. I wrote above ... Something like {$ VAR_1_KEY $}. And when you compile a template, just change it to $ key. - Dobby007
  • Excuse me. I did not understand the question) - Dobby007
  • > No. What are you ... Of course it won't be a bicycle. Normal template engines in general it is difficult to find. There is not enough <sarcasm> , anyone can decide that you are serious :) - Ilya Pirogov

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

without Evala it will be difficult to parse this block (but in general, it’s real). And eval is always a risk, especially if you do not manage the content. So I’d mean that you can come up with a simpler syntax in which it could be implemented.

Oops, but what about associative arrays to do ...

 {$array.key1.subKey1.subSubKey1} 

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

+ I would advise to add more converters.

 {$var156|toTimeStr} 

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

After experimenting with this you probably want to add parameters to this converter, for example, to set the time format, so you need to also consider the parameters:

 {$var156|toTimeStr 'Ymd H:i:s'} 

or even named parameters:

 {$var156|toTimeStr format='Ymd H:i:s'} 

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

    As for me - not the best format, in fact, I don’t see any reason to use something like this, pure PHP templating only in profile. If you want to make a really good template engine, then it should be fast and beautiful. In your "concept" I do not see either comfort or beauty (I speak strictly for myself).

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

    Well and, perhaps, make something to be a humanly visible variable, something like [ someVar ] , { someVar } etc. because I do not know how the rest, but as for me - the readability of such a $var$ code suffers. In general, ideally, it would be nice to write the code so that users could choose their own separators, i.e. let at least {{{{ var }}}} or OLOLOLOvarOLOLOLO do if they want. Well, it is 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.

    Well, implement caching.

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

    PS: how will you 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 are not writing for educational purposes only.

    • > In general, ideally, it would be nice to write the code so that users could choose their own separators. > Well, implement caching. More? How to make caching in the template engine? What to cache, how, and how much? - Oleg
    • Caching is needed in order that, when the template is ready - do not waste time on its analysis. What to cache and how - depends on the implementation, it is important that the ascent from the cache was as fast as possible. Regarding how much - well, the user will determine by calling the function, which parameters to set by default - judge for yourself - Zowie
    • And it would be wonderful to implement the ability to store caches both in files and in the database, moreover, to give the user the opportunity to realize the storage and reading of the cache. - Zowie