A modification to print string in template

dsecret
Member | 3
+
0
-

Hi guys,

During the last few months my team was evaluating different templating engines to replace our in-house solution, and we ended up with Latte to be the most “natural” choice. It's performance is great, as well as it's syntax. Also, very important for us, is that it is almost out-of-the-box compatible with templates we developed for previous projects, and we implemented all custom modifiers easily.

However, there is one thing that would be great for us if possible to implement:
Since in PHP “$var = ‘string’”, in our templating system this “{$var}” was the same as this “{‘string’}”. In Latte, we need to use this syntax “{=‘string’}”.

What would be the easiest way for us to modify Latte to be able to print strings without the need for using the equal sign?

Thanks,
Donald

nightfish
Member | 519
+
+1
-

@dsecret You could probably implement a compiler pass that would modify the AST to replace node representing {'hello'} with node representing {='hello'}.

David Grudl
Nette Core | 8239
+
+1
-

This is a fixed part of the lexer and exists to make it easier to insert javascript into templates.

https://github.com/…ateLexer.php#L279

Wouldn't it be easiest to simply replace {' with {=' before parsing?

dsecret
Member | 3
+
0
-

nightfish, David Grudl,

Thank you for the quick and informative response. We'll check what is the optimal approach to enable printing strings without having to use the equal sign (for some of us, with PHP background, use of that sign is a bit natural, but programmers that are not fluent in PHP for some reason find it annoying).

Btw, one thing we found very usefull with Latte is being able to implement custom modifiers without having to modify Latte' source code – great when using it with Composer.

dsecret
Member | 3
+
0
-

David Grudl wrote:

This is a fixed part of the lexer and exists to make it easier to insert javascript into templates.

https://github.com/…ateLexer.php#L279

Wouldn't it be easiest to simply replace {' with {=' before parsing?

@DavidGrudl

I did this by using ‘Latte\Loaders\StringLoader’ and ‘str_replace+file_get_contents’. It works as intented, but no cache file is created. I believe this will affect performance greatly? If yes, is there another way to do so?

@nightfish

I couldn't find any example of implementing the desired compiler pass – can you share some reference/example?

Thanks!
Donald