Jinja2 install issue
Question: I am getting the following error message when attempting `{% set foo = 42 %}`
in my html template. The template works fine when the `set` is not used.

    
    
    TemplateSyntaxError: Invalid block tag: 'set', expected 'endblock' or 'endblock content'
    

Below I have added the full session log as suggested in comments.

    
    
    *** Running dev_appserver with the following flags:
        --skip_sdk_update_check=yes --port=15093 --admin_port=8018
    Python command: /usr/bin/python2.7
    INFO     2016-05-14 18:36:26,513 devappserver2.py:762] Skipping SDK update check.
    INFO     2016-05-14 18:36:26,696 api_server.py:204] Starting API server at: http://localhost:56265
    INFO     2016-05-14 18:36:26,701 dispatcher.py:197] Starting module "default" running at: http://localhost:15093
    INFO     2016-05-14 18:36:26,704 admin_server.py:118] Starting admin server at: http://localhost:8018
    ERROR    2016-05-14 18:37:48,778 webapp2.py:1528] Invalid block tag: 'set', expected 'endblock' or 'endblock content'
    Traceback (most recent call last):
      File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.3/webapp2.py", line 1511, in __call__
        rv = self.handle_exception(request, response, e)
      File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.3/webapp2.py", line 1505, in __call__
        rv = self.router.dispatch(request, response)
      File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.3/webapp2.py", line 1253, in default_dispatcher
        return route.handler_adapter(request, response)
      File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.3/webapp2.py", line 1077, in __call__
        return handler.dispatch()
      File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.3/webapp2.py", line 547, in dispatch
        return self.handle_exception(e, self.app.debug)
      File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.3/webapp2.py", line 545, in dispatch
        return method(*args, **kwargs)
      File "/Users/brian/googleapps/lastturn/views.py", line 187, in get
        self.response.out.write(template.render(path, template_values))
      File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/ext/webapp/template.py", line 91, in render
        t = _load_internal_django(template_path, debug)
      File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/ext/webapp/template.py", line 165, in _load_internal_django
        template = django.template.loader.get_template(file_name)
      File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/_internal/django/template/loader.py", line 160, in get_template
        template = get_template_from_string(template, origin, template_name)
      File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/_internal/django/template/loader.py", line 168, in get_template_from_string
        return Template(source, origin, name)
      File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/_internal/django/template/__init__.py", line 158, in __init__
        self.nodelist = compile_string(template_string, origin)
      File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/_internal/django/template/__init__.py", line 186, in compile_string
        return parser.parse()
      File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/_internal/django/template/__init__.py", line 281, in parse
        compiled_result = compile_func(self, token)
      File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/_internal/django/template/loader_tags.py", line 195, in do_extends
        nodelist = parser.parse()
      File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/_internal/django/template/__init__.py", line 281, in parse
        compiled_result = compile_func(self, token)
      File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/_internal/django/template/loader_tags.py", line 173, in do_block
        nodelist = parser.parse(('endblock', 'endblock %s' % block_name))
      File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/_internal/django/template/__init__.py", line 279, in parse
        self.invalid_block_tag(token, command, parse_until)
      File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/_internal/django/template/__init__.py", line 332, in invalid_block_tag
        raise self.error(token, "Invalid block tag: '%s', expected %s" % (command, get_text_list(["'%s'" % p for p in parse_until])))
    TemplateSyntaxError: Invalid block tag: 'set', expected 'endblock' or 'endblock content'
    ERROR    2016-05-14 18:37:48,784 wsgi.py:279] 
    Traceback (most recent call last):
      File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/runtime/wsgi.py", line 267, in Handle
        result = handler(dict(self._environ), self._StartResponse)
      File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.3/webapp2.py", line 1519, in __call__
        response = self._internal_error(e)
      File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.3/webapp2.py", line 1511, in __call__
        rv = self.handle_exception(request, response, e)
      File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.3/webapp2.py", line 1505, in __call__
        rv = self.router.dispatch(request, response)
      File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.3/webapp2.py", line 1253, in default_dispatcher
        return route.handler_adapter(request, response)
      File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.3/webapp2.py", line 1077, in __call__
        return handler.dispatch()
      File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.3/webapp2.py", line 547, in dispatch
        return self.handle_exception(e, self.app.debug)
      File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.3/webapp2.py", line 545, in dispatch
        return method(*args, **kwargs)
      File "/Users/brian/googleapps/lastturn/views.py", line 187, in get
        self.response.out.write(template.render(path, template_values))
      File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/ext/webapp/template.py", line 91, in render
        t = _load_internal_django(template_path, debug)
      File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/ext/webapp/template.py", line 165, in _load_internal_django
        template = django.template.loader.get_template(file_name)
      File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/_internal/django/template/loader.py", line 160, in get_template
        template = get_template_from_string(template, origin, template_name)
      File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/_internal/django/template/loader.py", line 168, in get_template_from_string
        return Template(source, origin, name)
      File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/_internal/django/template/__init__.py", line 158, in __init__
        self.nodelist = compile_string(template_string, origin)
      File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/_internal/django/template/__init__.py", line 186, in compile_string
        return parser.parse()
      File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/_internal/django/template/__init__.py", line 281, in parse
        compiled_result = compile_func(self, token)
      File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/_internal/django/template/loader_tags.py", line 195, in do_extends
        nodelist = parser.parse()
      File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/_internal/django/template/__init__.py", line 281, in parse
        compiled_result = compile_func(self, token)
      File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/_internal/django/template/loader_tags.py", line 173, in do_block
        nodelist = parser.parse(('endblock', 'endblock %s' % block_name))
      File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/_internal/django/template/__init__.py", line 279, in parse
        self.invalid_block_tag(token, command, parse_until)
      File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/_internal/django/template/__init__.py", line 332, in invalid_block_tag
        raise self.error(token, "Invalid block tag: '%s', expected %s" % (command, get_text_list(["'%s'" % p for p in parse_until])))
    TemplateSyntaxError: Invalid block tag: 'set', expected 'endblock' or 'endblock content'
    INFO     2016-05-14 18:37:48,792 module.py:812] default: "GET / HTTP/1.1" 500 -
    

Below I have added the revisions I made to my `views.py`. The commented out
lines indicate the original jinja2 environment statements. These revisions are
in response to suggestions made in comments. But I still get the same error
message which seems to suggest jinja2 is not being used.

    
    
    TEMPLATE_DIR = os.path.join(os.path.dirname(__file__), 'templates')
    #jinja_environment = \
    #jinja2.Environment(loader=jinja2.FileSystemLoader(TEMPLATE_DIR))
    
    JINJA_ENVIRONMENT = jinja2.Environment(
        loader=jinja2.FileSystemLoader(os.path.dirname(__file__)),
        extensions=['jinja2.ext.autoescape'],
        autoescape=True)
    
    class BaseHandler(webapp2.RequestHandler):
    
        @webapp2.cached_property
        def jinja2(self):
            return jinja2.get_jinja2(app=self.app)
    
        def render_template(
            self,
            filename,
            template_values,
            **template_args
            ):
    #template = jinja_environment.get_template(filename)
        template = JINJA_ENVIRONMENT.get_template(filename)
            self.response.out.write(template.render(template_values))
    

Below is the base.html template.

    
    
    <html>
      <head>
        <link rel="stylesheet" href="/static/css/reset.css">
        <link rel="stylesheet" href="/static/css/style.css">
      </head>
      <body>
        <div class="wrapper">
        {% block content %}
        {% endblock content %}
        </div>
      </body>
    </html>
    

Below is the "heart" of the actual template. By "heart", I mean I have left
out the guts because this is just a test.

    
    
    {% extends "base.html" %}
        {% block content %}
        <center>
            <h1>Create or edit your own Blog</h1>
          </center>
          {% set foo = 42 %}
          {{ foo }}
    
        {% endblock content %}
    

[This answer suggests to me that jinja2 is not installed
(properly?).](http://stackoverflow.com/questions/31799710/)

Is there some other part of installation that I have to change?

[This answer shows more input about **ipython**
installation,](http://stackoverflow.com/questions/27843950/) but I am using
**google-app-engine** which may be different. The listing below confirms that
Jinja2 2.8 is installed.

    
    
    server:~ brian$ pip freeze
    Warning: cannot find svn location for setuptools===0.6c12dev-r85381
    all==0.0.6
    altgraph==0.7.1
    atari-py==0.0.17
    bdist-mpkg==0.4.4
    bonjour-py==0.3
    caller-module==0.0.9
    Django==1.3.1
    include-server===3.1-toolwhip.1
    Jinja2==2.8
    macholib==1.3
    MarkupSafe==0.23
    mod-python==3.3.1
    modulegraph==0.8.1
    numpy==1.11.0
    PIL==1.1.7
    Pillow==3.2.0
    public==0.0.0
    py2app==0.5.3
    pyglet==1.2.4
    pyobjc-core==2.3.2a0
    pyobjc-framework-AddressBook==2.3.2a0
    
    pyOpenSSL==0.12
    PyRSS2Gen==1.0.0
    python-dateutil==1.5
    PyYAML==3.11
    render==1.0.0
    requests==2.9.1
    six==1.10.0
    Twisted==11.0.0
    xattr==0.6.1
    zope.interface==3.5.1
    You are using pip version 8.1.1, however version 8.1.2 is available.
    server:~ brian$ 
    


Answer: After much help via comments to my original question, and to much stumbling, I
have finally answered my question. I was confused by both webapp2 and Jinja
having a template.render() but they are different.

template.render() from webapp seems to require 2 parameters -- the path and
the template name -- and my current code uses this format. However, I want to
use Jinja2 where template.render() requires only 1 parameter -- the template
name.

According to the [google docs for using Jinja in the guestbook
app](https://cloud.google.com/appengine/docs/python/gettingstartedpython27/generating-
dynamic-content-templates) the following format was shown.

    
    
    JINJA_ENVIRONMENT = jinja2.Environment(
        loader=jinja2.FileSystemLoader(os.path.dirname(__file__)),
        extensions=['jinja2.ext.autoescape'],
        autoescape=True)
    

But it turns out that does not work well with my chosen BaseHandler class
which puts templates in their own subdirectory, "templates". Instead I used
the following code where TEMPLATE_DIR replaces **file** in the "loader="
sentence .

    
    
    TEMPLATE_DIR = os.path.join(os.path.dirname(__file__), 'templates')
    JINJA_ENVIRONMENT = jinja2.Environment(
        loader=jinja2.FileSystemLoader(TEMPLATE_DIR ),
        extensions=['jinja2.ext.autoescape'],
        autoescape=True)
    

Btw, I no longer import the webapp `template`. And I use the following style
in my "get def"s.

    
    
    template = JINJA_ENVIRONMENT.get_template('index.html')
    self.response.out.write(template.render( template_values))
    

So, although the question asks "Jinja install issue", it was not exactly the
install that was problematic, rather I had difficulty integrating Jinja with
my existing directory structure.

