Re: Using mod in if in template

2008-03-24 Thread Tim Sawyer

On Monday 24 Mar 2008, Evert Rol wrote:
> On 24 Mar 2008, at 19:29 , Tim Sawyer wrote:
> >> The second thing to do is to read the template documentation to see
> >> what is available, because doing so will turn up things like this:
> >>
> >> http://www.djangoproject.com/documentation/templates/#divisibleby
> >
> > I saw that, but I couldn't work out how to conditionally output html
> > based on
> > its value.  {% if forloop.counter|divisibleby: "4" %} doesn't work.
>
> If that's your literal code, you should probably remove the space
> between the colon and quote: divisibleby:"4"

A-ha, sorted thanks.  It wasn't obvious from the documentation that I read 
that you could actually use the filters inside {% %} blocks.  I know better 
now!

Thanks again everyone,

Tim.

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Using mod in if in template

2008-03-24 Thread scott lewis


On 2008-03-24, at 1312, Tim Sawyer wrote:
>
> Hi Folks,
>
> I want to do this:
>
> 
>  
>{% for image in Images %}
>  
>{{ image.name }}
>{{ image.comment }}
>  
>  {% if forloop.counter % 4 %}
>
>
>  {% endif %}
>{% endfor %}
>  
> 
>
> which should hopefully give me a gallery of images where each line  
> has four
> thumbnails on it.
>
> Unfortunately, I can't do the mod (%) in the if statement, as it's  
> not valid
> syntax.  Is there a way around this, or a simpler solution?



What about just converting it to a 2-dimensional array in the view?  
For example, using grouper() from the itertools recipes [1]:

def grouper(n, iterable, padvalue=None):
 "grouper(3, 'abcdefg', 'x') --> ('a','b','c'), ('d','e','f'),  
('g','x','x')"
 return izip(*[chain(iterable, repeat(padvalue, n-1))]*n)

Images = grouper(4, Images)


Then, the logic in the template is straightforward:


{% for row in Images %}
 
 {% for image in row %}
 {% if image %} ... image detail ... {% endif %}
 {% endfor %}
 
{% endfor %}



scott.


[1]: http://docs.python.org/lib/itertools-recipes.html

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Using mod in if in template

2008-03-24 Thread Evert Rol


On 24 Mar 2008, at 19:29 , Tim Sawyer wrote:
>> The second thing to do is to read the template documentation to see
>> what is available, because doing so will turn up things like this:
>>
>> http://www.djangoproject.com/documentation/templates/#divisibleby
>
> I saw that, but I couldn't work out how to conditionally output html  
> based on
> its value.  {% if forloop.counter|divisibleby: "4" %} doesn't work.

If that's your literal code, you should probably remove the space  
between the colon and quote: divisibleby:"4"


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Using mod in if in template

2008-03-24 Thread Michael Wieher
honestly i think forloop.counter is what you need to use.

when you say it doesn't work, what do you mean?  you do realize this
variable starts at '1' not '0' ?


2008/3/24, [EMAIL PROTECTED] <[EMAIL PROTECTED]>:
>
>
> This seems to work fine for me: http://dpaste.com/41059/
>
> On Mar 24, 2:29 pm, Tim Sawyer <[EMAIL PROTECTED]> wrote:
> > On Monday 24 Mar 2008, James Bennett wrote:
> >
> > > The first thing to do is to step back and remember that the Django
> > > template language is not Python
> >
> > I know that - writing it as python code was the quickest way to get
> across
> > what I wanted! :-)
> >
> > > The second thing to do is to read the template documentation to see
> > > what is available, because doing so will turn up things like this:
> >
> > >http://www.djangoproject.com/documentation/templates/#divisibleby
> >
> > I saw that, but I couldn't work out how to conditionally output html
> based on
> > its value.  {% if forloop.counter|divisibleby: "4" %} doesn't work.
> >
> > On Monday 24 Mar 2008, Michael Wieher wrote:
> >
> > > Can you simulate it with an incremental variable?
> >
> > Sorry, I don't know what you mean.  Can you explain more or give me a
> quick
> > example?
> >
> > Thanks,
> >
> > Tim.
> >
>

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Using mod in if in template

2008-03-24 Thread [EMAIL PROTECTED]

This seems to work fine for me: http://dpaste.com/41059/

On Mar 24, 2:29 pm, Tim Sawyer <[EMAIL PROTECTED]> wrote:
> On Monday 24 Mar 2008, James Bennett wrote:
>
> > The first thing to do is to step back and remember that the Django
> > template language is not Python
>
> I know that - writing it as python code was the quickest way to get across
> what I wanted! :-)
>
> > The second thing to do is to read the template documentation to see
> > what is available, because doing so will turn up things like this:
>
> >http://www.djangoproject.com/documentation/templates/#divisibleby
>
> I saw that, but I couldn't work out how to conditionally output html based on
> its value.  {% if forloop.counter|divisibleby: "4" %} doesn't work.
>
> On Monday 24 Mar 2008, Michael Wieher wrote:
>
> > Can you simulate it with an incremental variable?
>
> Sorry, I don't know what you mean.  Can you explain more or give me a quick
> example?
>
> Thanks,
>
> Tim.
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Using mod in if in template

2008-03-24 Thread Tim Sawyer

On Monday 24 Mar 2008, James Bennett wrote:
> The first thing to do is to step back and remember that the Django
> template language is not Python

I know that - writing it as python code was the quickest way to get across 
what I wanted! :-)

> The second thing to do is to read the template documentation to see
> what is available, because doing so will turn up things like this:
>
> http://www.djangoproject.com/documentation/templates/#divisibleby

I saw that, but I couldn't work out how to conditionally output html based on 
its value.  {% if forloop.counter|divisibleby: "4" %} doesn't work.

On Monday 24 Mar 2008, Michael Wieher wrote:
> Can you simulate it with an incremental variable?

Sorry, I don't know what you mean.  Can you explain more or give me a quick 
example?

Thanks,

Tim.



--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Using mod in if in template

2008-03-24 Thread James Bennett

On Mon, Mar 24, 2008 at 2:12 PM, Tim Sawyer <[EMAIL PROTECTED]> wrote:
>  Unfortunately, I can't do the mod (%) in the if statement, as it's not valid
>  syntax.  Is there a way around this, or a simpler solution?

The first thing to do is to step back and remember that the Django
template language is not Python; completely forgetting Python syntax
is a good first step to writing effective templates, because making
assumptions that some bit of syntax will or won't work will get you in
trouble.

The second thing to do is to read the template documentation to see
what is available, because doing so will turn up things like this:

http://www.djangoproject.com/documentation/templates/#divisibleby


-- 
"Bureaucrat Conrad, you are technically correct -- the best kind of correct."

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Using mod in if in template

2008-03-24 Thread Michael Wieher
Can you simulate it with an incremental variable?

2008/3/24, Tim Sawyer <[EMAIL PROTECTED]>:
>
>
> Hi Folks,
>
> I want to do this:
>
> 
>   
> {% for image in Images %}
>   
> {{ image.name }}
> {{ image.comment }}
>   
>   {% if forloop.counter % 4 %}
> 
> 
>   {% endif %}
> {% endfor %}
>   
> 
>
> which should hopefully give me a gallery of images where each line has
> four
> thumbnails on it.
>
> Unfortunately, I can't do the mod (%) in the if statement, as it's not
> valid
> syntax.  Is there a way around this, or a simpler solution?
>
> Thanks,
>
> Tim.
>
> >
>

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Using mod in if in template

2008-03-24 Thread Tim Sawyer

Hi Folks,

I want to do this:


  
{% for image in Images %}
  
{{ image.name }}
{{ image.comment }}
  
  {% if forloop.counter % 4 %}


  {% endif %}
{% endfor %}
  


which should hopefully give me a gallery of images where each line has four 
thumbnails on it.

Unfortunately, I can't do the mod (%) in the if statement, as it's not valid 
syntax.  Is there a way around this, or a simpler solution?

Thanks,

Tim.

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---