I have to include a dynamic page content into my template, Say I have a left panel which gets the data dynamically through a view. Now, I have to include this left panel into all my pages but I do not want to duplicate the code for all the pages. Is there any way, I can write a single script and include it in all my templa开发者_运维百科tes to display the left panel in all my pages?
Thanks in advance.
What you trying to achieve is directly supported in pretty much all template languages I know of. I would strongly recommend using one the many good choices for Python:
- Genshi
- Kid
- Jinja
- Mako
If you were using Genshi for example (the default template language in TurboGears web application framework) what you want to do would look something like this:
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:py="http://genshi.edgewall.org/"
xmlns:xi="http://www.w3.org/2001/XInclude"
py:strip="">
<xi:include href="header.html" />
<xi:include href="sidebar.html" />
<xi:include href="footer.html" />
<!-- The rest of your page goes here -->
<html>
header.html
, sidebar.html
and footer.html
need only be defined once and reused in any other page.
精彩评论