eudicots / Cactus

Static site generator for designers. Uses Python and Django templates.
BSD 3-Clause "New" or "Revised" License
3.47k stars 313 forks source link

small addition to template_tags.py if_current_page() #164

Open elguavas opened 9 years ago

elguavas commented 9 years ago

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 - " :

def if_current_page(context, link_url, positive=True, negative=False, startswith='false'):
    """
    Return one of the passed parameters if the URL passed is the current one.
    For consistency reasons, we use the link_url of the page.
    smg - added startswith, if 'true' operate on url startswith instead of whole url.
    """
    page = context['__CACTUS_CURRENT_PAGE__']
    if startswith == 'true':  # smg - mods as above in docstring
        return positive if page.link_url.startswith(link_url) else negative
    else:
        return positive if page.link_url == link_url else negative
krallin commented 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.

elguavas commented 9 years ago

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.

elguavas commented 9 years ago

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.