Welcome Guest, Not a member yet? Register   Sign In
Using <!--- ---> instead of <?php ?> in view files
#11

[eluser]Dready[/eluser]
About the built-in parser and conditonnal statements : in fact you can do it, but in your controller. Here is the trick : let's imagine you have this template :
Code:
Hello {username}
{if_logged}You are logged in{/if_logged}

And in your controller :

Code:
$tpl = array('username'=>$this->user->name, 'if_logged' => array());
if ( $this->user->name != 'Anonymous' ) {
  $tpl['if_logged'][] = array();
}
$this->parser->parse('myview',$tpl);

The condition check has to be done in the controller through...
#12

[eluser]xwero[/eluser]
I like that trick Smile And i think it's good practice too, moving the control structures out of the template.
#13

[eluser]EEssam[/eluser]
Hi,

That is very cool, Dready but there's no way to have an elseif and else branches.
#14

[eluser]Dready[/eluser]
Why isn't that possible ?

Code:
Hello {username}
{if_blue}You you have blue eyes{/if_blue}
{if_red}You you have red eyes{/if_red}
{else}You have sunglasses{/else}

And in your controller :
Code:
$tpl = array('username'=>$this->user->name, 'if_blue' => array(),'if_red'=>array(),'else'=>array());
if ( $this->user->eyes->color == 'blue' ) {
  $tpl['if_blue'][] = array();
} elseif ( $this->user->too_much_smoke ) {
  $tpl['if_red'][] = array();
} else  {
  $tpl['else'][] = array();
}
$this->parser->parse('myview',$tpl);

It's really a matter of taste, I like my template files without any PHP code, so web designers can work on design, and php developer (including me) can work on code.
#15

[eluser]EEssam[/eluser]
But in this case I can only have 1 {else} branch per document!
#16

[eluser]Dready[/eluser]
Are you kidding ? Call it 'second_else', 'elseelse' or 'whatever', it's only the name of the key of an array...
#17

[eluser]EEssam[/eluser]
No, I'm not stupid Smile

But it's not too elegant. This is how it should look in a perfect world:

{if_blue}
You you have blue eyes
{else}
You have sunglasses
{/if_blue}
#18

[eluser]Rick Jolly[/eluser]
You guys shouldn't even be debating the template parser. It's great for the one-off email template, but don't create an application around it. It parses the view on every request. Smarty will compile the view into native php on the first load. Smarty also has caching support.




Theme © iAndrew 2016 - Forum software by © MyBB