>
## page was renamed from Jinja2
= Jinja2 =
Jinja2 is a modern and designer friendly templating language for Python, modelled after Django’s templates.
* https://palletsprojects.com/p/jinja/
* https://jinja.palletsprojects.com/en/2.10.x/
== Slackware 14 installation ==
{{{#!highlight sh
wget https://pypi.python.org/packages/source/J/Jinja2/Jinja2-2.7.1.tar.gz
cp Jinja2-2.7.1.tar.gz /tmp
tar xvzf Jinja2-2.7.1.tar.gz
cd Jinja2-2.7.1
python setup.py build
python setup.py install
}}}
== Check installation ==
{{{#!highlight python
python
Python 2.7.3 (default, Jul 3 2012, 21:16:07)
[GCC 4.7.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import jinja2
>>> jinja2.__version__
'2.7.1'
>>> quit()
}}}
== Jinja2 test ==
Check [[Python/CherryPy]]
{{{#!highlight sh
mkdir -p /var/www/htdocs/jinja2test/templates
cd /var/www/htdocs/jinja2test
}}}
File '''/var/www/htdocs/jinja2test/jinja2test.wsgi'''
{{{#!highlight python
import sys
sys.stdout = sys.stderr
import cherrypy
from jinja2 import Environment, FileSystemLoader
env = Environment(loader=FileSystemLoader('/var/www/htdocs/jinja2test/templates'))
cherrypy.config.update({'environment': 'embedded'})
class Jinja2Test(object):
@cherrypy.expose
def index(self):
return "Hello World Jinja2Test!!!!"
@cherrypy.expose
def add(self,param1,param2):
return str( int(param1)+int(param2) )
@cherrypy.expose
def testpage(self):
t = env.get_template('test.html')
navItems=[]
for i in range(1,10):
navItems.append( {'href':'hrefx','caption':'cap %d'%(i) } )
return t.render( titlex="Titleeeee" , navigation=navItems )
jjtest = Jinja2Test()
application = cherrypy.Application(jjtest, script_name=None, config=None)
}}}
File '''/var/www/htdocs/jinja2test/templates/test.html'''
{{{#!highlight html
{% if navigation|count > 0 %}
{% for item in navigation %}
-
{{item.caption}}
{% endfor %}
{% endif %}
My webpage {{titlex}}
}}}
Add virtual host in '''/etc/httpd/vhosts.conf'''
{{{
ServerName localhostjinja2test
DocumentRoot "/var/www/htdocs/jinja2test"
WSGIScriptAlias / /var/www/htdocs/jinja2test/jinja2test.wsgi
Require local
}}}
Add host to '''/etc/hosts'''
{{{
127.0.0.1 localhostjinja2test
}}}
Restart apache
* /etc/rc.d/rc.httpd restart
Open URLs:
* http://localhostjinja2test/
* http://localhostjinja2test/add/3/4
* http://localhostjinja2test/testpage
== Include other templates inside a template (partial HTML) ==
{{{#!highlight html
{% include 'othertemplate.html' %}
}}}