PHP code in FatFree template

I am trying to work with FatFree framework and trying to use the template engine. I am drawing a template with the following code -

echo Template::serve('template.php'); 

The problem I am facing is that inside the template.php file, F3 tags are recognized, but any PHP code does not work. For example, if I have the following code in the template.php file -

 <?php if (F3::get('var') == 'var1') { ?> <span>var1 is present</span> <?php } else { ?> <span>var1 not present</span> <?php } ?> 

Here, both var1 is present and var1 not present are printed regardless of the value of var. Also, php for loops doesn't work - so basically all of the PHP code doesn't work.

However, if I used <F3:check> to write the above PHP code, then everything works fine. Can we use PHP code in templates. If so, this is a serious limitation.

+7
source share
3 answers

I found the answer, although I really don't like it.

There are two different functions: F3::render() and Template::serve()

With F3::render() you can evaluate PHP expressions and use F3::get() to extract the variables. According to the website: "The only issue with embedding PHP code in your templates is the conscious effort needed to stick to MVC principles"

Template::serve() is for templates only. This means that it simply processes the template language.

So, in principle, and yes, it sucks and does not make sense, you can evaluate the PHP code in F3::render() , and you cannot use template variables ( {{@var}} ) -OR- you can use Template::serve() and you are only limited to calling PHP functions, not really evaluating the PHP code.

+8
source

Maybe try using a different template engine, which will make it easier to determine the dependence of variable blocks?

For example, in PHPTal http://phptal.org/manual/en/split/tal-condition.html you can do this as follows:

 <div tal:condition="php: var == 'var1'"> .... </div> 
0
source

It is undocumented, but you can put the code inside {~ ~} in the template, and it will be converted to <?php?> When compiling the template (using v3.6).

for example, {~ @color = 'red' ~} becomes <?php $color = 'red'?>

0
source

Source: https://habr.com/ru/post/908603/


All Articles