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.
Related
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!
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...
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
Looking through the Smarty debug console I discovered the variable {$u}, which contains the array video => "".
{$u} also contains photo, interview, etc. which is taken from the t1 table in the database. The database does have a url in the video column, but it is not being captured in {$u}. The problem right now is that I can't figure out where {$u} is populated.
What is the best way to find out where $u is populated?
If you can't debug your code line by line (with Zend or Xdebug), try to search your PHP code for ->assign('u', ->assign("u" or your Smarty templates for {$u=, {assign name="u", etc...
Unfortunatly there is no way to make Smarty tell you automatically where a varible was declared.
I use the pagination helper from CodeIgniter and it works. But when I see the result in my browser, I can observ unwanted spaces. CodeIgniter seems to insert them in an automatic way.
In my view:
<div><?php echo $this->pagination->create_links(); ?></div>
The code generated behind (html):
<div>
Previous
1
2
<strong>3</strong>
4
5
Next
Last
</div>
So there is a space before my previous link and two spaces before my "Last" link. Same thing happens when it's reversed (two spaces after my "First" link).
Why? It really blow my mind! Please do you know how to remove them?
Any suggestions gratefully received.
Solution (thanks to uzsolt's answer) : It works with first_tag_close and last_tag_open set to '' (see comments for more details).
Maybe you can set num_tag_open and num_tag_close config variable.
After trying all sorts of things by setting the config values from both within my controller and a application/config/pagination.php file. I managed to solve it by going into system/libraries/Pagination.php and reset the default values without any ' '.
Hope this helps someone else.