can anyone help me in developing a simple webpage in jinja2

Discussion in 'Python' started by Satabdi Mukherjee, Apr 5, 2013.

  1. i am a rookie in python and i am trying to develop a simple webpage using jinja2. can anyone please help me how to do that
    i am trying in this way but showing invalid syntax error

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
    <html lang="en">
    <head>
    <title>My Webpage</title>
    </head>
    <body>
    <ul id="navigation">
    {% for item in navigation %}
    <li><a href="{{ item.href }}">{{ item.caption }}</a></li>
    {% endfor %}
    </ul>

    <h1>My Webpage</h1>
    {{ a_variable }}
    </body>
    </html>
     
    Satabdi Mukherjee, Apr 5, 2013
    #1
    1. Advertising

  2. Satabdi Mukherjee

    Jan Riechers Guest

    On 06.04.2013 01:41, Satabdi Mukherjee wrote:
    > i am a rookie in python and i am trying to develop a simple webpage using jinja2.

    can anyone please help me how to do that
    > i am trying in this way but showing invalid syntax error
    >

    [...]
    > <ul id="navigation">
    > {% for item in navigation %}
    > <li><a href="{{ item.href }}">{{ item.caption }}</a></li>
    > {% endfor %}
    > </ul>
    >
    > <h1>My Webpage</h1>
    > {{ a_variable }}

    [...]
    >


    Hello,

    the jinja2 syntax is correct that way, see also this for reference for
    variable naming:
    http://jinja.pocoo.org/docs/templates/index.html#variables

    The invalid syntax is raised when? Can you post the error a bit more
    detailed, this will help giving you any advice.

    If you know the code part raising the error and you post it, this will
    also help.

    Jan
     
    Jan Riechers, Apr 6, 2013
    #2
    1. Advertising

  3. On Sat, Apr 6, 2013 at 6:15 PM, Jan Riechers <> wrote:
    > The invalid syntax is raised when? Can you post the error a bit more
    > detailed, this will help giving you any advice.
    >
    > If you know the code part raising the error and you post it, this will also
    > help.


    Agreed. But my guess would be the lack of colon on the for loop...
    which would be highlighted by the error thrown.

    ChrisA
     
    Chris Angelico, Apr 6, 2013
    #3
  4. Satabdi Mukherjee wrote:

    > i am a rookie in python and i am trying
    > to develop a simple webpage using jinja2.
    >
    > can anyone please help me how to do that


    You might try using your jinja template
    with named tuples ....

    # -------------------------------------------

    from jinja2 import Template

    from collections import namedtuple as NT

    nt = NT( 'Navigation' , 'href caption' )

    n1 = nt( 'http://python.org' , 'python' )
    n2 = nt( 'http://cython.org' , 'cython' )
    n3 = nt( 'http://jython.org' , 'jython' )
    n4 = nt( 'http://pypy.org/' , 'pypy' )

    nav = ( n1 , n2 , n3 , n4 )

    tmpl = Template( '''\
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
    <html lang="en">
    <head>
    <title>My Webpage</title>
    </head>
    <body>
    <ul id="navigation">
    {% for url , caption in navigation %}
    <li><a href="{{ url }}">{{ caption }}</a></li>
    {% endfor %}
    </ul>

    <h1>My Webpage</h1>
    {{ a_variable }}
    </body>
    </html>
    ''' )

    print tmpl.render(
    variable = 'Navigation' , navigation = nav )


    --
    Stanley C. Kitching
    Human Being
    Phoenix, Arizona
     
    Cousin Stanley, Apr 7, 2013
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Guest
    Replies:
    0
    Views:
    859
    Guest
    Jun 28, 2003
  2. ph4nut
    Replies:
    3
    Views:
    209
    ph4nut
    Aug 12, 2010
  3. MattC
    Replies:
    2
    Views:
    214
    David Alexander
    Jul 16, 2004
  4. JoeM

    Jinja2 + jQuery tabs widget

    JoeM, Mar 14, 2012, in forum: Python
    Replies:
    2
    Views:
    475
  5. Robert Iulian

    Jinja2 installation help

    Robert Iulian, Feb 8, 2013, in forum: Python
    Replies:
    6
    Views:
    190
    Robert Iulian
    Feb 9, 2013
Loading...

Share This Page