Store smarty variable in PHP variable - php

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}

Related

Is possible pass a variable from a tpl file to php file?

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.

How to get smarty value in tpl file - cs cart?

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}

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.

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

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}}

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 ?

Categories