Opened 18 years ago

Closed 18 years ago

Last modified 18 years ago

#2830 closed enhancement (wontfix)

Patch to add subviews for template tags

Reported by: gch@… Owned by: Adrian Holovaty
Component: Template system Version:
Severity: normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

In django/template/__init__.py, there exists a special tag function for performing common inclusion tasks, basically to insert the output of another template into a given template.

This is quite useful, but in most cases it'd be better for a template tag to call a python function rather than just inserting another template. I took the code for inclusion_tag, and created a new tag function, subview_tag, which instead uses a python function to generate content for a template tag. Like inclusion_tag, it is allowed to take the context and just pass it along.

In my case, I use subview_tags to be able to write custom tags in templates that end up doing rather complex tasks in python, without having to go through the normal hassle.

Here's an example of using subview_tag:

# Subview function
def my_subview(context, some_argument):
    return "This is an example page, with an argument %s" % some_argument

# register a new syntax tag: {% view my_argument %}
register.subview_tag('view', takes_context=True)(my_subview)

Attachments (1)

subview_tag.diff (1.7 KB ) - added by anonymous 18 years ago.

Download all attachments as: .zip

Change History (4)

by anonymous, 18 years ago

Attachment: subview_tag.diff added

comment:1 by gch@…, 18 years ago

Component: Admin interfaceTemplate system

Oops. Mismarked category.

comment:2 by Malcolm Tredinnick, 18 years ago

Resolution: wontfix
Status: newclosed

This doesn't seem to add anything you can't already do with either inclusion_tag() -- since you can execute as much Python code as you like inside your function there -- or, more generally simple_tag() after we fix #1105.

comment:3 by gch@…, 18 years ago

Just a note. Inclusion tag ONLY allows you to insert other templates within a given template, so no, inclusion_tag is not a suitable replacement for the problem that subview_tag tries to solve.

But yes, it looks like #1105 does also solve this problem, so I'll hope that that solution gets rolled in sometime.

Note: See TracTickets for help on using tickets.
Back to Top