Hi All,

I'm trying to use this new namespace feature[1] to access variables outside 
the loop context but I'm unsuccesful to do it as the documentation 
specifies.

[1] http://jinja.pocoo.org/docs/2.10/templates/#assignments

This is my script:

#!/home/..venv.../bin/python

import jinja2
from jinja2 import Environment, FileSystemLoader

if __name__ == "__main__":

    env = Environment(
        loader=FileSystemLoader('/home/..../templates/'),
        extensions=['jinja2.ext.loopcontrols']
   )

    print "jinja2 version: " + jinja2.__version__

    template=env.get_template("ns.j2")
    print template.render()


And the template, almost as in the docuemntation :


{% set items=["test1","test2","test3"]
{% set ns = namespace(found=false) %}
{% for item in items %}
    {% if item == "test2" %}
        {% set ns.found = true %}
    {% endif %}
{% endfor %}

Found item having something: {{ ns.found }}


The error:

$ ./cm.py 

jinja2 version: 2.10

Traceback (most recent call last):
  File "./cm.py", line 15, in <module>
    template=env.get_template("ns.j2")
  File 
"/home/ebarrera/PycharmProjects/chipmunk/sosreport-pollux-controller-2/chipmunk/lib/python2.7/site-packages/jinja2/environment.py",
 
line 830, in get_template
    return self._load_template(name, self.make_globals(globals))
  File 
"/home/ebarrera/PycharmProjects/chipmunk/sosreport-pollux-controller-2/chipmunk/lib/python2.7/site-packages/jinja2/environment.py",
 
line 804, in _load_template
    template = self.loader.load(self, name, globals)
  File 
"/home/ebarrera/PycharmProjects/chipmunk/sosreport-pollux-controller-2/chipmunk/lib/python2.7/site-packages/jinja2/loaders.py",
 
line 125, in load
    code = environment.compile(source, name, filename)
  File 
"/home/ebarrera/PycharmProjects/chipmunk/sosreport-pollux-controller-2/chipmunk/lib/python2.7/site-packages/jinja2/environment.py",
 
line 591, in compile
    self.handle_exception(exc_info, source_hint=source_hint)
  File 
"/home/ebarrera/PycharmProjects/chipmunk/sosreport-pollux-controller-2/chipmunk/lib/python2.7/site-packages/jinja2/environment.py",
 
line 780, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/home/ebarrera/PycharmProjects/chipmunk/templates/ns.j2", line 2, 
in template
    {% set ns = namespace(found=false) %}
jinja2.exceptions.TemplateSyntaxError: expected token 'end of statement 
block', got '{'


Can you guys tell me what I'm doing wrong ?

Thanks

-- 
You received this message because you are subscribed to the Google Groups 
"pocoo-libs" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/pocoo-libs.
For more options, visit https://groups.google.com/d/optout.

Reply via email to