|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
arian Smarty n00b
Joined: 20 Oct 2007 Posts: 4 Location: Netherlands
|
Posted: Sun Oct 21, 2007 11:06 am Post subject: Does template variable exists |
|
|
Hi,
I was just thinking, but wouldn't it handy to make a function wich checks if a variable exists in the template.
For example.
I load data from the database, and i put it in a smarty variable {$data}.
Then i use the foreach to print the data on the screen.
But when i don't use the $data variable in my template. It isn't nessacery to load the data from the database.
So my proposal is to make a function wich sees if the variable is used in the template.
Mabye like this:
<?php
if($smarty->var_exists('data')){
$data = array('a','b','c');
$smarty->assign('data',$data);
}
?>
so when my template is like this
... some html..
{foreach from=$data key=i item=id}
{$id}
{/foreach}
... other html...
$data will be assigned
But when my template doesn't contains the foreach with $data, $data will not be assigned.
This will reduce unnessacery database calls, other php stuff, so it should be faster at the end.
Arian |
|
Back to top |
|
czarnowski Guest
|
Posted: Mon Oct 22, 2007 3:06 pm Post subject: |
|
|
Use
$smarty->get_template_vars('youvarname'); |
|
Back to top |
|
arian Smarty n00b
Joined: 20 Oct 2007 Posts: 4 Location: Netherlands
|
Posted: Mon Oct 22, 2007 3:21 pm Post subject: |
|
|
czarnowski wrote: | Use
$smarty->get_template_vars('youvarname'); |
Yes, i know this function, but this function returns al the ASSIGNED vars. not the vars wich the designers uses in the template file. Right ? |
|
Back to top |
|
czarnowski Guest
|
Posted: Mon Oct 22, 2007 5:06 pm Post subject: |
|
|
The concept of smarty is to assign a variable to it and use it with a template not to control wheter you are using as example a foreach in a template and to make a database call in condition of it.
if you are using
Quote: | {foreach from=$data key=i item=id}
{$id}
{/foreach} |
and there is no $data nothing happens because this is the php code of the compiled template:
Code: | <?php $_from = $this->_tpl_vars['data']; if (!is_array($_from) && !is_object($_from)) { settype($_from, 'array'); }if (count($_from)):
foreach ($_from as $this->_tpl_vars['i'] => $this->_tpl_vars['id']):
?>
<?php echo $this->_tpl_vars['id']; ?>
<?php endforeach; endif; unset($_from); ?> |
But you can try something like this in a template:
Code: | {php}
if ($this->_tpl_vars['data'])
echo 'tralala';
else
$this->_tpl_vars['data']='Hallo World';
{/php}
{$data} |
|
|
Back to top |
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|