Open amitu opened 11 years ago
+1
+1
+1
Can anyone tell me what the status is here? I'd like to know if (and how) to create and use custom Django template tags with Cactus?
Thanks.
You can actually do this today (at least in v3, which is now the master version) using a plugin.
Here's an example:
from django import template
from django.template.loader import get_template
def signup_button(context, location):
"""
Display the signup button
"""
ctx = copy.copy(context)
ctx.update({"loc": location})
return ctx
def preBuild(_):
register = template.Library()
register.inclusion_tag(get_template('includes/buttons/signup-button.html'), takes_context=True)(signup_button)
template.base.builtins.append(register)
Note that it's probably possible to clean this up a bit (and not hack into builtins
). Looking at how Django loads new templatetags files is probably the best way to find out.
I seem to recall I had injected into builtins
to not have to re-load this tag which was going to be used on every single page of my website, but I'm sure there's a cleaner way.
Thanks @krallin - I'll give that a go :+1:
Let me know if you run into any trouble. It's been a while since I used that plugin, but I'm pretty sure that site used to build so there has to be a way to make it work! : )
@krallin the snippet you posted doesn't seem to work. I tried it with a really simple filter:
from django import template
def mymarkdown(value):
return 'test'
def preBuild(site):
register = template.Library()
register.filter('mymarkdown', mymarkdown)
Which gives me:
django.template.base.TemplateSyntaxError: Invalid filter: 'mymarkdown'
Update: the following works
from cactus.template_tags import register
def mymarkdown(value):
return 'test'
def preBuild(site):
register.filter('mymarkdown', mymarkdown)
@pmdarrow ,
You might have missed that line from the snippet:
template.base.builtins.append(register)
If you don't add that line, then you'd have to {% load ... %}
the library.
Cheers,
@krallin I'm writing a custom filter, and I registered it with Cactus' library using
from cactus.template_tags import register
register.filter('mymarkdown', mymarkdown)
so no builtins
hacking was required.
@pmdarrow ,
I understand that. I was simply explaining why the snippet I provided did not work for you.
Basically, there are two ways to add a filter:
Note that the cactus.template_tags
library is added to the Django built-ins for you by Cactus:
https://github.com/koenbok/Cactus/blob/2dd7159280363f0b762d43e3bfa1ec1a2a6effea/cactus/site.py#L156
So, there's still builtins
hacking, only it's not your plugin doing it ;)
Now, obviously, both ways work equally well! : )
Cheers,
@krallin ah, that makes sense :smiley:. It would be awesome if there was some docs for this, but I see that's on the roadmap in #117. Might be worth closing this issue now that there's a solution.
Once # 30 is merged, the following diff implements this feature.
It is a bit hacky, I am making cactus project itself a valid django app by adding cactus.models module and cactus.templatetags package. Then I am putting all plugins available as cactus.templatestags.PLUGIN_NAME, Sample plugin, plugins/t.py:
To use it in template:
I thought of some naming convention for plugins, so only some plugins are added to cactus.templatetags.PLUGIN_NAME, but rejected it as now I can ship a blog plugin that contains both plugin hooks and custom templatetag libraries all related to blogging, list of posts etc.