Modificar la etiqueta de plantilla personalizada antes de invocar

I have been modifying this open source Django project for my own use. There is this existing custom template tag of theirs and in the render function of the Node they have something like the following:

        plate = resolve_variable(self.plate, context)
        context.push()
        context['self'] = plate
        template = Template(f(str))
        result = template.render(context)
        context.pop()
        return result

There are several different versions of this custom tag I would like to create, but really they will only differ on the function f that is being invoked above. So instead of just copying the entire tag code multiple times I can try setting f to something else in a view function right before rendering a template that utilizes this tag. But it doesn't work because of the template compilation process, which apparently only looks at the content of the tag source file itself, and doesn't consider dynamic changes to it (e.g. altering f).

So, how can I accomplish this - somehow alter f on the fly from outside and then invoke the tag.

preguntado el 08 de noviembre de 11 a las 10:11

1 Respuestas

What I really want to do is have an optional parameter to the template tag specifying an alternate function, and if its not supplied, a default function is used- EXCEPT if the custom tag is being invoked recursively when the top level invocation supplied this optional parameter, in which case the lower level invocation should use the same function. So, I think I can just set a class variable f in the Node class to the new function if its supplied in the tag, so recursive invocations of the Nodeclass will use it. And then at the top level, f is set back to null in the __del__ method (destructor) of the node.

respondido 08 nov., 11:15

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.