How can I assign a smarty variable to a PHP variable. I have
{assign var=arrayname value="/"|explode:$form.attributes}
{assign var=id value=$arrayname.2}
I want to assign the smarty variable id to a php variable
<?php
// I want $id here
?>
How can I do this?
Thanks in advance
I don't quite understand...how was your $arrayname passed to Smarty....? I assume you have used something like
<?php
$smarty->assign('arrayname',$arrayvariable)
?>
So in PHP there is equation:
$you_have_wanted==$arrayvariable[2]
This isn't really a situation you should find yourself in.
You should be able to either do what you want to do entirely in smarty or entirely in php.
If you explain in detail what your trying to do we can help you with one or the other
It is possible to use php in a smarty template but:
"This is for advanced users only, not normally needed and not recommended."
The whole point of the smarty template is to keep your business logic in a separate view.
Therefore you should deal with your variables in the php page then assign them to the template as in Lucifer Orichalcum's example.
If the user Id value you a trying to retrieve comes from a form you have to deal with the form values in the .php file ($_POST['id']) not in the .tpl
Same rule for your sessions variables.
Can you tell us more about where the Id comes from in the first place ?
Related
I'm currently using an own template system where I need to assign variables to their placeholders which will be replaced later. Unfortunately, It's not possible yet to use conditions like if/else directly inside the template. I always have to handle this in PHP and need to assign the final values to the template.
How it currently is in php:
$template->assign('{content}', ($b ? 'value' : 'value2')), and in html: <div>{content}</div>
What I want:
<div><?php echo ($b ? 'value' : 'value2'); ?></div> or <div><?php echo ($b ? 'value' : $this->renderAnotherTemplate()); ?></div>
I could use .php-files or something like that but as far as I remember, the template file name is visible in the url. So I need a way, to read a string from a template file, execute it's php and accessing the current variables I use in the parser (eval is dangerous).
Does anyone of you have a good idea, how to do this?
Thank you!
Why don’t you just include a PHP file?
include(‘template.php’)
You’ll be able to access all the variables inside your template file that are available in the scope where you call include(). You could wrap it within a method on your template object which may also accept variables to be used in your template file.
The other solution would be to pull in a templating language such as blade, twig or smarty. But honestly I’d just go with the simpler include on a small project
Is possible pass from php to tpl with this:
PHP:
$smarty->assign('variable',$foo);
TPL:
{$variable}
But i don't know if is possible pass a variable from tpl to php...
I have never tried it, but take a look to get_template_vars() and {php} tag. Maybe it is what you are looking for.
I am trying to display some html code from a php variable created in a controller like
$this->template()->assign(array('html' => "<p>Ajith chandran</p>"));
But when I write {$html} in template file,this html code is rendering as normal text and displaying it with full code in browser.And I tried this in two other ways like
{php}echo $html;{/php} and <?php echo $html; ?>
but both are not displaying anything.
You should display this variable in Smarty using simply:
{html}
If for any reason I need to use {php} tag what I really don't recommend (it's a bad practice and in Smarty 3.1 is deprecated) you can display html value using:
{php} echo $template->getTemplateVars('html'); {/php}
Of course those both syntaxes should be used in TPL file and not in PHP file. If you want to use in PHP file any Smarty variable you could probably use in your case:
echo $this->template()->getTemplateVars('html');
What you should also consider is security of this solution. You should think about escaping your output or at least strip some tags. Now you can set<script>alert('I am a very bad script');</script> to html variable and user will see JavaScript alert. I assume you might not know what your html variable will store and Smarty by default doesn't escape variables what can make to serious problems with your site.
So you could either use for example:
{$html|escape:"html"}
{php} echo htmlspecialchars($template->getTemplateVars('html')); {/php}
to display those data safely or use global setting escape_html to do it for all Smarty variables:
In PHP
$this->template->escape_html = true;
In Smarty:
{$html}
{php} echo htmlspecialchars($template->getTemplateVars('html')); {/php}
As you see when using {php} tag even if setting escape_html to true you need to escape data in PHP otherwise you will display data as they were set.
Either set $escape_html to false in Smarty or use {$html nofilter}
See: http://www.smarty.net/docs/en/variable.escape.html.tpl
I want to use a loop in smarty to read an object value which i will be passing from controller later on.
{foreach from=$foo->bars item=bar}
{$bar->product->name}
{$bar->code}
{/foreach}
To initialize this, i wrote the following code in smarty file:
{php}
$bar1->product->name = "prod1";
$bar1->code = "BC5E";
$bar2->product->name = "prod2";
$bar2->occasionName = "XW9D";
$this->assign($foo->bars, array($bar1,$bar2);
{/php}
But this gives me an error, is this the right way to create an object in Smarty ?
$this->assign($foo->bars, array($bar1,$bar2));
You forgot to close last ).
Can you please do all the execution with the object on the php page and then you can assign the final array on to the smarty and use easily on the .tpl page this is the best way.
Try not to process data on the .tpl page process all the data on php page and assign it to .tpl and then just show it.
Smarty is a template engine and is designed for view(presentation) mainly.
Write the foreach loop on .php create array output and assign to tpl.
Please tell me how to store smarty variable in PHP variable
Smarty variable = {$v.user_id}
I want
<?php
$var = {$v.user_id}
?>
Please tell me possible option
in smarty template file, use this to assign a value to variables.
{assign var=var value=$v.user_id}
ex:-
In PHP file you want to assign values to variable use this:
$smarty->assign('Name','Ajithkumar');
Smarty template file:
{$Name}
now you can get a name Ajithkumar.
where exactly do you want it to work? in php
$var = $this->_tpl_vars['v']['user_id'];
or in smarty
{assign var="var" value=$v.user_id}