Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #40455 > unrolled thread

Embedding a for inside an html template for substitution

Started byFerrous Cranus <nikos.gr33k@gmail.com>
First post2013-03-04 07:14 -0800
Last post2013-03-04 23:46 -0800
Articles 7 — 4 participants

Back to article view | Back to comp.lang.python


Contents

  Embedding a for inside an html template for substitution Ferrous Cranus <nikos.gr33k@gmail.com> - 2013-03-04 07:14 -0800
    Re: Embedding a for inside an html template for substitution Michael Torrie <torriem@gmail.com> - 2013-03-04 08:59 -0700
      Re: Embedding a for inside an html template for substitution Ferrous Cranus <nikos.gr33k@gmail.com> - 2013-03-04 09:22 -0800
        Re: Embedding a for inside an html template for substitution Michael Torrie <torriem@gmail.com> - 2013-03-04 20:56 -0700
        Re: Embedding a for inside an html template for substitution Roland Koebler <r.koebler@yahoo.de> - 2013-03-05 18:16 +0100
      Re: Embedding a for inside an html template for substitution Ferrous Cranus <nikos.gr33k@gmail.com> - 2013-03-04 09:22 -0800
    Re: Embedding a for inside an html template for substitution nagia.retsina@gmail.com - 2013-03-04 23:46 -0800

#40455 — Embedding a for inside an html template for substitution

FromFerrous Cranus <nikos.gr33k@gmail.com>
Date2013-03-04 07:14 -0800
SubjectEmbedding a for inside an html template for substitution
Message-ID<1a5a68d8-a97e-4069-a860-c7737dd5a503@googlegroups.com>
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.

[toc] | [next] | [standalone]


#40459

FromMichael Torrie <torriem@gmail.com>
Date2013-03-04 08:59 -0700
Message-ID<mailman.2845.1362412789.2939.python-list@python.org>
In reply to#40455
On 03/04/2013 08:14 AM, Ferrous Cranus wrote:
> 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

[toc] | [prev] | [next] | [standalone]


#40468

FromFerrous Cranus <nikos.gr33k@gmail.com>
Date2013-03-04 09:22 -0800
Message-ID<6ce88927-ed2f-4045-a23c-033f18cb332a@googlegroups.com>
In reply to#40459
Τη Δευτέρα, 4 Μαρτίου 2013 5:59:34 μ.μ. UTC+2, ο χρήστης Michael Torrie έγραψε:
> On 03/04/2013 08:14 AM, Ferrous Cranus wrote:
> 
> > 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



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?

[toc] | [prev] | [next] | [standalone]


#40499

FromMichael Torrie <torriem@gmail.com>
Date2013-03-04 20:56 -0700
Message-ID<mailman.2864.1362455785.2939.python-list@python.org>
In reply to#40468
On 03/04/2013 01:06 PM, Ferrous Cranus wrote:
> 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

[toc] | [prev] | [next] | [standalone]


#40546

FromRoland Koebler <r.koebler@yahoo.de>
Date2013-03-05 18:16 +0100
Message-ID<mailman.2896.1362504186.2939.python-list@python.org>
In reply to#40468
Hi,

On Mon, Mar 04, 2013 at 09:22:38AM -0800, Ferrous Cranus wrote:
> 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

On Mon, Mar 04, 2013 at 08:56:14PM -0700, Michael Torrie wrote:
> 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')


On Mon, Mar 04, 2013 at 08:56:14PM -0700, Michael Torrie wrote:
> 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

[toc] | [prev] | [next] | [standalone]


#40469

FromFerrous Cranus <nikos.gr33k@gmail.com>
Date2013-03-04 09:22 -0800
Message-ID<mailman.2852.1362417767.2939.python-list@python.org>
In reply to#40459
Τη Δευτέρα, 4 Μαρτίου 2013 5:59:34 μ.μ. UTC+2, ο χρήστης Michael Torrie έγραψε:
> On 03/04/2013 08:14 AM, Ferrous Cranus wrote:
> 
> > 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



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?

[toc] | [prev] | [next] | [standalone]


#40505

Fromnagia.retsina@gmail.com
Date2013-03-04 23:46 -0800
Message-ID<eb33f5bd-8cbe-48ed-98c9-c549b590d689@googlegroups.com>
In reply to#40455
Τη Δευτέρα, 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 and simple, djanfo indeed is an overkill for me needs.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web