Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom Template Tags Don't Work #3

Open
rossm6 opened this issue Nov 24, 2019 · 1 comment
Open

Custom Template Tags Don't Work #3

rossm6 opened this issue Nov 24, 2019 · 1 comment

Comments

@rossm6
Copy link

rossm6 commented Nov 24, 2019

The custom template tags - for example 'try_to_include' - all subclass from the Template.Node class and implement a render method. But this render method in fact gets passed a RequestContext object and not a context object which your code assumes.

@gridlined
Copy link
Collaborator

The context received here should be that which is given from the template itself. What should happen is that:

  • the developer provides context to the tag as either a string literal:
    {% try_to_include "path/to/template.html" %}
    or some variable:
    {% try_to_include context %}
  • the tag is initialized with a template.Variable() that will look for the "template_name" property in some context, when resolved
  • upon rendering, the context, which was given as the argument, is used to attempt to resolve the template_name
    • if the context was a literal string, the result is the same literal string
    • if the context was a string variable, the result is that string
    • if the context was an object or dict containing a resolvable "template_name" property or key, the corresponding value is the result
  • the result is then used when we get_template() by name
    • if the template is found, then we render() it and the resulting string is returned
    • upon expected template.TemplateDoesNotExist, template.VariableDoesNotExist, or AttributeError, the rendering given is an empty string
    • other raised exceptions will cause the tag to fully error out

The only reason you should receive a RequestContext in the IncludeNode's render() would be if that is the context passed from the template itself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants