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

counter vs. $count++ and how to not print

 
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 -> General
View previous topic :: View next topic  
Author Message
douglassdavis
Smarty Junkie


Joined: 21 Jan 2008
Posts: 541

PostPosted: Tue Mar 17, 2009 7:55 pm    Post subject: counter vs. $count++ and how to not print Reply with quote

I find it so much easier to just do $count++ than to use the counter function. The problem is, when you do

<{$count++}> it prints out the value.

Is there any way to not print the value here?

Thanks.
-d
Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Tue Mar 17, 2009 8:46 pm    Post subject: Reply with quote

It's all in the documentation.

See http://www.smarty.net/manual/en/language.function.counter.php

Use
Code:

{counter print=false}
Back to top
View user's profile Send private message
douglassdavis
Smarty Junkie


Joined: 21 Jan 2008
Posts: 541

PostPosted: Wed Mar 18, 2009 12:51 pm    Post subject: Reply with quote

Thank you, but that wasn't the question. Really the question is more general. Is there a way to not print the value that an expression evaluates to? I think there should be. For example, if I do this:

{$arrayname|@print_r}

It will print out the value of the array, but then print out what print_r returns, which is not needed.

Same with {$var++}. To me, that's a much more simple way to implement a counter than the counter function.

For example this:

Code:
{counter print=false}


would not work for what I am trying to do, I would have to give it a name, then assign it to a variable as well. Because I may have several counters, and the result should be in a variable. It's just more complicated than {$count++}
Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Wed Mar 18, 2009 1:45 pm    Post subject: Reply with quote

You could write a "noprint" modifier which just returns an empty string.

File: modifier.noprint.php
Code:
function smarty_modifier_noprint($string)
{
    return '';
}
Back to top
View user's profile Send private message
douglassdavis
Smarty Junkie


Joined: 21 Jan 2008
Posts: 541

PostPosted: Wed Mar 18, 2009 2:49 pm    Post subject: Reply with quote

U.Tews wrote:
You could write a "noprint" modifier which just returns an empty string.

File: modifier.noprint.php
Code:
function smarty_modifier_noprint($string)
{
    return '';
}


Ok thanks.
Back to top
View user's profile Send private message
foomin
Smarty n00b


Joined: 24 Mar 2009
Posts: 4

PostPosted: Tue Mar 24, 2009 5:34 pm    Post subject: Reply with quote

Did you manage to sort this out? Because I'm having the same issue - I'm trying to increment using {$x++} but all I'm getting is "0 1 2 3" etc on the screen as well as my content. I would be grateful if you could let me know how you solved it.

Thanks!
Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Tue Mar 24, 2009 5:51 pm    Post subject: Reply with quote

{$x++} is not supported.
Back to top
View user's profile Send private message
foomin
Smarty n00b


Joined: 24 Mar 2009
Posts: 4

PostPosted: Tue Mar 24, 2009 5:54 pm    Post subject: Reply with quote

U.Tews wrote:
{$x++} is not supported.


Even if I have assigned it? Like so:

Code:
{assign var=x value=0}


I'm very new to Smarty so I am unaware of any specific counting tags (apart from counter which is not working). I basically need to loop through some items but stop after a certain amount (e.g. I have 10 items but only want to show first 4). In PHP I would assign a value of zero to a variable then just plus one on to it in a for loop. Is there a specific variable to use to count up?
Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Tue Mar 24, 2009 6:22 pm    Post subject: Reply with quote

You can use

Code:
{assign var=x value=$x+1}

or

{counter assign=x}


To incremement the value of $X but not print it.
Back to top
View user's profile Send private message
foomin
Smarty n00b


Joined: 24 Mar 2009
Posts: 4

PostPosted: Tue Mar 24, 2009 7:30 pm    Post subject: Reply with quote

Thanks for that, I'll give it a try Smile
Back to top
View user's profile Send private message
douglassdavis
Smarty Junkie


Joined: 21 Jan 2008
Posts: 541

PostPosted: Tue Mar 24, 2009 11:53 pm    Post subject: Reply with quote

U.Tews wrote:
{$x++} is not supported.


Hmmm.. maybe it should be taken out of the language then. It works very well... and is so simple. Smile It just seems natural that people are going to try it if it works.

This may be the wrong forum, but if Smarty 3 has the new short assign syntax, it should be ok in smarty 3 right?
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 -> General 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