Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

MODIFIER: is_first - prints only first occurrence of string

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Plugins
View previous topic :: View next topic  
Author Message
colynb
Smarty n00b


Joined: 12 Aug 2004
Posts: 4

PostPosted: Thu Aug 12, 2004 11:59 pm    Post subject: MODIFIER: is_first - prints only first occurrence of string Reply with quote

I needed a way to print a string only when it's the first occurrence within a loop, so I created this variable modifier.

Code:
/**
 * Smarty plugin
 * @package Smarty
 * @subpackage plugins
 */


/**
 * Smarty is_first modifier plugin
 *
 * Type:     modifier<br>
 * Name:     is_first<br>
 * Date:     Aug 12, 2004
 * Purpose:  checks if first occurrence of string
 * Input:    string to check
 * Example:
    {if $var|is_first}
        {$var}
    {/if}
 * @param string
 * @return boolean
 */
   
function smarty_modifier_is_first($string)
{
   
   global $smarty_prev_occ;
   
   if ($string !== $smarty_prev_occ) {
      
      $smarty_prev_occ = $string;
      
      return true;
   
   } else {
   
      return false;
   
   }

}


/*********** EXAMPLE SETUP ************/

// array returned from a database
$stories = array(
   
   array(
      'section'  => 'SECTION ONE',
      'headline' => 'Headline 1',
      'subhead'  => 'Subheadline 1',
      'author'   => 'Author 1'
   ),
   
   array(
      'section'  => 'SECTION ONE',
      'headline' => 'Headline 2',
      'subhead'  => 'Subheadline 2',
      'author'   => 'Author 2'
   ),
   
   array(
      'section'  => 'SECTION TWO',
      'headline' => 'Headline 3',
      'subhead'  => 'Subheadline 3',
      'author'   => 'Author 3'
   )
);


// assign array to smarty template
$smarty->assign('stories', $stories);
      
   

/*********** EXAMPLE TEMPLATE ************/

{section name=id loop=$stories}
    {if $stories[id].section|is_first}
        <h1>{$stories[id].section}</h1>
    {/if}
    {$stories[id].headline}<br>
    {$stories[id].subhead}<br>
    <$stories[id].author}<p>
    ....
{/section}


/*********** EXAMPLE RESULTS ************/

<h1>SECTION ONE</h1>
Headline 1<br>
Subheadline 1<br>
Author 1<P>

Headline 2<br>
Subheadline 2<br>
Author 2<P>

<h1>SECTION TWO</h1>
Headline 3<br>
Subheadline 3<br>
Author 3<P>



All comments and suggestions for improvement are welcome!
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Fri Aug 13, 2004 12:30 am    Post subject: Reply with quote

I was going to say, that you may want to review example 7-25 in the manual: http://smarty.php.net/manual/en/language.function.section.php but I realize now that you aren't using a nested array structure for your sections.

So basically, you are using this to see if a value has changed (which supposes that values are arriving from a sorted list). I can see how some folks would find that useful.

FWIW, you can do this in template:

Code:
{assign var=last value=''}
{section name=id loop=$stories}
    {if $last != $stories[id].section}
        <h1>{$stories[id].section}</h1>
        {assign var=last value=$stories[id].section}
    {/if}
    {$stories[id].headline}<br>
    {$stories[id].subhead}<br>
    <$stories[id].author}<p>
    ....
{/section}


You may want to enhance your modifier to allow for multiple levels of checking:

[php:1:b4a1125f7a]<?php

$smarty_prev_occ = array();

function smarty_modifier_is_first($string, $var='_')
{
global $smarty_prev_occ;
if ($string !== $smarty_prev_occ[$var]) {
$smarty_prev_occ[$var] = $string;
return true;
} else {
return false;
}
}

?>[/php:1:b4a1125f7a]

Then multi-level group statements such as the following won't collide:

Code:
{if $stories[id].section|is_first:"section"}
  ...
   {if $stories[id].second_section|is_first:"second_section"}
   ...
Back to top
View user's profile Send private message
colynb
Smarty n00b


Joined: 12 Aug 2004
Posts: 4

PostPosted: Fri Aug 13, 2004 4:38 pm    Post subject: Reply with quote

Thanks! That solves some of the issues I was having with it.

Looking ahead, I wonder if there's a way to make this modifier inherently know the scope without having to pass it another variable. Perhaps it could get access to the section's name or loop attributes through some kind of inheritance somehow.

--colynb
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Plugins All times are GMT
Page 1 of 1

 
Jump to:  
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
Protected by Anti-Spam ACP