templates - concise if statements in django templating system -


here monster inefficient code:

{% link in header_links %}     {% if not link.image %}     {% if not link.url %}             <li><a href="{{ link|lower }}">{{ link }}</a></li>         {% else %}             <li><a href="{{ link.url }}">{{ link }}</a></li>         {% endif %}     {% else %}     {% if not link.url %}             <li><a href="{{ link|lower }}"><img src="{{ link }}" /></a></li>         {% else %}             <li><a href="{{ link.url }}"><img src="{{ link.image }}" /></a></li>         {% endif %}     {% endif%} {% endfor %} 

as can see, ridiculous. simple tertiary statement or 2 totally fitting, except within {% %} blocks can't access variables filters , things that.

here python/django pseduo code expresses same thing efficiency think possible.

{% link in header_links %}             <li><a href="{% print link|lower if not image.url else image.url %}">{% print "<img src='" + link.image + "' />" if link.image else print link %}</a></li> {% endfor %} 

as can see, using 2 tertiary statements awesome , more visually efficient anyway. yet code doesn't work.

any suggestions awesome!!

thanks, django noob

in closing:

we came conclusion following mvc paradigm leads me "heavy" lifting controller section , give view little thinking possible.

the pseudo code end using follows:

in view

header_links = {} links = link.object.all() link in links:     header_links['url'] = (link.name if not link.url else link.url)     header_links['name'] = (link.name if not link.image else "<img src='" + link.image +"' />") context = context({ "header_links": header_links }) 

in controller

{% link in header_links %}     <li><a href="{{ link['url']|lower }}"><img src="{{ link['name'] }}" /></a></li> {% endfor %} 

just rewrote if statement , think way looks more obvious. not clever solution it's more readable.

{% link in header_links %}     {% if not link.image  , not link.url%}         <li><a href="{{ link|lower }}">{{ link }}</a></li>     {% endif %}     {% if not link.image  , link.url%}             <li><a href="{{ link.url }}">{{ link }}</a></li>     {% endif %}     {% if link.image  , not link.url%}         <li><a href="{{ link|lower }}"><img src="{{ link }}" /></a></li>     {% endif %}     {% if link.image  , link.url%}         <li><a href="{{ link.url }}"><img src="{{ link.image }}" /></a></li>     {% endif %} {% endfor %} 

Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -