Override part of base template

12 views
Skip to first unread message

Giovanni

unread,
Jan 11, 2011, 12:31:58 PM1/11/11
to web.py
In my website I'm using a base template that contains most of the
formatting.

render = web.template.render(basedir + 'templates/', base='layout',
globals=globals_vars_custom)

in "layout.html" I have something like:

$def with (content)
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
"http://www.w3.org/TR/REC-html40/loose.dtd">
<html>
<head>
<title>PAGE TITLE</title>
<!-- Some CSS and some javascript -->
</head>
<body>
$:content
</body>
</html>


This base template works fine for the 90% of my website, but I have a
page in which I need to insert some other data inside the `<head>`
(some meta tags).

How can I do this? Is it possible to put the "<head>" inside a
structure that I can easily override inside a template?

Thanks,
Giovanni

andrei

unread,
Jan 13, 2011, 3:14:19 AM1/13/11
to we...@googlegroups.com
You can define vars in inner template, and get their values in base, like this:

page.html:

$def with(title)
$var title: $title

base.html:

$def with(page)
<title>$page.get("title","Default title")</title>
$:page


DavidA

unread,
Jan 13, 2011, 7:55:23 AM1/13/11
to web.py
I use this snippet in my base template. I think I picked it up from an
old post in this group:

<head>
...
$if hasattr(content, 'stylesheets'):
$for f in content.stylesheets.split():
<link rel="stylesheet" href="$f" type="text/css"/>
...
</head>

And then in a contained template:

$var stylesheets: /static/css/custom.css /static/css/print.css

Giovanni

unread,
Jan 13, 2011, 10:35:28 AM1/13/11
to web.py


On Jan 13, 3:14 am, andrei <Andre...@gmail.com> wrote:
Thank you very much!
Reply all
Reply to author
Forward
0 new messages