Welcome, guest | Sign In | My Account | Store | Cart

Notice! PyPM is being replaced with the ActiveState Platform, which enhances PyPM’s build and deploy capabilities. Create your free Platform account to download ActivePython or customize Python with the packages you require and get automatic updates.

Download
ActivePython
INSTALL>
pypm install ice.template

How to install ice.template

  1. Download and install ActivePython
  2. Open Command Prompt
  3. Type pypm install ice.template
 Python 2.7Python 3.2Python 3.3
Windows (32-bit)
0.1.0 Available View build log
Windows (64-bit)
0.1.0 Available View build log
Mac OS X (10.5+)
0.1.0 Available View build log
Linux (32-bit)
0.1.0 Available View build log
Linux (64-bit)
0.1.0 Available View build log
Web
 
License
GPL v.3
Lastest release
version 0.1.0 on Jan 5th, 2011

Persistent Cheetah templates

Features:

  • Cheetah template engine,
  • sources in filesystem,
  • stored in ZODB,
  • editable TTW, simple "$var" Cheetah syntax for non-technical user,
  • able to testing in management web-form,
  • able to reset from source.

Use cases:

  • mail templates,
  • editable HTML snippets and pages,
  • other.
  • Needed imports for these tests *
>>> import os, tempfile
>>> temp_dir = tempfile.mkdtemp()
>>> import zope.component
>>> import ice.template
>>> from zope.configuration import xmlconfig
>>> context = xmlconfig.file('meta.zcml', ice.template)
Create templates
  1. Create source file using Cheetah syntax "$" for the variables:

    System Message: WARNING/2 (<string>, line 38)

    Literal block expected; none found.

>>> file = os.path.join(temp_dir, 'welcome.tmpl')
>>> open(file, 'w').write('''
...     Dear $user_name!
...       Welcome to $site_name!
... ''')
  1. Register template:

    System Message: WARNING/2 (<string>, line 47)

    Literal block expected; none found.

>>> context = xmlconfig.string('''
...     <configure
...           xmlns="http://namespaces.zope.org/zope"
...           xmlns:ice="http://namespaces.zope.org/ice"
...           i18n_domain="test">
...
...           <ice:template
...               name="welcome"
...               title="Welcome"
...               storage="my-templates"
...               source="%s"
...               variables="user_name
...                          site_name"
...               />
...
...       </configure>
...     ''' % file, context=context)
Create storage

Need create (one or more) local utility to store templates. Note, the utility name used for lookup the storage:

>>> templates = ice.template.Templates()
>>> zope.component.provideUtility(
...     templates, ice.template.ITemplates, 'my-templates')
Usage

Use template:

>>> data = {'user_name':u'Bob', 'site_name':'www.gnu.org'}
>>> templates.compileTemplate('welcome', data)
'\n    Dear Bob!\n    Welcome to www.gnu.org!\n'
Manage templates

Looking all templates, registered for this storage:

>>> list(templates.getAllTemplates())

System Message: ERROR/3 (<string>, line 95)

Inconsistent literal block quoting.

[(u'welcome', <ice.template.zcml.Template object at ...>)]

Get variables names:

>>> templates.getVariables('welcome')

System Message: ERROR/3 (<string>, line 100)

Inconsistent literal block quoting.

[u'user_name', u'site_name']

Get template:

>>> templates.getTemplate('welcome')

System Message: ERROR/3 (<string>, line 105)

Inconsistent literal block quoting.

'n Dear $user_name!n Welcome to $site_name!n'

Edit template:

>>> templates.setTemplate('welcome', "$site_name for you, $user_name")
>>> templates.compileTemplate('welcome', data)
'www.gnu.org for you, Bob'

Reset from source:

>>> templates.resetTemplate('welcome')

System Message: ERROR/3 (<string>, line 117)

Inconsistent literal block quoting.

'n Dear $user_name!n Welcome to $site_name!n'

>>> templates.compileTemplate('welcome', data)
'\n    Dear Bob!\n    Welcome to www.gnu.org!\n'
Using property

Another way to use special property:

>>> class Pagelet(object):

System Message: ERROR/3 (<string>, line 129)

Inconsistent literal block quoting.

... ... template = ice.template.PersistentTemplate('my-templates', 'welcome') ... ... def update(self): ... self.data = {'user_name':'Man', 'site_name':'www.python.com.ua'} ... ... def render(self): ... return self.template(self.data)

>>> view = Pagelet()
>>> view.update()
>>> view.render()
'\n    Dear Man!\n    Welcome to www.python.com.ua!\n'
Management UI

Management persistent Cheetah templates UI based on z3c.pagelet layers.

There are 3 views for management templates:

  1. Listing of all storages.
  2. Listing of all templates in a storage.
  3. Edit form for given template, reload, testing, preview.

Actually, you need to do only one: register listing of all storages pagelet using class ice.template.browser.storages.Pagelet (see example in configure.zcml in ice.template.browser module), because (2) and (3) are already registered. For examples, take a look configure.zcml in ice.template.tests module.

CHANGES

Version 0.1.0 (2009-05-04)
  • Initial release, this code is moved from common ice.app library.

Subscribe to package updates

Last updated Jan 5th, 2011

Download Stats

Last month:1

What does the lock icon mean?

Builds marked with a lock icon are only available via PyPM to users with a current ActivePython Business Edition subscription.

Need custom builds or support?

ActivePython Enterprise Edition guarantees priority access to technical support, indemnification, expert consulting and quality-assured language builds.

Plan on re-distributing ActivePython?

Get re-distribution rights and eliminate legal risks with ActivePython OEM Edition.