7

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.

skaffman
  • 398,947
  • 96
  • 818
  • 769
Siddharth
  • 5,009
  • 11
  • 49
  • 71
  • That is interesting... I am looking into it. I am using it right now and haven't tested that aspect of it yet. – Tim Withers Feb 21 '12 at 03:15
  • Yup if you use Template you can't use PHP instead you need to use f3 template tags. You need to use View if you need to use PHP as templating engine I think. But I'm not seeing any downside there. – amilaishere Dec 12 '16 at 06:24

3 Answers3

8

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.

Tim Withers
  • 12,072
  • 5
  • 43
  • 67
  • Thanks for the answer. I think I can live with that though it is a serious question on the framework itself. No other framework (Pylons for instance) do that. You say that one can call PHP functions though. Where are those functions located and how can I call them? – Siddharth Feb 21 '12 at 14:21
  • 5
    You can call any function `{{date('m/d/Y',@timestamp)}}` or I use a created function defined in the index.php page for exploding data, `{{exploded(@delim,@phrase,0)}}`. I think the main thing is that the `{{}}` are essentially short tags `= ?>` so you can do anything in the brackets that you could do with short tags. – Tim Withers Feb 21 '12 at 17:06
  • 1
    I don't think it sucks but rather is yet another example of what appears to be "intentional design" to limit the "fat" (i.e. PHP) in templates / views (pun intended). With Conditional Segments via and Repeatable Segments via there should not be much need for PHP code in a template and if there is a need for complex PHP then evaluate it in the controller where it belongs and access it as a variable in your template. Much better code separation as UI designers don't need to concern themselves with programming and programmers can focus on back-end. – nikolaosinlight Sep 12 '16 at 02:47
0

Maybe try to use different template engine which will allow you define easier the blocks variable dependency?

For example in PHPTal http://phptal.org/manual/en/split/tal-condition.html you can do it like that:

<div tal:condition="php: var == 'var1'">
....
</div>
  • 1
    Why? Fat free built-in template engine already has the Conditional Segment logic as even the OP mentions themselves which addresses their issue perfectly. I think the issue is the OP prefers to put PHP in their templates which isn't necessarily a good thing if you consider design vs. programming and separation of concerns w.r.t. MVC. – nikolaosinlight Sep 12 '16 at 02:52
0

It is undocumented but you can put code within {~ ~} in a template and it will be converted to <?php ?> when the template is compiled (using v3.6).

e.g. {~ @color = 'red' ~} will become <?php $color = 'red' ?>

Richard
  • 39,052
  • 6
  • 25
  • 29