|
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 |
TonyB Smarty n00b
Joined: 11 Apr 2009 Posts: 4
|
Posted: Tue Aug 18, 2009 9:05 am Post subject: Problem with {section} and new {for} |
|
|
The following code works with Smarty 2:
Code: | {section name = news_item loop = $articles }
<li class="notice">
<p class="notice_title">{$articles[news_item].title} </p>
<p class="byline">By: {$articles[news_item].username} <br />
{$articles[news_item].date_published|date_format} </p>
{$articles[news_item].body} <br />
{if isset($articles[news_item].comment_flag)}
{if isset($member_logged_in)}
{if isset($articles[news_item].watch_flag)}
<a href="bb/viewtopic.php?t={$articles[news_item].comment_flag}&watch=1&ticket={$ticket}">Comments</a>
{else}
<a href="bb/viewtopic.php?t={$articles[news_item].comment_flag}&watch=0&ticket={$ticket}">Comments</a> <br />
{/if}
{else}
<p>Login in to view comments</p>
{/if}
{/if}
</li>
{/section} |
It would not work with Smarty 3 Alpha, I got the following error message:
Code: | Code: 0
Error: Syntax Error in template "G:\Tony\Webwork\private\smarty\templates\news.tpl" on line 10 " {section name = news_item loop = $articles } " - Unexpected " }", expected one of: "}" , " "
File: G:\Tony\Webwork\private\smarty\sysplugins\internal.templatecompilerbase.php
Line: 264
|
I tried to change the code to suit Smarty 3 Alpha using the guidance given in this post:
Quote: | PostPosted: Wed Oct 22, 2008 2:59 pm
Some interesting things happening in alpha lately.
First, we are proposing to deprecate both {section} and {foreach} in favor of a new {for}
function for looping. Examples:
Code:
{for $var in $myarray}
key is {$var:key}
index is {$var:index}
iteration is {$var:iteration}
total is {$var:total}
{/for}
Notice we use {$var:foo} to access array properties, and the iteration var is used as the
identifier (no need to "name" a for loop.) |
I changed my code to this:
Code: | {for $article in $articles}
<li class="notice">
<p class="notice_title">{$article.title} </p>
<p class="byline">By: {$article.username} <br />
{$article.date_published|date_format} </p>
{$article.body} <br />
{if isset($article.comment_flag)}
{if isset($member_logged_in)}
{if isset($article.watch_flag)}
<a href="bb/viewtopic.php?t={$article.comment_flag}&watch=1&ticket={$ticket}">Comments</a>
{else}
<a href="bb/viewtopic.php?t={$article.comment_flag}&watch=0&ticket={$ticket}">Comments</a> <br />
{/if}
{else}
<p>Login in to view comments</p>
{/if}
{/if}
</li>
{/for} |
I got the following error message:
Code: | Code: 0
Error: Syntax Error in template "G:\Tony\Webwork\private\smarty\templates\news.tpl" on line 10 " {for $article in $articles} " - Unexpected "in", expected one of: "==" , "!=" , "(>,GT)" , "(<,LT)" , "(>=,GE)" , "(<=,LE)" , "===" , "!=="
File: G:\Tony\Webwork\private\smarty\sysplugins\internal.templatecompilerbase.php
Line: 264 |
Can anybody help me with this? |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Tue Aug 18, 2009 2:39 pm Post subject: |
|
|
try {foreach}, I think those got split up at some point and was noted. Where did you see this syntax for {for} ? |
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Tue Aug 18, 2009 3:39 pm Post subject: |
|
|
Regarding your first problem: Do not use whitespaces with the Smarty delimiters. '{ ' and ' }' will nit be interpreted by Smarty as tag delimiter. They are literals which will allow to write javascript code including curly braces which the need to use {literal} tags as needed in Smarty2.
{section name = news_item loop = $articles} would work.
Indeed array processing was consolidated already many month ago in the {forach} tag. it allows the old Smarty2 syntax and the new {foreach $item in $array} syntax. |
|
Back to top |
|
TonyB Smarty n00b
Joined: 11 Apr 2009 Posts: 4
|
Posted: Sun Aug 23, 2009 8:14 am Post subject: Problem with {section} and new {for} |
|
|
mohrt,
Thanks for the help,
I tried using {foreach ... in ... } with the following code:
Code: | {foreach $article in $articles}
<li class="notice">
<p class="notice_title">{$article.title} </p>
<p class="byline">By: {$article.username} <br />
{$article.date_published|date_format} </p>
{$article.body} <br />
{if isset($article.comment_flag)}
{if isset($member_logged_in)}
{if isset($article.watch_flag)}
<a href="bb/viewtopic.php?t={$article.comment_flag}&watch=1&ticket={$ticket}">Comments</a>
{else}
<a href="bb/viewtopic.php?t={$article.comment_flag}&watch=0&ticket={$ticket}">Comments</a> <br />
{/if}
{else}
<p>Login in to view comments</p>
{/if}
{/if}
</li>
{/foreach} |
I got the following error message:
Code: | Code: 0
Error: Syntax Error in template "G:\Tony\Webwork\private\smarty\templates\news.tpl" on line 10 " {foreach $article in $articles} " - Unexpected " ", expected one of: "}" , "(&&,AND)" , "(||,OR)" , ISODD , ISNOTODD , ISEVEN , ISNOTEVEN , ISODDBY , ISNOTODDBY , ISEVENBY , ISNOTEVENBY , ISDIVBY , ISNOTDIVBY
File: G:\Tony\Webwork\private\smarty\sysplugins\internal.templatecompilerbase.php
Line: 264 |
The syntax for {for} came from:
http://www.phpinsider.com/smarty-forum/viewtopic.php?t=14168&postdays=0&postorder=asc&start=42
U.Tews,
Thanks for the help. |
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Sun Aug 23, 2009 2:30 pm Post subject: |
|
|
Sorry , my fault. The correct syntax is like in PHP {foreach $array as $item} |
|
Back to top |
|
xime Smarty n00b
Joined: 30 Nov 2009 Posts: 1
|
Posted: Mon Nov 30, 2009 3:21 am Post subject: xime |
|
|
Everyone. Here Zhen Renao, I also came along for the ride and increase popularity points. |
|
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
|
|