can anyone help me in developing a simple webpage in jinja2

  • Thread starter Satabdi Mukherjee
  • Start date
S

Satabdi Mukherjee

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>
 
J

Jan Riechers

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
 
C

Chris Angelico

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
 
C

Cousin Stanley

Satabdi said:
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 )
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top