Giter Site home page Giter Site logo

Comments (13)

rukeba avatar rukeba commented on June 2, 2024

+1

from cactus.

stepmr avatar stepmr commented on June 2, 2024

+1

from cactus.

moskrc avatar moskrc commented on June 2, 2024

+1

from cactus.

WillsB3 avatar WillsB3 commented on June 2, 2024

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.

krallin avatar krallin commented on June 2, 2024

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.

WillsB3 avatar WillsB3 commented on June 2, 2024

Thanks @krallin - I'll give that a go 👍

from cactus.

krallin avatar krallin commented on June 2, 2024

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.

pmdarrow avatar pmdarrow commented on June 2, 2024

@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.

pmdarrow avatar pmdarrow commented on June 2, 2024

Update: the following works

from cactus.template_tags import register

def mymarkdown(value):
    return 'test'

def preBuild(site):
    register.filter('mymarkdown', mymarkdown)

from cactus.

krallin avatar krallin commented on June 2, 2024

@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,

from cactus.

pmdarrow avatar pmdarrow commented on June 2, 2024

@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.

krallin avatar krallin commented on June 2, 2024

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

  • 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.

pmdarrow avatar pmdarrow commented on June 2, 2024

@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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.