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

Support for dynamically included templates #4

Open
eliasdorneles opened this issue Mar 26, 2019 · 0 comments
Open

Support for dynamically included templates #4

eliasdorneles opened this issue Mar 26, 2019 · 0 comments

Comments

@eliasdorneles
Copy link

Hello!

Thanks for writing and sharing this library, it's pretty handy! 👍 🌮

I've bumped into an issue when using a dynamic include like {% include variable_pointing_to_a_template_path %} -- which is supported by Django, but fails with TemplateDoesNotExist when you run get_node_instances(...) on a template that contains that (see example project attached).

I believe it would be a better behavior to skip loading the template in these cases (when it's a dynamic variable instead of a hardcoded path).

This is the Traceback:

>>> from django.template.loader import get_template
>>> from django.template.base import VariableNode
>>> from template_analyzer.djangoanalyzer import get_node_instances
>>> tmpl = get_template('index.html')
>>> get_node_instances(tmpl, VariableNode)
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/home/elias/.virtualenvs/web/local/lib/python2.7/site-packages/template_analyzer/djangoanalyzer.py", line 243, in get_node_instances
    return _scan_nodes(nodelist, context, instances)
  File "/home/elias/.virtualenvs/web/local/lib/python2.7/site-packages/template_analyzer/djangoanalyzer.py", line 158, in _scan_nodes
    results += _extend_nodelist(node, context, instance_types)
  File "/home/elias/.virtualenvs/web/local/lib/python2.7/site-packages/template_analyzer/djangoanalyzer.py", line 114, in _extend_nodelist
    results += _scan_nodes(block.nodelist, context, instance_types, block, ignore_blocks=all_block_names)
  File "/home/elias/.virtualenvs/web/local/lib/python2.7/site-packages/template_analyzer/djangoanalyzer.py", line 182, in _scan_nodes
    results += _scan_nodes(subnodelist, context, instance_types, current_block)
  File "/home/elias/.virtualenvs/web/local/lib/python2.7/site-packages/template_analyzer/djangoanalyzer.py", line 147, in _scan_nodes
    template = get_template(node.template.var)
  File "/home/elias/.virtualenvs/web/local/lib/python2.7/site-packages/django/template/loader.py", line 25, in get_template
    raise TemplateDoesNotExist(template_name, chain=chain)
TemplateDoesNotExist: template_point

Here is the content of the main templates showing the issue (attached a full project reproducing it):

$ cat myapp/templates/index.html 
{% extends "base.html" %}
{% block body %}
<h1>block index</h1>
{% with template_point="t1.html" %}
{% include template_point %}
{% endwith %}
{% endblock %}
$ cat myapp/templates/other.html 
{% extends "base.html" %}
{% block body %}
<h1>block other</h1>
{% with template_point="t2.html" %}
{% include template_point %}
{% endwith %}
{% endblock %}

testingproj.zip

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

No branches or pull requests

1 participant