Embedding a for inside an html template for substitution

F

Ferrous Cranus

Hello pythonistas!

I'am tryign to populate a table with dictionary keys and values:

Foe that iam using an html template and the questions is what i should write inside 'files.html' so then then the python script populate the table.

<table>
<tr><th>SuperHost - Economy</th></tr>
<tr><td>ΧώÏος στο δίσκο: 1 GB</td></tr>
<tr><td>Μηνιαία Κίνηση δεδομένων: 1 GB</td></tr>
<tr><td>Control Panel: cPanel 11 & Fantastico Deluxe</td></tr>
<tr><td>Domains: 1</td></tr>
<tr><td>Subdomains: 1</td></tr>
<tr><td>FTP Accounts: 1</td></tr>
<tr><td>Emails (POP3): 2</td></tr>
<tr><td>WebMail: [ RoundCube|Horde|Squirrel ]: ÎΑΙ</td></tr>
<tr><td>Mysql Databases: 2</td></tr>
</table>

Instead of writing the above html data inside my html template how would i write it with a for that then will be substituted by the python script?

can you please write an example for me that user "files.html" and gets populates by "files.py" ?

i want os ee how it lloks like please!

Thank you.
 
M

Michael Torrie

Instead of writing the above html data inside my html template how
would i write it with a for that then will be substituted by the
python script?

What templating system are you using? Django's?
can you please write an example for me that user "files.html" and
gets populates by "files.py" ?

If you're using a CGI script, just use a normal python for loop and
print out the stuff you want using this kind of notation:

for x in xrange(4):
print "<tr><td>{0}</td></tr>".format(x)


Another alternative is to make your own templating system. Have it load
and parse the html file (maybe using one of the xml or html parsers),
and substitude some sort of field marker for data. But that is
re-inventing the wheel.

http://wiki.python.org/moin/Templating
 
F

Ferrous Cranus

Τη ΔευτέÏα, 4 ΜαÏτίου 2013 5:59:34 μ.μ. UTC+2, ο χÏήστης Michael Torrie έγÏαψε:
What templating system are you using? Django's?







If you're using a CGI script, just use a normal python for loop and

print out the stuff you want using this kind of notation:



for x in xrange(4):

print "<tr><td>{0}</td></tr>".format(x)





Another alternative is to make your own templating system. Have it load

and parse the html file (maybe using one of the xml or html parsers),

and substitude some sort of field marker for data. But that is

re-inventing the wheel.



http://wiki.python.org/moin/Templating



Firstly thank you for your answer, but iam a bit confised.

can i just put the liens you provided me inside "files.html" and thwy will work?

Thats pure pythjon code!

for x in xrange(4):
print "<tr><td>{0}</td></tr>".format(x)

wont html complain?
 
F

Ferrous Cranus

Τη ΔευτέÏα, 4 ΜαÏτίου 2013 5:59:34 μ.μ. UTC+2, ο χÏήστης Michael Torrie έγÏαψε:
What templating system are you using? Django's?







If you're using a CGI script, just use a normal python for loop and

print out the stuff you want using this kind of notation:



for x in xrange(4):

print "<tr><td>{0}</td></tr>".format(x)





Another alternative is to make your own templating system. Have it load

and parse the html file (maybe using one of the xml or html parsers),

and substitude some sort of field marker for data. But that is

re-inventing the wheel.



http://wiki.python.org/moin/Templating



Firstly thank you for your answer, but iam a bit confised.

can i just put the liens you provided me inside "files.html" and thwy will work?

Thats pure pythjon code!

for x in xrange(4):
print "<tr><td>{0}</td></tr>".format(x)

wont html complain?
 
M

Michael Torrie

What do you advise me to do?
Generate html via python code like print '''stuf......''' or use an
html templating system?
Up until now i was using the first method and i though it would be a
nice idea to seperate design from code.

But please also provide me a simple example of 'files.html' and
'files.py' so i can understand how does the templating system you
mentioned work. Was it Django?!


Just generating html code is simple, but it does become a bit messy over
time.

Check out that link in my previous e-mail to a list of python templating
engines. Choose one and try it. No I cannot provide any example code.
I haven't used any of these systems; I'm just googling for you. Most
of them come with examples anyway.

Check out, among others:
http://www.cheetahtemplate.org/docs/users_guide_html/
And actually this one uses cheetah to do static pages (not a full-blown
web app):
http://www.ivy.fr/tahchee/

I do recommend you at least take a look at Django. It may be overkill
for your needs. It does contain a templating engine, but it also
contains a whole lot more useful things for doing web-based applications.

Here's the link in case you missed it:
http://wiki.python.org/moin/Templating
 
N

nagia.retsina

Τη ΔευτέÏα, 4 ΜαÏτίου 2013 5:14:00 μ.μ. UTC+2, ο χÏήστης Îίκος ΓκÏ33κ έγÏαψε:
Hello pythonistas!



I'am tryign to populate a table with dictionary keys and values:



Foe that iam using an html template and the questions is what i should write inside 'files.html' so then then the python script populate the table.



<table>

<tr><th>SuperHost - Economy</th></tr>

<tr><td>ΧώÏος στο δίσκο: 1 GB</td></tr>

<tr><td>Μηνιαία Κίνηση δεδομένων: 1 GB</td></tr>

<tr><td>Control Panel: cPanel 11 & Fantastico Deluxe</td></tr>

<tr><td>Domains: 1</td></tr>

<tr><td>Subdomains: 1</td></tr>

<tr><td>FTP Accounts: 1</td></tr>

<tr><td>Emails (POP3): 2</td></tr>

<tr><td>WebMail: [ RoundCube|Horde|Squirrel ]: ÎΑΙ</td></tr>

<tr><td>Mysql Databases: 2</td></tr>

</table>



Instead of writing the above html data inside my html template how would i write it with a for that then will be substituted by the python script?



can you please write an example for me that user "files.html" and gets populates by "files.py" ?



i want os ee how it lloks like please!



Thank you.

Thank you Michael, i have decided to use cheetahtemplate. It seems nice andsimple, djanfo indeed is an overkill for me needs.
 
R

Roland Koebler

Hi,

can i just put the liens you provided me inside "files.html" and thwy
will work?

Thats pure pythjon code!
There are several template-engines where you more or less include
python-code into the template, e.g.: empy, mako, pyratemp

Check out that link in my previous e-mail to a list of python templating
engines. Choose one and try it. No I cannot provide any example code.
Here's an example for pyratemp (where I'm the author ;)):

files.html:

<table>
<tr><th>@!title!@</th></tr>
<!--(for e in mylist)-->
<tr><td>@!e!@</td></tr>
<!--(end)-->
</table>

files.py:

import pyratemp
t = pyratemp.Template(filename="files.html")
result = t(title="title ...", mylist=["entry 1", "entry 2", "entry 3"])
print result.encode("ascii", 'xmlcharrefreplace')


I do recommend you at least take a look at Django. It may be overkill
for your needs. It does contain a templating engine,
Last time I tried, the template-engine of Django did not work on its
own. If you need Django-like templates without Django, you can use Jinja.
And if you need a real sandbox (so that you can use untrusted templates),
I would recommend Jinja.


Roland
 

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

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,831
Latest member
RusselWill

Latest Threads

Top