Forums / Cotonti / Development / Twig: modern template language

GHengeveld
#25939 2010-09-04 20:53
Great, it looks like I started something here, which was my intention entirely :)

I noticed the Trac tickets. I'll take a look at the modulo operator. If I get it to work I'll backport it to the Genoa branch as well.

The piping system that Twig uses should be fairly simple to implement, but we'd have to make a decision as to what functions to implement.

I'd prefer the name CTemplate, but that is also the name of a Template language for C. By the way, are you keeping an eye on the improvements/updates done by the XTemplate team? (Although their latest commit was almost 2 years ago)

Added 1 hour 8 minutes later:

I'm looking into the modulo operater, but it's not exactly a 2 minute deal. The difficulty lies in having XTemplate look for an operator twice, since we want to be able to do this:

<!-- IF {PHP.i} % 3 == 1 -->
(1,4,7,10,13 etc)

Added 2 hours 12 minutes later:

After fiddling around with this all day I've finally got it. It turned out to be easier than I though.

This did it:
case '%': eval("\$res = $val % $val2;"); break;
Along with adding the % sign to the preg_match list.

It's not so nice because it uses eval() which is potentially unsafe. I'll add a check so only integers are allowed.



Added 32 minutes later:

Okay, I've committed the changes to trunk and I've backported them to the Genoa branch as well. See changeset 1315.

Templates will now support the modulo operator (% sign) like the example above. A simple odd/even check can be written like this: (note the spaces)
<!-- IF {PHP.i} % 2 == 0 -->even<!-- ELSE -->odd<!-- ENDIF -->

A while ago, I added two other operators as well, namely CONTAINS and HAS. These are now backported to the Genoa branch as well. CONTAINS will check if one string is contained in the other. HAS will check if an array has a certain value. They can be used like this:
<!-- IF {PHP.usr.name} CONTAINS 'Kora' -->Username contains 'Kora'<!-- ENDIF -->
<!-- IF {PHP.arrayofstrings} HAS 'somestring' -->The array has this value<!-- ENDIF -->

Dit bericht is bewerkt door Koradhil (2010-09-05 01:06, 14 jaren ago)