Jump to content

if smarty condition to select the first result


Recommended Posts

I'm trying to show the first post of the news section in a different way respect to the other posts in the news list.
My problem is that the if condition that select the first post doesn't work.
I'm trying to use the code i found here  and that i'm pasting here :
 

Example 7.37. first property example


{* show table header at first iteration *}
<table>
{foreach $items as $i}
  {if $i@first}
    <tr>
      <th>key</td>
      <th>name</td>
    </tr>
  {/if}
  <tr>
    <td>{$i@key}</td>
    <td>{$i.name}</td>
  </tr>
{/foreach}
</table>

So i went In the smartblog module, inside postcategory.tlp
In the middle has :
 

{foreach from=$postcategory item=post}
    {include file="./category_loop.tpl" postcategory=$postcategory}
{/foreach}

And then i went to the included file category_loop.tpl that i changed in this way writing an if condition with smarty
 

{if $post@ifirst}
<div itemtype="#" itemscope="" class="sdsarticleCat1 clearfix col-xs-12">
    <div id="smartblogpost-{$post.id_post}">
   
    <div class="articleContent1 col-xs-8">
    
          {assign var="options" value=null}
                            {$options.id_post = $post.id_post} 
                            {$options.slug = $post.link_rewrite}
                    {assign var="activeimgincat" value='0'}
                    {$activeimgincat = $smartshownoimg} 
                    {if ($post.post_img != "no" && $activeimgincat == 0) || $activeimgincat == 1}
                    <a itemprop="url" href='{smartblog::GetSmartBlogLink('smartblog_post',$options)}' title="{$post.meta_title}" class="imageFeaturedLink">
              <img itemprop="image"  alt="{$post.meta_title}"  src="{$modules_dir}/smartblog/images/{$post.post_img}-single-default.jpg" class="imageFeatured">
              </a>
                    {/if}
          
    </div>
    <div class="articleContent1d col-xs-4">
	<div class="sdsarticleHeader">
         {assign var="options" value=null}
                            {$options.id_post = $post.id_post} 
                            {$options.slug = $post.link_rewrite}
                            <p class='sdstitle_block'><a title="{$post.meta_title}" href='{smartblog::GetSmartBlogLink('smartblog_post',$options)}'>{$post.meta_title}</a></p>
             {assign var="options" value=null}
                        {$options.id_post = $post.id_post}
                        {$options.slug = $post.link_rewrite}
               {assign var="catlink" value=null}
                            {$catlink.id_category = $post.id_category}
                            {$catlink.slug = $post.cat_link_rewrite}
         <span>{l s='Posted by' mod='smartblog'} <span itemprop="author">{if $smartshowauthor ==1} <i class="icon icon-user"></i>  {if $smartshowauthorstyle != 0}{$post.firstname} {$post.lastname}{else}{$post.lastname} {$post.firstname}{/if}</span>{/if}   <i class="icon icon-tags"></i>  <span itemprop="articleSection"><a href="{smartblog::GetSmartBlogLink('smartblog_category',$catlink)}">{if $title_category != ''}{$title_category}{else}{$post.cat_name}{/if}</a></span>  <span class="comment">  <i class="icon icon-comments"></i>  <a title="{$post.totalcomment} Comments" href="{smartblog::GetSmartBlogLink('smartblog_post',$options)}#articleComments">{$post.totalcomment} {l s=' Comments' mod='smartblog'}</a></span>{if $smartshowviewed ==1}  <i class="icon icon-eye-open"></i>{l s=' views' mod='smartblog'} ({$post.viewed}){/if}</span>
    </div>
           <div class="sdsarticle-des">
          <span itemprop="description" class="clearfix"><div id="lipsum">
	{$post.short_description}</div></span>
         </div>
        <div class="sdsreadMore">
                  {assign var="options" value=null}
                        {$options.id_post = $post.id_post}  
                        {$options.slug = $post.link_rewrite}  
                         <span class="more"><a title="{$post.meta_title}" href="{smartblog::GetSmartBlogLink('smartblog_post',$options)}" class="r_more">{l s='Read more' mod='smartblog'} </a></span>
        </div>
    </div>
   </div>
</div>
{/if}
<div itemtype="#" itemscope="" class="sdsarticleCat clearfix col-xs-4">
    <div id="smartblogpost-{$post.id_post}">
   
    <div class="articleContent">
    
          {assign var="options" value=null}
                            {$options.id_post = $post.id_post} 
                            {$options.slug = $post.link_rewrite}
                    {assign var="activeimgincat" value='0'}
                    {$activeimgincat = $smartshownoimg} 
                    {if ($post.post_img != "no" && $activeimgincat == 0) || $activeimgincat == 1}
                    <a itemprop="url" href='{smartblog::GetSmartBlogLink('smartblog_post',$options)}' title="{$post.meta_title}" class="imageFeaturedLink">
              <img itemprop="image"  alt="{$post.meta_title}"  src="{$modules_dir}/smartblog/images/{$post.post_img}-single-default.jpg" class="imageFeatured">
              </a>
                    {/if}
          
    </div>
	<div class="sdsarticleHeader">
         {assign var="options" value=null}
                            {$options.id_post = $post.id_post} 
                            {$options.slug = $post.link_rewrite}
                            <p class='sdstitle_block'><a title="{$post.meta_title}" href='{smartblog::GetSmartBlogLink('smartblog_post',$options)}'>{$post.meta_title}</a></p>
             {assign var="options" value=null}
                        {$options.id_post = $post.id_post}
                        {$options.slug = $post.link_rewrite}
               {assign var="catlink" value=null}
                            {$catlink.id_category = $post.id_category}
                            {$catlink.slug = $post.cat_link_rewrite}
         <span>{l s='Posted by' mod='smartblog'} <span itemprop="author">{if $smartshowauthor ==1} <i class="icon icon-user"></i>  {if $smartshowauthorstyle != 0}{$post.firstname} {$post.lastname}{else}{$post.lastname} {$post.firstname}{/if}</span>{/if}   <i class="icon icon-tags"></i>  <span itemprop="articleSection"><a href="{smartblog::GetSmartBlogLink('smartblog_category',$catlink)}">{if $title_category != ''}{$title_category}{else}{$post.cat_name}{/if}</a></span>  <span class="comment">  <i class="icon icon-comments"></i>  <a title="{$post.totalcomment} Comments" href="{smartblog::GetSmartBlogLink('smartblog_post',$options)}#articleComments">{$post.totalcomment} {l s=' Comments' mod='smartblog'}</a></span>{if $smartshowviewed ==1}  <i class="icon icon-eye-open"></i>{l s=' views' mod='smartblog'} ({$post.viewed}){/if}</span>
    </div>
           <!--<div class="sdsarticle-des">-->
          <!--<span itemprop="description" class="clearfix"><div id="lipsum">-->
	<!--{$post.short_description}</div></span>-->
         <!--</div>-->
        <div class="sdsreadMore">
                  {assign var="options" value=null}
                        {$options.id_post = $post.id_post}  
                        {$options.slug = $post.link_rewrite}  
                         <span class="more"><a title="{$post.meta_title}" href="{smartblog::GetSmartBlogLink('smartblog_post',$options)}" class="r_more">{l s='Read more' mod='smartblog'} </a></span>
        </div>
   </div>
</div>

The problem is that the if condition is skipped and isn't working:
How can i solve this problem?

Link to comment
Share on other sites

Dear NC

 

I tryed your solution writing this in postcategory.tlp
 



{foreach from=$postcategory item=post name='postcategory'}
    {include file="./category_loop.tpl" postcategory=$postcategory}
{/foreach}

 


and this in category_loop.tlp
 

{if $smarty.foreach.postcategory.first}

{/if}

But is still not working.


 

 

Here's how I usually do something for the first item only:

{foreach from=$categories item='category' name='categories'}
    {if $smarty.foreach.categories.first}
        First item code here
    {/if}
{/foreach}
Link to comment
Share on other sites

I'm going to guess that you can't create a foreach loop in one TPL file and then reference it in another. Try:

{foreach from=$postcategory item=post name='postcategory'}
    {include file="./category_loop.tpl" postcategory=$postcategory first=$smarty.foreach.postcategory.first}
{/foreach}

Then you can use {$first} in category_loop.tpl.

Link to comment
Share on other sites

  • 2 weeks later...

Thank you very much, this was the answer to my problem


 

I'm going to guess that you can't create a foreach loop in one TPL file and then reference it in another. Try:

{foreach from=$postcategory item=post name='postcategory'}
    {include file="./category_loop.tpl" postcategory=$postcategory first=$smarty.foreach.postcategory.first}
{/foreach}

Then you can use {$first} in category_loop.tpl.

Edited by oby1sky (see edit history)
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...