how to achieve the break tag in django template

1,400 views
Skip to first unread message

CCC

unread,
Apr 9, 2010, 7:42:15 AM4/9/10
to Django users
now i have a finished template which wrote by j2ee,like this:
<#list dailyStarList as dailyS>
<li <#if (dailyS_index % 2) ==
0>class="list_color_w"<#else>class="list_color"</#if>><h2><#list
chatAvatarList as cal>
<#if dailyS.avatarid == cal.id>
${(cal.name[0..1])!''}之星
<#break>
</#if>
</#list></h2><h3><#if (dailyMemberList)??>
<#list dailyMemberList as dm>
<#if dailyS.idx == dm.idx>
${(dm.myname)!''}
<#break>
</#if>
</#list>
</#if></h3><h4>${(dailyS.dailyNum)?c!''}</h4></li>
</#list>


And Now i want to changeit,write by django,anyone know how to achieve
the "<#break>"
in this tenplate
thanks!

CCC

unread,
Apr 11, 2010, 9:11:38 PM4/11/10
to Django users
anyone can help?Thanks advance

Jirka Vejrazka

unread,
Apr 12, 2010, 4:37:24 AM4/12/10
to django...@googlegroups.com
>> now i have a finished template which wrote by j2ee,like this:
>>         <#list  dailyStarList as dailyS>
>>                <li <#if (dailyS_index % 2) ==
>> 0>class="list_color_w"<#else>class="list_color"</#if>><h2><#list

Hi,

the syntax you're using is not standard Django template syntax, so
unless someone has replaced the default Django tempating system with
the same one as you did (and is familiar with it and read your email),
you are unlikely to get a response from this list.

Sorry

Jirka

Jirka Vejrazka

unread,
Apr 12, 2010, 4:41:27 AM4/12/10
to django...@googlegroups.com
>  the syntax you're using is not standard Django template syntax, so
> unless someone has replaced the default Django tempating system with
> the same one as you did (and is familiar with it and read your email),
> you are unlikely to get a response from this list.

Oops - sorry. I misread your email (another proof I should not reply
to anyone before my first cup of coffee ;-)

I'm not very familiar with j2ee syntax, but the logic seems a bit
too heavy for a template, you might be better off implementing the
logic in a view (hence, standard Python+Django), put the result in the
context and only display the context in a template.

Just my 2 cents.

Jirka

CCC

unread,
Apr 12, 2010, 6:01:22 AM4/12/10
to Django users
Thanks for reply
I know waht's you mean.
But in the template inside the for loop,there are some 'css',not only
just put the result in the template
That's the difficult i can't solved out

David De La Harpe Golden

unread,
Apr 12, 2010, 8:27:52 AM4/12/10
to django...@googlegroups.com
On 09/04/10 12:42, CCC wrote:
> now i have a finished template which wrote by j2ee,like this:

Your template stuff (FreeMarker I think?), well, it is not Django.

> And Now i want to changeit,write by django,anyone know how to achieve
> the "<#break>"
> in this tenplate
> thanks!
>

Well, you can't (IIRC)... but N.B. you probably only think you need it.

This isn't some strange oversight, it'd be fairly easy for django
developers to add if they actually wanted it in*. Admittedly, template
languages do have a way of slowly growing in complexity until they do
all sorts of stuff not originally envisaged, but it's a design decision
to deliberately leave things out in template languages as they're not
"for" general programming - complex loop control logic in the template
is kind of a sign you're straying into things more properly handled
elsewhere.

* note how e.g. jinja2 has a break and continue as an extension
(jinja2.ext.loopcontrols) to its django-like "for" construct - but an
extension that you have to explicitly enable so you know you're being
naughty.

Reformatting for readability and commenting (^^^) -

<#list dailyStarList as dailyS>

^^^ this could conceivably become something like
^^^ {% for daily_star in daily_star_list %}

<li
<#if (dailyS_index % 2) ==0>
class="list_color_w"
<#else>
class="list_color"
</#if>
>

^^^ django has cycle (actually FreeMarker might too for all I know),
^^^ allowing a verbosity reduction
^^^ <li class="{% cycle 'list_color_w' 'list_color' %}">

<h2>
<#list chatAvatarList as cal>
<#if dailyS.avatarid == cal.id>
${(cal.name[0..1])!''}之星
<#break>
</#if>
</#list>
</h2>

^^^ This is why you _think_ you need break. But chances are
^^^ you wouldn't do it this way in the first place. This is (ab)using
^^^ a looping construct to iterate over the avatar list until
^^^ you find a matching avatar to the current daily_star in the outer
^^^ loop - when your
^^^ daily star instances presumably could just have a related avatar,
^^^ or at least a method returning the relevant avatar.
^^^ So this should probably
^^^ just be something like (n.b. I'm vague on the precise meaning of
^^^ some FreeMarker operators but that's incidental):
^^^
^^^ <h2>{{ daily_star.avatar.name|slice:":2"|default:"" }}之星</h2>
^^^
^^^ (aside: even if you did still want to do what you are
^^^ doing but left out the break, think about it - it probably wouldn't
^^^ matter much:
^^^ it'd just pointlessly continue iterating through the list until the
^^^ end, and computers operating at the speed they do, you mightn't
^^^ notice unless your list was enormous...)

<h3>
<#if (dailyMemberList)??>
<#list dailyMemberList as dm>
<#if dailyS.idx == dm.idx>
${(dm.myname)!''}
<#break>
</#if>
</#list>
</#if>
</h3>

^^^ Again you're apparently (ab)using list/if/break to do some sort
^^^ of a related object lookup.
^^^ <h3>{{ daily_star.daily_member.myname|default:"" }}</h3>
^^^

<h4>
${(dailyS.dailyNum)?c!''}
</h4>

^^^ <h4>{{ daily_star.daily_num|stringformat:"d"|default:"" }}</h4>
^^^ or something like that, with the caveat that FreeMarker c and
^^^ %d probably aren't exact equivalents.

</li>
</#list>


Reply all
Reply to author
Forward
0 new messages