I'm trying to get smarty value in my profile_account.tpl file.
{capture name="profile_pic"}{$user_data.main_pair.icon.image_path}{/capture}
How can I get this in top_quick_links.tpl?
"$smarty.capture.profile_pic"
But that doesn't return any value. How can I do it?
Maybe you did not assign the $user_data array for the template engine.
Your solution is not so elegant. I suggest you to put this into your controller file:
This is the way to assign custom variable for Smarty in CS-Cart
Registry::get("view")->assign("user_profile_picture", $user_data['main_pair']['icon']['image_path']);
And use this variable directly in the .tpl file:
<img src="{$user_profile_picture}" />
Please use the following code for 4.x versions:
{$smarty.capture.profile_pic nofilter}
Related
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 want to change my website main page, using php code!, I found the tpl file which I need to change, problem is that I have no experience in tpl and I want to write it with php but it won't work. I am trying to use:
{include_php file="/path/to/somefile.php"}
To use php language but it does not work. How does it work with tpl in Smarty templates using php language.
My suggestion the following is a php file
include_once("/var/www/html/smarty/libs/Smarty.class.php");
$smartyobject=new Smarty();
$varr="stackoverflow"; //php variable it can also be a array";
$smartyobject->assign("website", $varr); //assigning a variable to "website", and this is now a smarty variable.
$smartyobject->display("tplfile"); //this is the actuall file that get displayed, in this tpl file you can use "website" variable {$website}
smarty has so many in functions loops.. so there is no need include any php file. do whatever you want to in the php file save the result in a variable and display it in a tpl file.
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.
I need to use PHP file_get_contents() in smarty tpl file. I can't use it in PHP and assign it to smarty template. Because the URL is generated dynamically through loop inside smarty template file. So I'm using smarty plugin function to achieve that task. But I want to know whether is there any way I can use it in template file directly instead of parsing it from plugin file.
I've attached the plugin code which I'm using to achieve this function. Please anyone let me know how to use it in smarty tpl file directly.
function smarty_function_getTitle($params)
{
if ($params['id']) {
$content = file_get_contents("http://youtube.com/get_video_info?video_id=".$params['id']);
parse_str($content, $ytarr);
return $ytarr['title'];
}
}
I've used below code to call it in smarty template:
{getTitle id=$videoId}
Suggestions are welcome!
For those of you reading that didn't read the comments above, myself and OP are both aware that this is not how you use a template engine. He seems to have his reasons for wanting to do this directly in the template rather than a plugin or ahead of time in his code. So don't slag me for demonstrating how, please :)
Here is how you can do it in Smarty.
{"http://youtube.com/get_video_info?video_id=`$videoId`"|file_get_contents|parse_str:$result}
{$result.title}
I did the first part all in one call but if you want to be careful you can split it into multiple calls with checks. But I tested this locally and it worked fine.
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 ?