Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.

#1 Nov. 14, 2005 06:06:18

Tom T.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Repeating Blocks Within a Template


A bit stumped here . . . Is there a recommended convention for
repeating blocks within a template? e.g., I have a paginated
object_list generic view, and I want to repeat my pager code (i.e.,
"back", "next") at both the top and bottom of the list without copying
and pasting.

-- Tom

Offline

#2 Nov. 14, 2005 08:59:48

Simon W.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Repeating Blocks Within a Template


On 14 Nov 2005, at 06:06, Tom Tobin wrote:A bit stumped here . . . Is there a recommended convention for
repeating blocks within a template? e.g., I have a paginated
object_list generic view, and I want to repeat my pager code (i.e.,
"back", "next") at both the top and bottom of the list without copying
and pasting.Not really. You can write a custom template tag for your buttons butthat might be considered overkill - and would also mean that some ofyour presentation logic would end up in Python code (in the templatetag definition).Maybe it's time we bit the bullet and introduced a {% capture %} tag(or similar) that chucks the rendered output of its contents in avariable in the context. Then you could do this:{% capture pagination_controls %}
... HTML goes here ...
{% end capture %}
{{ pagination_controls }}

... more stuff ...

{{ pagination_controls }}I think we've avoided this in the past because it makes Django'stemplate language too much like a programming language, but I thinkit provides an elegant solution to your problem and hence should be acandidate for inclusion - unless we can come up with a more elegantsolution.Cheers,

Simon Willison

Offline

#3 Nov. 14, 2005 13:45:01

Robert W.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Repeating Blocks Within a Template


Simon Willison wrote:
>
>
> On 14 Nov 2005, at 06:06, Tom Tobin wrote:
>
>> A bit stumped here . . . Is there a recommended convention for
>> repeating blocks within a template? e.g., I have a paginated
>> object_list generic view, and I want to repeat my pager code (i.e.,
>> "back", "next") at both the top and bottom of the list without copying
>> and pasting.
>
>
> Not really. You can write a custom template tag for your buttons but
> that might be considered overkill - and would also mean that some of
> your presentation logic would end up in Python code (in the template
> tag definition).
>
> Maybe it's time we bit the bullet and introduced a {% capture %} tag
> (or similar) that chucks the rendered output of its contents in a
> variable in the context. Then you could do this:
>
> {% capture pagination_controls %}
> ... HTML goes here ...
> {% end capture %}
> {{ pagination_controls }}
>
> ... more stuff ...
>
> {{ pagination_controls }}
>
> I think we've avoided this in the past because it makes Django's
> template language too much like a programming language, but I think it
> provides an elegant solution to your problem and hence should be a
> candidate for inclusion - unless we can come up with a more elegant
> solution.
>
> Cheers,
>
> Simon Willison
>

For this kind of thing I use @inclusion_tag. This is in new-admin and
ticket #625 . It is a decorator which makes creating simple tags very easy:

--------------------
tag definition
----------------------
@inclusion_tag('farming/cow_detail')
def cow_display(cow):
return {
'name': cow.name,
'similar': find_similar_cows(cow)
}
-------------------------
template : 'farming/cow_detail'
-------------------------
<div>
<h2>{{name}}</h2>
<b> similar cows: </b>
{%for cow in similar %}
<p>{{cow.name}}</p>
{% end for %}
</div>
--------------------------
Use in other template
-------------------------
{% cow_display cow %}
------------------------

The good thing about defining a template tag is that it is easily
reusable across multiple templates, not just within one. For even
simpler cases, I use the {% include path/to/template %} tag detailed in
#598.

Offline

#4 Nov. 14, 2005 17:02:56

Richie H.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Repeating Blocks Within a Template



> For this kind of thing I use @inclusion_tag. This is in new-admin and
> ticket #625.

This is great enhancement! I'm not very clear about the wisdom of merging
such a patch with my copy of Django - can I be sure that this patch will
be merged into the head at some point? (Presumably yes, because new-admin
will be merged in.) Is there something in Trac that answers that
question?

Thanks,

--
Richie Hindle

Offline

Board footer

Moderator control

Enjoy the 18th of November
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support