|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
danbru1211 Smarty Rookie
Joined: 15 Jul 2014 Posts: 9 Location: Austria, Graz
|
Posted: Mon Nov 30, 2015 9:01 am Post subject: cache half side of if in foreach loop (maybe bug) |
|
|
EDIT: I opened a github issue: https://github.com/smarty-php/smarty/issues/110
Hi,
i want to cache the list of existing options in my <select> tag. but i dont want to cache what option will be selected by default using the selected="selection" parameter in each <option> tag.
see this code for example:
Code: | if(!$smarty->isCached('contactpersons_add.tpl')) {
$companies = []; // will be filled from db
$smarty->assign('companies', $companies); //should be cached!
}
$companyId = isset($_GET['companyId']) ? $_GET['companyId'] : $companies[0]['ID'];
$smarty->assign('companyId', $companyId, true); //dont cache this
$smarty->display('contactpersons_add.tpl'); |
this is part of my template:
Code: | <select>
{foreach from=$companies item=$company}
<option value="{$company.ID}"{if $companyId == $company.ID} selected="selected"{/if}>{$company.Name|escape}</option>
{/foreach}
</select> |
my problem here is the resulting file in the cache directory. the first time the page loads, everything works ok. But on second time, the if in the option tag does not work anymore. See the resulting cache file in cache directory here:
Code: | <select class="form-control" id="inputCompanyID" name="CompanyID" required="required">
<option value="26"<?php if ($_smarty_tpl->tpl_vars['companyId']->value == $_smarty_tpl->tpl_vars['company']->value['ID']) {?> selected="selected"<?php }?>>Company 1</option>
<option value="21"<?php if ($_smarty_tpl->tpl_vars['companyId']->value == $_smarty_tpl->tpl_vars['company']->value['ID']) {?> selected="selected"<?php }?>>Company 2</option>
<option value="12"<?php if ($_smarty_tpl->tpl_vars['companyId']->value == $_smarty_tpl->tpl_vars['company']->value['ID']) {?> selected="selected"<?php }?>>Company 3</option>
</select> |
Hope you see the problem that "$_smarty_tpl->tpl_vars['company']" does not exists on second page call because the foreach itself has been cached away. Instead, the compiler of smarty should insert the real ID of each company into the if code.
It should instead look like this (this was handmade, not smarty-compiler made:
Code: | <select class="form-control" id="inputCompanyID" name="CompanyID" required="required">
<option value="26"<?php if ($_smarty_tpl->tpl_vars['companyId']->value == 26) {?> selected="selected"<?php }?>>Company 1</option>
<option value="21"<?php if ($_smarty_tpl->tpl_vars['companyId']->value == 21) {?> selected="selected"<?php }?>>Company 2</option>
<option value="12"<?php if ($_smarty_tpl->tpl_vars['companyId']->value == 12) {?> selected="selected"<?php }?>>Company 3</option>
</select> |
How to fix this? |
|
Back to top |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Tue Dec 01, 2015 8:36 pm Post subject: |
|
|
1. Use javascript to set current selection. Completely agnostic and would work straight out the oven.
2. Use http://www.smarty.net/docs/en/language.function.html.options.tpl and cache its output based on list contents + selected item.
3. Split your optlist into a bunch of subtemplates with their own cache_id each (NOT RECOMMENDED.) |
|
Back to top |
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|