Open elguavas opened 9 years ago
Can you show me how this is used in your template files?
Note that you can add your own template tags with a plugin, so I'm not sure it's necessary to have this built-in to Cactus.
E.g. create plugins/my_template_tags.py
, and add:
from django import template
def is_subtree(...):
...
def preBuild(site):
register = template.Library()
register.simple_tag(takes_context=True)(is_subtree)
template.base.builtins.append(register)
Better docs on this might be useful
Let me know.
i agree a plugin is a much cleaner way to do this, so with the extra guidance you've given i'll attempt this solution instead.
as for how i use it, as i mentioned it may be unwanted for general use. the site in question is divided into "sections" (like "Photos", ""Files", "Music" etc.) that are each a subtree of the pages directory. i found it useful to be able to easily tell, in a template, if the page i was at was in a particular section/subtree. one example use is for having different content in a sidebar depending what site section the page is in, another is for keeping the toplevel/navbar item of the dropdown for the section active while on any page in the section, like so:
<li class="dropdown {% if_current_page '/photos/' 'active' '' 'true' %}">
however as i now have some info on creating a plugin for my own template tags i'll probably use that solution instead.
ok implemented the functionality i wanted as a custom tag via plugin per example and it works well.
and yes, one day some docs on how to hook into various things via plugins would be awesome. ;) thanks.
hi, i've found several reasons for wanting to easily know if i'm in a pages "subtree" of my site within a template, so i made the following tiny addition to template_tags.py so that if_current_page() can be used for this purpose.
with the small edit in place you can add 'true' as an optional final argument in a {% if_current_page ... %} tag to have it match a path starting with the passed in link_url instead of matching the whole link_url.
dunno if this is of any more general use than my own, but fwiw here is my edited if_current_page(), my comments start with "smg - " :