Re: Conditionals in Templates
On 4-7-2012 0:49, Javier Guerra Giraldez wrote: > On Tue, Jul 3, 2012 at 3:39 PM, Bill Freemanwrote: >> The obvious ways are: >> >> 1. Provide a model method that returns a string representing the type >> of the instance, compare against that >> 2. Decorate the instance with an attribute giving the name of the type >> as a string... > > 3. write a template tag 4. Don't use polymorphic variables in templates. Handle it in the view by providing differently named variables (x and x_list for example). -- Melvyn Sopacua -- 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 django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: Conditionals in Templates
On Tue, Jul 3, 2012 at 3:39 PM, Bill Freemanwrote: > The obvious ways are: > > 1. Provide a model method that returns a string representing the type > of the instance, compare against that > 2. Decorate the instance with an attribute giving the name of the type > as a string... 3. write a template tag -- Javier -- 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 django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: Conditionals in Templates
The obvious ways are: 1. Provide a model method that returns a string representing the type of the instance, compare against that 2. Decorate the instance with an attribute giving the name of the type as a string... Can't change the instance (I really think you can still decorate it in the view, python is flexible that way, but), instead of rows of instances, return rows of wrapper objects that have the type name as above, and the original instance as another attribute. On Tue, Jul 3, 2012 at 3:50 PM, Russ Abbottwrote: > I'm generating a table. I want the cells to be different depending on the > type of the content. How do I test for the type of the content? I'd like to > do something like this. > > > {% for row in rows %} > > {% for cell in row %} > > {% if isinstance( {{ cell }}, list) %} > > {% for element in cell %} > {{ element }} > {% endfor %} > > {% else %} > {{ cell }} > {% endif %} > > {% endfor %} > > {% endfor %} > > > But "{% if isinstance( {{ cell }}, list) %}" is not allowed. Apparently I > can't call a function in an {% if ... %} tag. Is there another way to do > this? > > Thanks. > > -- > You received this message because you are subscribed to the Google Groups > "Django users" group. > To view this discussion on the web visit > https://groups.google.com/d/msg/django-users/-/jRlkDDGDgk0J. > To post to this group, send email to django-users@googlegroups.com. > To unsubscribe from this group, send email to > django-users+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/django-users?hl=en. -- 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 django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: Conditionals in Templates
Here's a workaround I came up with. I defined a class: class TableValue: def __init__(self, value): self.value = value self.is_list = isinstance(value, list) Then in the Template I wrote {% for row in rows %} {% for cell in row %} {% if cell.is_list %} {% for element in cell.value %} {{ element }} {% endfor %} {% else %} {{ cell.value }} {% endif %} {% endfor %} {% endfor %} Is that considered decent Django? On Tuesday, July 3, 2012 12:50:15 PM UTC-7, Russ Abbott wrote: > > I'm generating a table. I want the cells to be different depending on the > type of the content. How do I test for the type of the content? I'd like to > do something like this. > > > {% for row in rows %} > > {% for cell in row %} > > {% if isinstance( {{ cell }}, list) %} > > {% for element in cell %} > {{ element }} > {% endfor %} > > {% else %} > {{ cell }} > {% endif %} > > {% endfor %} > > {% endfor %} > > > But "{% if isinstance( {{ cell }}, list) %}" is not allowed. Apparently I > can't call a function in an {% if ... %} tag. Is there another way to do > this? > > Thanks. > -- You received this message because you are subscribed to the Google Groups "Django users" group. To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/HxfDQVmchj0J. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Conditionals in Templates
I'm generating a table. I want the cells to be different depending on the type of the content. How do I test for the type of the content? I'd like to do something like this. {% for row in rows %} {% for cell in row %} {% if isinstance( {{ cell }}, list) %} {% for element in cell %} {{ element }} {% endfor %} {% else %} {{ cell }} {% endif %} {% endfor %} {% endfor %} But "{% if isinstance( {{ cell }}, list) %}" is not allowed. Apparently I can't call a function in an {% if ... %} tag. Is there another way to do this? Thanks. -- You received this message because you are subscribed to the Google Groups "Django users" group. To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/jRlkDDGDgk0J. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: Multiple choice conditionals in templates?
Thank you! I did read the template docs but I missed that one. Works great! :-) Sean On Aug 30, 2006, at 6:30 PM, Kenneth Gonsalves wrote: > > > On 30-Aug-06, at 2:45 PM, Sean Schertell wrote: > >> Obviously this doesn't work because you can't use "==" in templates. >> So how can I achieve this? > > use ifequal > > -- > > regards > kg > http://lawgon.livejournal.com > http://nrcfosshelpline.in/web/ > > > > > > > --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
Re: Multiple choice conditionals in templates?
On 30-Aug-06, at 2:45 PM, Sean Schertell wrote: > Obviously this doesn't work because you can't use "==" in templates. > So how can I achieve this? use ifequal -- regards kg http://lawgon.livejournal.com http://nrcfosshelpline.in/web/ --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
Multiple choice conditionals in templates?
How can I display different things in my template depending on the value of a single column in my database? For example: {% for e in events %} {% if e.status == '1' %}Buy Tickets{% endif %} {% if e.status == '2' %}SOLD OUT! {% endif %} {% if e.status == '3' %}Cancelled! {% endif %} {% endfor %} Obviously this doesn't work because you can't use "==" in templates. So how can I achieve this? :-) Sean --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---