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

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

30th June 2009 Europython 2009, Birmingham, UK 1

Zope Page Templates

An introduction to Zope Page Templates and their use outside of Zope

Matt [email protected]

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

30th June 2009 Europython 2009, Birmingham, UK 2

Who Am I

● Technical Director of Netsight→ Web development fi rm in Bristol, UK

● 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

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

30th June 2009 Europython 2009, Birmingham, UK 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

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

30th June 2009 Europython 2009, Birmingham, UK 4

What is ZPT/TAL

TAL – Template Attribute LanguageZPT – Zope Page Templates

ZPT = an implementation of TAL

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

30th June 2009 Europython 2009, Birmingham, UK 5

Zope?! I don't DO Zope!

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

30th June 2009 Europython 2009, Birmingham, UK 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

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

30th June 2009 Europython 2009, Birmingham, UK 7

The Idea of Templating

Data XML/TALTemplate

ZPT

XHTML

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

30th June 2009 Europython 2009, Birmingham, UK 8

The Idea of Templating

Data XML/TALTemplate

ZPT

XHTML XML RSSPlaintext

or or not

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

30th June 2009 Europython 2009, Birmingham, UK 9

Why use templates?

●Separate Presentation and Logic●Keeping code clean●Multiple presentations of same data (RSS, JSON, REST, XML)

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

30th June 2009 Europython 2009, Birmingham, UK 10

But, why TAL?

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

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

30th June 2009 Europython 2009, Birmingham, UK 11

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

<ul> <li tal:repeat=”name row” tal:content=”name” /> </ul>

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

30th June 2009 Europython 2009, Birmingham, UK 12

But, why TAL?

<ul> <li tal:repeat=”name row” tal:content=”name”> Dummy data </li> </ul>

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

30th June 2009 Europython 2009, Birmingham, UK 13

But, why TAL?

<ul> <li tal:repeat=”name row” tal:content=”name”> Dummy data </li> </ul>

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

30th June 2009 Europython 2009, Birmingham, UK 14

Makes well-formed XHTML easy

<ul> <li> % if foo = 'bar': ${name} </li> % endif </ul>

Nesting Error!

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

30th June 2009 Europython 2009, Birmingham, UK 15

Makes well-formed XHTML easy

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

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

30th June 2009 Europython 2009, Birmingham, UK 16

Makes well-formed XHTML easy

• Ensures that you close all elements and quote attributes

• Escapes all ampersands by default & -> &amp;

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

30th June 2009 Europython 2009, Birmingham, UK 17

So, how do I use it?

Create virtualenv

% virtualenv zptdemo

Install zope.pagetemplate in virtualenv

% cd zptdemo% bin/easy_install zope.pagetemplate

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

30th June 2009 Europython 2009, Birmingham, UK 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)

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

30th June 2009 Europython 2009, Birmingham, UK 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>

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

30th June 2009 Europython 2009, Birmingham, UK 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>

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

30th June 2009 Europython 2009, Birmingham, UK 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

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

30th June 2009 Europython 2009, Birmingham, UK 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

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

30th June 2009 Europython 2009, Birmingham, UK 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

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

30th June 2009 Europython 2009, Birmingham, UK 24

Some advanced features

( but not too many )

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

30th June 2009 Europython 2009, Birmingham, UK 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>

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

30th June 2009 Europython 2009, Birmingham, UK 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>

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

30th June 2009 Europython 2009, Birmingham, UK 27

METAL macros

Internationalisation:

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

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

30th June 2009 Europython 2009, Birmingham, UK 28

Questions?

[email protected]

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

30th June 2009 Europython 2009, Birmingham, UK 29

We are looking for Developers!

Come chat to me or drop an email to

[email protected]