Smarty one tag inside another - smarty

I want to assign some value with included smarty value to another, like this:
{assign var="one" value="hello world {$two}"}
but error is happened. Please help how resolve it.

you should not use limiters in limiters i guess.
{assign var="one" value="hello world $two"}
worked for me.

Resolved
{assign var="one" value="hello world $two"}

The simplest solution to this particular case is to use the |cat modifier, since modifiers can be used wherever you would normally have a string or variable.
{assign var=one value="hello "|cat:$two}

Related

Using defined smarty variable as object key

I want to access the data of the object that comes as a smarty variable according to the variable I have defined.
I'm actually expecting like {$page->page_meta->description} as I wrote below. But nothing happens. It doesn't give an error. What is the problem?
Ok now I understand what you mean.
{foreach $metas as $meta}
{$page->page_meta->{$meta}}
{/foreach}
PS next time do not insert screenshots like this but write code

updating value of an object variable in smarty in the template

I'm trying to modify a template in a simple manner, not rewrite the underlying code. I realize normally object assignment is not done in the template, but is it possible?
I want something like this:
{if $product->available_now == "XXX001"}
{assign var="product->available_now" value={"YYY123"}}
{/if}
I want to read the object variable $product->available_now and, based on its contents, update it to something else for use later down in the template.
Everything I try ends up with either a blank screen or else (as is the case with my code above) an unchanged variable.
FYI: this is for use with prestashop
Try with:
{if $product->available_now == "XXX001"}
{assign var=product->available_now value="YYY123"}
{/if}
I must confess that I have not tested it but it has to be the error you made, as this is the correct way to assign a value to a variable!

How to use dynamic variable in smarty template function

I need to use the variable inside the popup tag from Smarty.
I can't declare the var on the server because it's dynamic (originating from a loop).
I tried all the different approaches with the assign tag like
{assign var=title value="$some_loop_var - sitename!"}
or
{assign var="myfield" value=$some_loop_var + "btn_licencee_select"}
Always the printed variable is empty.
Any ideas what I might be doing wrong?
The solution was quite trivial in the end:
If you use a variable inside a Smarty function you need to "escape" it with backticks:
{popup text="`$some_loop_var`_static_part" }
Hope this helps anybody else as well. Took me hours to figure this out...

JSTL: Dynamic number currency, change the pattern and keep it dynamic?

I'm trying to show a currency dynamicly, that works totally fine!
But I'd like to change the pattern from "SFr. 150'000.00" to "150'000.00 SFr." (and still keep it dynamicly!)
Any suggestions how I could do that?
Here is the code:
<fmt:setLocale value="${pageContext.request.locale}" scope="session" />
<fmt:formatNumber type="currency" value="${investVolume}" />
Thanks in advance!
<fmt:formatNumber type="currency" value="${investVolume}" pattern="###,###.## ¤"/>
The pattern attribute follows DecimalFormat rules.
The ¤ in the pattern represents the currency symbol.
Although I'm not sure if that ' would show in an upper or lower position this should be the way to go.

Need trick to use cached value in nocache section

I want to iterate thru single array and disable caching only for some elements.
So my idea was to keep key and get element by key in nocache section. Unfortunatelly i haven't found any possibility to:
assign cached $rec#key in nocache section,
or keep variable key definition in cached section.
Is there any way (without smarty code modification) to do it ?
here my test.tpl:
{foreach $array as $rec}
{if $rec.dynamic}
{assign var="key" value=$rec#key}
{nocache}
{$array[$key].text}
{/nocache}
{else}
{$rec.text}
{/if}
{/foreach}
and test.php:
<?php
include_once 'libs/Smarty.class.php';
$smarty=new smarty();
$smarty->caching=1;
$smarty->assign('array',array(
'r1'=>array('dynamic'=>true,'text'=>'dynamic'),
'r2'=>array('dynamic'=>false,'text'=>'static')
));
$smarty->display('test.tpl');
(of course i will use it for much more complicated things than text display:) )
I tried lot of tricks and by myself i think it is not possible, please tell me i am wrong :)
Finally, I have found a solution: Use count in your loop.
{nocache}
{counter start=0 skip=1 assign="count"}
{/nocache}
{section name="co" loop=$publication}
{nocache}
{$publication[$count].id}
{counter}
{/nocache}
{/section}
What you are trying to accomplish is, as of Smarty 3.1.x, not possible. With 3.2 Smarty will allow you to "export" values into a {nocache} section to ensure they're available when the template is re-executed.
Until 3.2 is released (don't ask for a date, I don't know) you may be able to do this yourself using a compiler function.

Resources