I'm trying to modify an application with codeigniter..
but the if else loop etc.. is not the default if else ex.:
{if isset($form1)}
{/if}
{foreach $data_ as $row}
{/foreach}
{cycle value="odd,event"}
where I can find a tutorial to learn this php particularity in template?
thanks!
Related
I'm working on a legacy application that is written in PHPfox.
There is this foreach loop in one of my view.html.php files:
{foreach from=$aReportSubmissions key=iKey item=aReportSubmission}
<td>{$aReportSubmission.event_name}</td>
{/foreach}
The problem is that $aReportSubmission.event_name is an array and it gets outputted as Array. How can I loop through this variable and show the individual events?
PHPfox uses template tags that resemble Smarty template engine.
The following did NOT work for me:
{foreach from=$aReportSubmission.event_name item=iEvent} {iEvent},{/foreach}
I can see $ is missing before iEvent inside the inner loop
Check if this is works
{foreach from=$aReportSubmissions key=iKey item=aReportSubmission}
<td>{foreach from=$aReportSubmission.event_name item=iEvent} {$iEvent},{/foreach}</td>
{/foreach}
Try with this
{foreach from=$aReportSubmissions key=iKey item=aReportSubmission}
{$aReportSubmission.event_name|print_r}
{/foreach}
I would like to define smarty block names according to smarty data, but I can't seem to do it.
Example:
{foreach $array as $code}
{block name=block_$code}
<div id='{$code}'></div>
{/block}
{/foreach}
My purpose is to extend a specific block_$code block by a child template. Is this possible or is there some other trick I could use to do this?
Thanks.
You can use the {assign} block and the cat modifier. For example
{foreach $array as $code}
{assign var=foo value="block_"|cat:$code}
{block name=$foo}
<div>
{/block}
{/foreach}
N.b. I've not tested this, but it should work. You might also be able to short-circuit this and just use {block name="block_"|cat:$code}.
I was able to find the following link from 2011 indicating that this wasn't possible at that time. I suspect it still isn't:
http://www.smarty.net/forums/viewtopic.php?t=19805&highlight=block%20variable%20name
The good news it that I was able to figure out how to make my code work without it. I wanted to be able to override just one of the divs defined by the foreach. Here's how I can do it:
Parent:
{foreach $array as $code}
{block name=code_loop}
<div>Normal Stuff</div>
{/block}
{/foreach}
Child:
{block name=code_loop}
{if $code == 'code of interest'}
<div>New Stuff</div>
{else}
{$smarty.block.parent}
{/if}
{/block}
I wanted opinion is it a good practice to do things like this while working with MVC architecture:
{foreach from=$items item="list"}
{if $list.index < 5}
{assign value="good" var=$class_name}
{else}
{if $list.index % 2 eq 1}
{assign value="bad" var=$class_name}
{else}
{assign value="average" var=$class_name}
{/if}
{/if}
{/foreach}
Or should I do such things inside php and then just access with:
{foreach from=$items item="list"}
{$list.class_name}
{/foreach}
The reason I'm asking this is, because I was told it's need to be done inside templates(because it's styling issues and etc), but I think opposite, I think it needs to be done inside PHP controller so that way you leave templates a little bit cleaner.
So what is the better approach and why?
If you referring class_name as in the <a **class="class_name"**>, then yes, You should generate that in the smarty template so that the template has control over how it should look. If you referring class_name to something else that is class Class_name, and you want to print that out. Then no, it should stay within your PHP code.
But all these are neither best practices stated anywhere or documented, but more company standard or team lead preferences. And you do what they want if you want to keep your job smooth and easy.
What is wrong with this Smarty php templating code? If I include either of these two loops individually in the file, they work. But if I have them both in the file like below only the first loop gets completed.
{include file="vote_js.tpl"}
{section name=i loop=$posts}
{include file="posts_bitother.tpl"}
{/section}
{section name=j loop=$posts2}
{include file="posts_bitposts.tpl"}
{/section}
So for example
{section name=j loop=$posts2}
{include file="posts_bitposts.tpl"}
{/section}
works fine on it's own.
Why is this happening and what is the way around it?
Solved it, was an error in my smarty code later on
Here is the Sample Code:
{section name="firstlevel" loop=$xxx}
<ul>
{section name="secondlevel" loop=$xxx[firstlevel]}
<li>$xxx[firstlevel][secondlevel].values</li>
{/section}
</ul>
{sectionelse}
No values
{/section}
{if $smarty.section.firstlevel.last}
{include file="pagination.tpl"}
{/if}
I have limited 10 records per page, but this smarty code not calling the include file pagination. Please suggest what wrong in it or give idea...
I'm not a Smarty guru but I believe "last" only returns true while in the section. This would probably be preferable anyway as there is little or no use showing pagination when there are no records.
{section name=firstlevel loop=$xxx}
<ul>
{section name=secondlevel loop=$xxx[firstlevel]}
<li>$xxx[firstlevel][secondlevel].values</li>
{/section}
</ul>
{if $smarty.section.firstlevel.last}
{include file="pagination.tpl"}
{/if}
{sectionelse}
No values
{/section}