OR operator in Smarty - smarty

I don't know if this can be classified as a bug, so I am sorry if this is posted in the wrong section.
I am trying to make the following work:
{if (preg_match("/Nokia308/i",$info) || preg_match("/Nokia309/i",$info))}
...CONTENT...
{/if}
With no joy result, however:
{if preg_match("/Nokia308/i",$info)}
{if preg_match("/Nokia309/i",$info)}
...CONTENT...
{/if}
{/if}
Works even though it is very messy coding.
Basically I want to display some content if preg_match("/Nokia308/i",$info) or preg_match("/Nokia309/i",$info) is set.
I can't see what I am doing wrong, could anybody shed some light on this?
Thanks.

I don't know what exactly you want to achieve but using:
(preg_match("/Nokia308/i",$info) || preg_match("/Nokia309/i",$info)
condition, you want to make this condition true if the $info variable contains Nokia308 or Nokia309 case-insensitive.
So if you assign:
$smarty->assign('info','Nokia307');
no ...CONTENT... string will be displayed but if you assign:
$smarty->assign('info','Nokia308');
or
$smarty->assign('info','Nokia309');
or
$smarty->assign('info','NOKIA308');
...CONTENT... string will be displayed.
So everything is working here as it should.

Related

Smarty Combine internal get variable with another value

I'm trying to build a foreach loop in smarty where i have to access specific get vars like "user_1", "user_2" etc to mark checkboxes as checked. So what i need is smth like
{if $smarty.get.user_{$foreach_current_user_id} == "on"}checked{/if}
but this doesn't work.
Is there a way to pass the loop variable to the get variable? I've haven't found smth on the internet yet...
Thanks if someone knows a solution
The easiest way is to create a variable with the desired name:
{$user_name='user_'|cat:$foreach_current_user_id}
{if $smarty.get.$user_name == "on"}checked{/if}

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

smarty concatanate a var from a file and a normal smarty one in the smarty include section

Hi I am trying to evaluate a variable from a file and a normal one but seems to be harder than it looks so :
This works:
{config_load file="archive_page.conf"
section="profile"} {include file="header.tpl" title=#pageTitle# keywords=#keywords# description=#description#}
I would like to also use my var and concatenate the text together so the below doesn't work also I have tried variations with '', "" but leads either an error message or one of the variables to display as text...
{config_load file="archive_page.conf"
section="profile"} {include file="header.tpl" title=#pageTitle#$MYVARHERE keywords=#keywords# description=#description#}
I tried various things but I can't get it to work, any help is much appreciated.
use the cat variable modifier:
title=#pageTitle#|cat:$MYVARHERE

Ordering $file by name - Smart templates

I wonder if anyone can help me. I'm using PodHawk - a basic podcast cms, I would like the admin area file select to show my files in order - by name.
The select/option dropdown uses this code, is there a straightforward way to get the dropdown to display in order by name,. I've searched but cant find anything in the Smarty documentation, but I'm probably using the wrong terminology!
{foreach from=$upload item=file}
<option value="{$file|escape:'url'}">{$file}</option>
{/foreach}
many thanks rob
Solved with many thanks to poster below -
{$upload|#sort:$smarty.const.SORT_NUMERIC}
{foreach from=$upload item=file}
<option value="{$file|escape:'url'}">{$file}</option>
{/foreach}
cgwyllie neglected that asort() returns a boolean, not the sorted array. So his approach wouldn't work. As the index is not used, a(ssociative)sort is not required.
{$_foo = $upload|sort:$smarty.const.SORT_LOCALE_STRING}
{foreach $upload as $file}
<option value="{$file|escape:'url'}">{$file|escape:"html"}</option>
{/foreach}
should do the trick. Make sure you really need that $file urlencoded, otherwise change escape:"url" to escape:"html".
(the above is Smarty3 syntax)
If the variable $upload contains an array of file names, then it should be possible to apply the PHP asort function (http://php.net/asort) to the array as a smarty modifier.
{foreach from=$upload|#asort item=file}
<option value="{$file|escape:'url'}">{$file}</option>
{/foreach}
The # symbol is needed to apply the modifier to the array as a whole, and not to each individual element. (See http://www.smarty.net/docsv2/en/language.modifiers.tpl)
If the array is of more complex data structures than just strings, the following discussion may be of use to you: http://www.smarty.net/forums/viewtopic.php?t=1079&postdays=0&postorder=asc&start=0
Edit
As mentioned by rodneyrehm, this solution is not quite correct although the poster found a satisfactory solution at: http://www.smarty.net/forums/viewtopic.php?t=1079&postdays=0&postorder=asc&start=0

Resources