View previous topic :: View next topic |
Author |
Message |
douglassdavis Smarty Junkie
Joined: 21 Jan 2008 Posts: 541
|
Posted: Tue Mar 17, 2009 7:55 pm Post subject: counter vs. $count++ and how to not print |
|
|
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 |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
|
Back to top |
|
douglassdavis Smarty Junkie
Joined: 21 Jan 2008 Posts: 541
|
Posted: Wed Mar 18, 2009 12:51 pm Post subject: |
|
|
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 |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Wed Mar 18, 2009 1:45 pm Post subject: |
|
|
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 |
|
douglassdavis Smarty Junkie
Joined: 21 Jan 2008 Posts: 541
|
Posted: Wed Mar 18, 2009 2:49 pm Post subject: |
|
|
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 |
|
foomin Smarty n00b
Joined: 24 Mar 2009 Posts: 4
|
Posted: Tue Mar 24, 2009 5:34 pm Post subject: |
|
|
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 |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Tue Mar 24, 2009 5:51 pm Post subject: |
|
|
{$x++} is not supported. |
|
Back to top |
|
foomin Smarty n00b
Joined: 24 Mar 2009 Posts: 4
|
Posted: Tue Mar 24, 2009 5:54 pm Post subject: |
|
|
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 |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Tue Mar 24, 2009 6:22 pm Post subject: |
|
|
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 |
|
foomin Smarty n00b
Joined: 24 Mar 2009 Posts: 4
|
Posted: Tue Mar 24, 2009 7:30 pm Post subject: |
|
|
Thanks for that, I'll give it a try |
|
Back to top |
|
douglassdavis Smarty Junkie
Joined: 21 Jan 2008 Posts: 541
|
Posted: Tue Mar 24, 2009 11:53 pm Post subject: |
|
|
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. 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 |
|
|