• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

Chapter 34. Implementing TALES Namespaces > Step 2: Implementing the Namespace

Step 2: Implementing the Namespace

The actual code of the namespace is not much harder than that of the interface, if you have played with the user locales before. You just add the following implementation in the package's __init__.py file:

01 from zope.interface import implements
02 from zope.tales.interfaces import ITALESFunctionNamespace
03 from zope.security.proxy import removeSecurityProxy
04 from interfaces import IFormatTalesAPI
05
06
07 class FormatTalesAPI(object):
08
09     implements(IFormatTalesAPI, ITALESFunctionNamespace)
10
11     def __init__(self, context):
12         self.context = context
13
14     def setEngine(self, engine):
15         """See zope.tales.interfaces.ITALESFunctionNamespace"""
16         self.locale = removeSecurityProxy(engine.vars['request']).locale
17
18     def fullDate(self):
19         """See book.formatns.interfaces.IFormatTalesAPI"""
20         return self.locale.dates.getFormatter(
21             'date', 'full').format(self.context)
22
23     def fullTime(self):
24         """See book.formatns.interfaces.IFormatTalesAPI"""
25         return self.locale.dates.getFormatter(
26             'time', 'full').format(self.context)
27
28     def fullDateTime(self):
29         """See book.formatns.interfaces.IFormatTalesAPI"""
30         return self.locale.dates.getFormatter(
31             'dateTime', 'full').format(self.context)


					  


PREVIEW

                                                                          

Not a subscriber?

Start A Free Trial


  
  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint