smarty - how to output the variable "{$myvar#display}" - php

I can't seem to find a way to escape the "#" sign in a smarty variable output block.
Here's my setup:
$data = array("myvar#display"=>4534534);
$smarty->assign($data);
Assign doco here
In my template file:
<body>
{$myvar#display}
</body>
Output:
Message: Undefined property: Smarty_Variable::$display
Any ideas how I can display that without changing the "#" sign (I am unable to modify this as it is a convention used throughout the application and I do not have control over this)
Running LAMP 5.2.17, Smarty 3
PS I've tried this without success...
{assign var=jason value="$myvar#display"}{eval var=$jason}
output
4534534#display

the only way that I see with smarty 3 (with tag {php} disabled) is assign a new variable.
If you have an access somewhere in php code, you can put in:
$smarty->assign('at','#');
and get the var name in tpl:
{$myvar{$at}display}
I think that's more simple than remake the logic behind your complex variable name.
Alternatively, You can try if you have the tag {php} enabled and get index of private smarty array.
EDIT
this works for me
{assign var=foo value='myvar#display'}
{${$foo}}

Related

Phpfox display html from php variable

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

Including PHP file inside smarty template file

I have completely no idea about smarty template system. What I am trying to do is include a php file to get some variable inside a .tpl file (this is a WHMCS template).
I have tried like:
{php} include ('file.php'); {/php} //doesn't work
{include_php file='file.php'} //doesn't work
I have also followed the answer of this question. Still couldn't get it working.
How can I include code.php inside header.tpl of WHMCS? Any help please?
Just for reference: both tpl and php file is in the same directory, if that helps anyway.
It's really not recommended to use php code in Smarty. In fact it's now deprecated and you should avoid such solution as much as possible because it often doesn't have sense.
However if you really want to use PHP in your Smarty files for some reason you need to use SmartyBC (Smarty Backward Compatibility) class instead of Smarty class.
So for example instead of:
require_once(_PS_SMARTY_DIR_.'Smarty.class.php');
$smarty = new Smarty();
you should use:
require_once('SmartyBC.class.php');
$smarty = new SmartyBC();
Then you will be able to use PHP in your Smarty template files
EDIT
If you have just problem with including it's probably your directories problem (however you haven't showed any errors).
I assume you have your files inside your templates directory and you set it properly using:
$smarty->setTemplateDir('templates');
if you simple display index.tpl file in Smarty and you have this PHP file in the same directory (in template directory) you can include it without a path.
However if you include in this index.tpl file another tpl file, when you want to include php file you need to pass the full path to this PHP file, for example:
{include_php 'templates/file.php''}
Your using Smarty the wrong way. The whole point of Smarty is to NOT include any PHP in your presentation bits (views, a.k.a. the good ol' HTML).
So, whatever you're trying to do in that PHP file, just let it do its magic, but send the actual result to Smarty. For example, do you want to show a table of users you get out of a database? Execute the query, fetch the result and pass the results (like an array of results) to smarty like this:
<?php
$smarty = new Smarty();
$users = $db->query('SELECT * FROM users');
// Assign query results to template file.
$smarty->assign('users', $users);
// Compile and display the template.
$smarty->display('header.tpl');
Now, in your smarty template you can access that array like this:
<html>
{foreach from=$users item=user}
Username: {$user->username}<br />
{/foreach}
</html>
I hope you see where I am going. Keep your application logic in the PHP file, and let the template just take care of the looks. Keep the template as dumb as possible!
You get data into Smarty by assigning it. Embedding PHP is not recommended, and deprecated from Smarty 3.
php:
$smarty->assign('foo','bar');
smarty:
{$foo}

Assigning array of objects in smarty

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.

How can I assign a smarty variable to a PHP variable

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 ?

Get Template Name - Smarty

I'm dealing with website is a mess and need to find out the template file that smarty is rendering.
Is there a method on the smarty template object that I can call to get the current template file?
for example
echo $tplObj->getTemplate(); // echos "shop/templates/cart.tpl"
From the doc:
{$smarty.template}
Returns the name of the current template being processed. The following example shows the container.tpl and the included banner.tpl with {$smarty.template} within both.
<b>Main container is {$smarty.template}</b>
{include file='banner.tpl'}
will output
<b>Main page is container.tpl</b>
banner.tpl
Maybe the {debug} tag and its associated Debugging console could help, here ?

Categories