|
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 |
afrancesconi Smarty Rookie
Joined: 08 Feb 2015 Posts: 6
|
Posted: Mon Apr 06, 2015 8:25 am Post subject: Change the value of a Smarty var during foreach |
|
|
Hello,
I'm using a Smarty template to show a list of blog posts. The template file obviously(?) contains a {foreach} loop to show them sequentally:
Code: | {foreach $previews as $post}
<article class="{$class.post}" id="{$post.id}">
....
</article>
{/foreach} |
Everything works, but now I want to mark some of them as "draft". I thought to use the value of {$class.post}: it's just a predefined string "item-post", but for some articles I would like it to be "item-post draft".
Now the problem: how can I get inside each loop, check if the post is a draft, then change the value of {$class.post} accordingly?
Moreover: the template file should be as simple as possible (actually it should remain the same as now). I'll not be the maintainer of the template file, but I can change the logic behind on the PHP.
Should I define a custom foreach loop (like {iterateposts})?... Or is all of this a bad practice?
Thanks for the help |
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Mon Apr 06, 2015 2:21 pm Post subject: |
|
|
How will you find out if a post is a draft?
I would not reassign {$class.post} in the foreach loop because the original content gets lost. Perhaps you can load $class['postdraft'] in your PHP script with "item-post draft"
Code: | {foreach $previews as $post}
<article class="{if put draft condition here}{$class.postdraft}{else}{$class.post}{/if}" id="{$post.id}">
....
</article>
{/foreach} |
|
|
Back to top |
|
afrancesconi Smarty Rookie
Joined: 08 Feb 2015 Posts: 6
|
Posted: Tue Apr 07, 2015 7:26 am Post subject: |
|
|
Quote: | How will you find out if a post is a draft? |
On the PHP side I have access at each post object (in fact I assign the list of posts to the $previews variable). Then I call fetch() to generate the HTML from the template.
Anyway, the solution you described is ok but it makes the template a little harder to customize (I must leave it really, really, simple). I'm thinking to create a customized for-loop for the list of articles, so I can use it this way:
Code: |
{iterateposts $previews as $post}
<article class="{$class.post}" id="{$post.id}">
....
</article>
{/iterateposts}
|
"iterateposts" should be defined like the Smarty's foreach, but with a difference: for each loop, it changes the value of $class.post based on the $post->isDraft property.
Is it possible to do this? |
|
Back to top |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Tue Apr 07, 2015 7:40 am Post subject: |
|
|
You can just make template function that output necessary class name based on certain input value.
No need for any convoluted stuff you're trying to implement.
Code: | <article class="{myproperclass $post.isDraft}" id="{$post.id}">
....
</article> |
Also, I hope you remember, that idref can't start from number? |
|
Back to top |
|
afrancesconi Smarty Rookie
Joined: 08 Feb 2015 Posts: 6
|
Posted: Tue Apr 07, 2015 7:45 am Post subject: |
|
|
AnrDaemon wrote: | Also, I hope you remember, that idref can't start from number? |
Can you explain this last line? What's the "idref" you're talking about? |
|
Back to top |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
|
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
|