On 15 jan, 12:02, Greg <gregplaysgui...@gmail.com> wrote:
> I'm building a CMS system in django, and in the admin I need to be
> able to inspect a template to see what nodes it contains.
> (specifically, I'm searching for a custom template tag related to the
> CMS)
> Using
>
> from django.template.loader import get_template
> t = get_template("test.html")
> print t.nodelist
>
> gives me a list of TextNodes etc

IIRC, this is only a list of top-level nodes - nested ones won't
appear in it.

> as expected for basic templates, but
> when I try it on a template that extends another template, all I get
> is the ExtendsNode in my list, ie
>
> [<ExtendsNode: extends "base.html">]
>
> I'm assuming this is a feature, not a bug, and is due to lazy loading
> or something along those lines — my question is how do I work out what
> nodes the final rendered template will have?

The base Node and NodeList classes both have a get_nodes_by_type
method that
"Return a list of all nodes (within this node and its nodelist) of the
given type".

This method is recursive, which solves the problem mentioned above.
IOW, whether or not a template extends another one, you want :

t = get_template("test.html")
print t.nodelist.get_nodes_by_type(YourNodeTypeHere)


HTH
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to