Zope Page Templates
                     An introduction to Zope Page Templates
                     and their use outside...
Who Am I
 ●   Technical Director of Netsight
      →   Web development f rm in Bristol, UK
                          i



...
The Irony of this talk...

   Most of it was taken from a talk
             on PHPTAL!

                 Thanks to Kornel ...
What is ZPT/TAL


                 TAL – Template Attribute Language
                   ZPT – Zope Page Templates



     ...
Zope?! I don't DO Zope!




30th June 2009   Europython 2009, Birmingham, UK   5
TAL is a standard (sort of)


 Of cial speci cation
 http://wiki.zope.org/ZPT/TAL


 Multiple implementations
 http://en.w...
The Idea of Templating

                                              XML/TAL
                 Data
                      ...
The Idea of Templating

                                 XML/TAL
          Data
                                 Template
...
Why use templates?

      ● Separate Presentation and Logic
      ● Keeping code clean


      ● Multiple presentations of...
But, why TAL?


      <ul>
        % for name in row:
            <li>${name}</li>
        % endfor
      </ul>



30th Ju...
But, why TAL?
     <ul>
       % for name in row:
           <li>${name}</li>
       % endfor
    </ul>




      <ul>
   ...
But, why TAL?


      <ul>
        <li tal:repeat=”name row”
            tal:content=”name”>
        Dummy data
        </...
But, why TAL?


      <ul>
        <li tal:repeat=”name row”
            tal:content=”name”>
        Dummy data
        </...
Makes well-formed XHTML
easy

      <ul>
        <li>
        % if foo = 'bar':
             ${name}
        </li>        ...
Makes well-formed XHTML
easy


  <ul>
    <li
tal:condition=”python:foo='bar'”
tal:content=”name” />
  </ul>



30th June ...
Makes well-formed XHTML
easy


           • Ensures that you close all elements
             and quote attributes
        ...
So, how do I use it?


Create virtualenv
     % virtualenv zptdemo

Install zope.pagetemplate in virtualenv
     % cd zptd...
So, how do I use it?

In mycode.py:
from zope.pagetemplate.pagetemplatefile 
    import PageTemplateFile

my_pt = PageTemp...
So, how do I use it?

In mytemplate.py:
<html>
<body>
<h1>Hello World</h1>

<div tal:condition=”python:foo == 'bar'”>
<ul>...
So, how do I use it?
End result:
<html>
<body>
<h1>Hello World</h1>

<ul>
<li>apple</li>
<li>banana</li>
<li>carrot</li>
<...
Some TAL niceties



<a href=”href” tal:omit-tag=”not:href”>
   Optionally linked text
</a>


                 Omit the ta...
Some TAL niceties



<title tal:content=”page/title |
               site/title | default”>
  My Website
</title>

       ...
Some TAL niceties



<option tal:repeat=”c countries”
        tal:content=”c”
        tal:attributes=”selected
           ...
Some advanced features




                 ( but not too many )

30th June 2009     Europython 2009, Birmingham, UK   24
METAL macros
A master template:
<html metal:define-macro=”main”>
  <head><title>My Site</title></head>
  <body>
    <div m...
METAL macros
A sub-template for a page:
<html metal:use-macro=
              ”template/macros/main”>
  <head><title>My Sit...
METAL macros



Internationalisation:
<h1 i18n:translate=””>Some text</h1>




30th June 2009    Europython 2009, Birmingh...
Questions?
                   matth@netsight.co.uk




30th June 2009   Europython 2009, Birmingham, UK   28
We are looking for Developers!
Come chat to me
or
drop an email to

 careers@netsight.co.uk




30th June 2009     Europyt...
Upcoming SlideShare
Loading in...5
×

An introduction to Zope Page Templates and their use outside of Zope (+Audio)

2,697

Published on

Zope Page Templates have been around for a while, and used extensively in Zope and many Zope based apps and frameworks, but did you know you can use ZPT with any Python project? Indeed there are implementations of the syntax used, Template Attribute Language (TAL), for other languages too. Making it one of the most portable, cross platform templating languages there is. Find out why ZPT and TAL are so elegant, and how to use them with your Python project.

I will cover why TAL is a great choice for templating, the simple syntax of TAL and how to create and render page template objects in your code.

Published in: Technology, Art & Photos
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
2,697
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
28
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

An introduction to Zope Page Templates and their use outside of Zope (+Audio)

  1. 1. Zope Page Templates An introduction to Zope Page Templates and their use outside of Zope Matt Hamilton matth@netsight.co.uk 30th June 2009 Europython 2009, Birmingham, UK 1
  2. 2. Who Am I ● Technical Director of Netsight → Web development f rm in Bristol, UK i ● 10 years experience with Zope/Plone ● More of an integrator than core developer → I get involved in all those sticky projects of merging Plone in with other systems in an enterprise 30th June 2009 Europython 2009, Birmingham, UK 2
  3. 3. The Irony of this talk... Most of it was taken from a talk on PHPTAL! Thanks to Kornel Lesiński who did a talk on it at the Web Standards conference 2008 in London 30th June 2009 Europython 2009, Birmingham, UK 3
  4. 4. What is ZPT/TAL TAL – Template Attribute Language ZPT – Zope Page Templates ZPT = an implementation of TAL 30th June 2009 Europython 2009, Birmingham, UK 4
  5. 5. Zope?! I don't DO Zope! 30th June 2009 Europython 2009, Birmingham, UK 5
  6. 6. TAL is a standard (sort of) Of cial speci cation http://wiki.zope.org/ZPT/TAL Multiple implementations http://en.wikipedia.org/wiki/Template_Attribute_Language 30th June 2009 Europython 2009, Birmingham, UK 6
  7. 7. The Idea of Templating XML/TAL Data Template ZPT XHTML 30th June 2009 Europython 2009, Birmingham, UK 7
  8. 8. The Idea of Templating XML/TAL Data Template ZPT or or not Plain XHTML XML RSS text 30th June 2009 Europython 2009, Birmingham, UK 8
  9. 9. Why use templates? ● Separate Presentation and Logic ● Keeping code clean ● Multiple presentations of same data (RSS, JSON, REST, XML) 30th June 2009 Europython 2009, Birmingham, UK 9
  10. 10. But, why TAL? <ul> % for name in row: <li>${name}</li> % endfor </ul> 30th June 2009 Europython 2009, Birmingham, UK 10
  11. 11. But, why TAL? <ul> % for name in row: <li>${name}</li> % endfor </ul> <ul> <li tal:repeat=”name row” tal:content=”name” /> </ul> 30th June 2009 Europython 2009, Birmingham, UK 11
  12. 12. But, why TAL? <ul> <li tal:repeat=”name row” tal:content=”name”> Dummy data </li> </ul> 30th June 2009 Europython 2009, Birmingham, UK 12
  13. 13. But, why TAL? <ul> <li tal:repeat=”name row” tal:content=”name”> Dummy data </li> </ul> 30th June 2009 Europython 2009, Birmingham, UK 13
  14. 14. Makes well-formed XHTML easy <ul> <li> % if foo = 'bar': ${name} </li> Nesting % endif </ul> Error! 30th June 2009 Europython 2009, Birmingham, UK 14
  15. 15. Makes well-formed XHTML easy <ul> <li tal:condition=”python:foo='bar'” tal:content=”name” /> </ul> 30th June 2009 Europython 2009, Birmingham, UK 15
  16. 16. Makes well-formed XHTML easy • Ensures that you close all elements and quote attributes • Escapes all ampersands by default & -> &amp; 30th June 2009 Europython 2009, Birmingham, UK 16
  17. 17. So, how do I use it? Create virtualenv % virtualenv zptdemo Install zope.pagetemplate in virtualenv % cd zptdemo % bin/easy_install zope.pagetemplate 30th June 2009 Europython 2009, Birmingham, UK 17
  18. 18. So, how do I use it? In mycode.py: from zope.pagetemplate.pagetemplatefile import PageTemplateFile my_pt = PageTemplateFile('mytemplate.pt') context = {'row': ['apple', 'banana', 'carrot'], 'foo':'bar'} print my_pt.pt_render(namespace=context) 30th June 2009 Europython 2009, Birmingham, UK 18
  19. 19. So, how do I use it? In mytemplate.py: <html> <body> <h1>Hello World</h1> <div tal:condition=”python:foo == 'bar'”> <ul> <li tal:repeat="item rows" tal:content="item" /> </ul> </div> </body> </html> 30th June 2009 Europython 2009, Birmingham, UK 19
  20. 20. So, how do I use it? End result: <html> <body> <h1>Hello World</h1> <ul> <li>apple</li> <li>banana</li> <li>carrot</li> </ul> </div> </body> </html> 30th June 2009 Europython 2009, Birmingham, UK 20
  21. 21. Some TAL niceties <a href=”href” tal:omit-tag=”not:href”> Optionally linked text </a> Omit the tag if there is href variable evaluates false 30th June 2009 Europython 2009, Birmingham, UK 21
  22. 22. Some TAL niceties <title tal:content=”page/title | site/title | default”> My Website </title> If there is no page title or site title, then use the default text 30th June 2009 Europython 2009, Birmingham, UK 22
  23. 23. Some TAL niceties <option tal:repeat=”c countries” tal:content=”c” tal:attributes=”selected python:c==’UK’” /> Create an option for each country, and if the UK then set selected 30th June 2009 Europython 2009, Birmingham, UK 23
  24. 24. Some advanced features ( but not too many ) 30th June 2009 Europython 2009, Birmingham, UK 24
  25. 25. METAL macros A master template: <html metal:define-macro=”main”> <head><title>My Site</title></head> <body> <div metal:define-slot=”body”> Dummy body </div> </body> </html> 30th June 2009 Europython 2009, Birmingham, UK 25
  26. 26. METAL macros A sub-template for a page: <html metal:use-macro= ”template/macros/main”> <head><title>My Site</title></head> <body> <div metal:fill-slot=”body”> This is my real body text </div> </body> </html> 30th June 2009 Europython 2009, Birmingham, UK 26
  27. 27. METAL macros Internationalisation: <h1 i18n:translate=””>Some text</h1> 30th June 2009 Europython 2009, Birmingham, UK 27
  28. 28. Questions? matth@netsight.co.uk 30th June 2009 Europython 2009, Birmingham, UK 28
  29. 29. We are looking for Developers! Come chat to me or drop an email to careers@netsight.co.uk 30th June 2009 Europython 2009, Birmingham, UK 29
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×