Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP code in FatFree template

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

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

The problem which I'm facing is that, inside the template.php file the F3 tags are recognised but any PHP code doesn't work. For instance, 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 is printed irrespective of the value of var. Also, php for loops are not working - so basically all the php code is not working.

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

like image 622
Siddharth Avatar asked Feb 16 '12 10:02

Siddharth


1 Answers

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

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

With F3::render() you can evaluate PHP expressions and use the F3::get() to retrieve 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"

The Template::serve() is for templating only. Meaning its simply to process the templating language.

So basically, and yes it sucks and doesn't make sense, you can evaluate PHP code in the F3::render() and you can't use templating variables ({{@var}}) -OR- you can use Template::serve() and you are limited to only calling PHP functions, and not truly evaluating PHP code.

like image 72
Tim Withers Avatar answered Sep 22 '22 14:09

Tim Withers