MoinMoin Logo
  • Comments
  • Immutable Page
  • Menu
    • Navigation
    • RecentChanges
    • FindPage
    • Local Site Map
    • Help
    • HelpContents
    • HelpOnMoinWikiSyntax
    • Display
    • Attachments
    • Info
    • Raw Text
    • Print View
    • Edit
    • Load
    • Save
  • Login

Navigation

  • Start
  • Sitemap

Upload page content

You can upload content for the page named below. If you change the page name, you can also upload content for another page. If the page name is empty, we derive the page name from the file name.

File to load page content from
Page name
Comment

Revision 18 as of 2025-05-31 19:32:34
  • Python
  • Jinja2

Contents

  1. Jinja2
    1. Slackware 14 installation
    2. Check installation
    3. Jinja2 test
    4. Include other templates inside a template (partial HTML)

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

Toggle line numbers
   1 wget https://pypi.python.org/packages/source/J/Jinja2/Jinja2-2.7.1.tar.gz
   2 cp Jinja2-2.7.1.tar.gz /tmp
   3 tar xvzf Jinja2-2.7.1.tar.gz
   4 cd Jinja2-2.7.1
   5 python setup.py build
   6 python setup.py install

Check installation

Toggle line numbers
   1 python
   2 Python 2.7.3 (default, Jul  3 2012, 21:16:07) 
   3 [GCC 4.7.1] on linux2
   4 Type "help", "copyright", "credits" or "license" for more information.
   5 >>> import jinja2
   6 >>> jinja2.__version__
   7 '2.7.1'
   8 >>> quit()

Jinja2 test

Check Python/CherryPy

Toggle line numbers
   1 mkdir -p /var/www/htdocs/jinja2test/templates
   2 cd /var/www/htdocs/jinja2test

File /var/www/htdocs/jinja2test/jinja2test.wsgi

Toggle line numbers
   1 import sys
   2 sys.stdout = sys.stderr
   3 import cherrypy
   4 
   5 from jinja2 import Environment, FileSystemLoader
   6 env = Environment(loader=FileSystemLoader('/var/www/htdocs/jinja2test/templates'))
   7 
   8 cherrypy.config.update({'environment': 'embedded'})
   9 
  10 class Jinja2Test(object):
  11     @cherrypy.expose
  12     def index(self):
  13         return "Hello World Jinja2Test!!!!"
  14     @cherrypy.expose
  15     def add(self,param1,param2):
  16        return str( int(param1)+int(param2) )
  17     @cherrypy.expose
  18     def testpage(self):
  19         t = env.get_template('test.html')
  20         navItems=[]
  21         for i in range(1,10):
  22                 navItems.append( {'href':'hrefx','caption':'cap %d'%(i) } )
  23         return t.render( titlex="Titleeeee" , navigation=navItems )
  24 
  25 jjtest = Jinja2Test()
  26 application = cherrypy.Application(jjtest, script_name=None, config=None)

File /var/www/htdocs/jinja2test/templates/test.html

Toggle line numbers
   1 <html>
   2     <head></head>
   3     <body>
   4         <ul id="navigation">
   5         {% if navigation|count > 0 %}
   6         {% for item in navigation %}
   7         <li>
   8           <a href="{{item.href}}">{{item.caption}}</a> 
   9         </li>
  10         {% endfor %}
  11         {% endif %}
  12         </ul>
  13         <h1>My webpage {{titlex}} </h1>
  14     </body>
  15 </html>

Add virtual host in /etc/httpd/vhosts.conf

<VirtualHost *:80>
    ServerName localhostjinja2test
    DocumentRoot "/var/www/htdocs/jinja2test"
    WSGIScriptAlias / /var/www/htdocs/jinja2test/jinja2test.wsgi
    <Directory "/var/www/htdocs/jinja2test">
      Require local
    </Directory>
</VirtualHost>

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)

Toggle line numbers
   1 {% include 'othertemplate.html' %}
  • MoinMoin Powered
  • Python Powered
  • GPL licensed
  • Valid HTML 4.01