Comments (13)
+1
from cactus.
+1
from cactus.
+1
from cactus.
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.
from cactus.
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.
from cactus.
Thanks @krallin - I'll give that a go 👍
from cactus.
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! : )
from cactus.
@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'
from cactus.
Update: the following works
from cactus.template_tags import register
def mymarkdown(value):
return 'test'
def preBuild(site):
register.filter('mymarkdown', mymarkdown)
from cactus.
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,
from cactus.
@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.
from cactus.
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:
- Add the filter to a new library, and add that library to the built-ins (what I had originally suggested).
- Add the filter to a library that is already present in the built-ins (this is what you did).
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,
from cactus.
@krallin ah, that makes sense 😃. 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.
from cactus.
Related Issues (20)
- cactusformac.com Offline HOT 11
- django 1.6 conflicts with python 3.5 from conda install HOT 1
- fatal error for cryptography-1.5.2 during python setup.py install HOT 1
- Objects and arrays yaml variables not working HOT 4
- Using Django Compressor HOT 1
- locale problem in django 1.6.11 HOT 2
- Django variable CURRENT_PAGE.absolute_final_url not existing in Cactus 1.1.22 (440) HOT 8
- How can I remove the /posts/ part in my URL while keeping the posts folder?
- Blog Plugin Not Working? HOT 2
- Can't build because of missing staticfiles template HOT 1
- [question] Still maintained? HOT 6
- Urls in sites without locale being prefixed by 'en-us' HOT 1
- Is there a way to turn off gzip compression when deploying to AWS? HOT 1
- robots.txt shoud feature full URL to site.
- Testing site before uploading HOT 2
- Pretty URLs on S3 with HTTPS
- [question] New release? HOT 1
- cactus build fails with Python3 HOT 3
- No Updates since 3 years? HOT 9
- cactus serve fails
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cactus.