def error(self, error): """Returns a simple error page with debugging informations. """ dirname = os.path.dirname(__file__) filename_default = os.path.abspath(os.path.join(dirname, "error.pt")) fileObj = open(filename_default, "r") template = u"%s" % fileObj.read() fileObj.close() pt = PageTemplate(template) return pt.render(context=self.context, error=error)
def __init__(self, id, content="", alt="", permission=None, attrs=None, **rcontext): self.id = id self.attrs = attrs or {} self.permission = permission self.rcontext = rcontext if 'id' not in self.attrs: self.attrs['id'] = repr(id) self.update() attributes = u';'.join([u'%s %s' % v for v in self.attrs.items()]) rcontext.update(attrs=self.attrs, attributes=attributes, id=id) body = self.body % self.rcontext rcontext.update(content=content, alt=alt) self.template = PageTemplate(body)
class Action(object): """A model action is used to add some action in model views. The content and alt parameters should be a :py:class:`pyramid.i18n.TranslationString`:: >>> from webob import Request >>> request = Request.blank('/') >>> class MyAction(Action): ... body = u'<a tal:attributes="%(attributes)s">${content}</a>' >>> action = MyAction('myaction', content=_('Click here'), ... attrs={'href': repr('#'), 'onclick': repr('$.click()')}) >>> action.render(request) u'<a href="#" id="myaction" onclick="$.click()">Click here</a>' """ def __init__(self, id, content="", alt="", permission=None, attrs=None, **rcontext): self.id = id self.attrs = attrs or {} self.permission = permission self.rcontext = rcontext if 'id' not in self.attrs: self.attrs['id'] = repr(id) self.update() attributes = u';'.join([u'%s %s' % v for v in self.attrs.items()]) rcontext.update(attrs=self.attrs, attributes=attributes, id=id) body = self.body % self.rcontext rcontext.update(content=content, alt=alt) self.template = PageTemplate(body) def update(self): pass def render(self, request): rcontext = {'action': self, 'request': request} rcontext.update(self.rcontext) localizer = get_localizer(request) mapping = getattr(request, 'action_mapping', {}) if not mapping: for k in ('model_name', 'model_label', 'model_id'): mapping[k] = localizer.translate(getattr(request, k, '')) request.action_mapping = mapping for k in ('content', 'alt'): v = rcontext[k] if isinstance(v, TranslationString): v = TranslationString(v, domain=v.domain, mapping=request.action_mapping) rcontext[k] = localizer.translate(v) return self.template.render(**rcontext) def __repr__(self): return '<%s %s>' % (self.__class__.__name__, self.id)
def __init__(self, id, content="", alt="", attrs=None, **rcontext): self.id = id self.attrs = attrs or {} self.rcontext = rcontext if 'id' not in self.attrs: self.attrs['id'] = repr(id) self.update() attributes = u';'.join([u'%s %s' % v for v in self.attrs.items()]) rcontext.update(attrs=self.attrs, attributes=attributes, id=id) body = self.body % self.rcontext rcontext.update(content=content, alt=alt) self.template = PageTemplate(body)
def __call__(self): """Returns the PDF printout. """ context = aq_inner(self.context) body = context.getRawText() # Check there is a template if self.default_template == "": IStatusMessage(self.request).addStatusMessage( _("Template is empty. Check your Printout control panel."), type="info" ) return self.request.response.redirect(context.absolute_url()) # Render Chameleon Template plone_portal_state = getMultiAdapter((context, self.request), name="plone_portal_state") plone_tools = getMultiAdapter((context, self.request), name="plone_tools") plone_context_state = getMultiAdapter((context, self.request), name="plone_portal_state") try: pt = PageTemplate(self.default_template, format="xml", encoding="utf-8", debug=True) cooked_body = cook_body(body, self.default_body_stylesheet) output = pt.render( context=context, plone_portal_state=plone_portal_state, plone_tools=plone_tools, plone_context_state=plone_context_state, body=cooked_body, ) except ExpatError, e: error = { "title": "Error rendering the default (chameleon/zpt) template", "message": str(e), "fo_output": "", "template": escape(self.default_template), "stylesheet": escape(self.default_body_stylesheet), "body": "", "cooked_body": "", } return self.error(error)
def dump_peopledir(peopledir): old_style = getattr(peopledir, 'categories', None) is not None template = PageTemplate(body=_DUMP_XML) if old_style: categories = peopledir.categories else: categories = peopledir['categories'] return template(peopledir=peopledir, categories=categories, sections=[(x, peopledir[x]) for x in peopledir.order], is_column=IPeopleSectionColumn.providedBy, is_group=IPeopleReportGroup.providedBy, is_report=IPeopleReport.providedBy, is_redirector=IPeopleRedirector.providedBy, category_items=_xxx_category_items, acl_info=_acl_info, column_info=_xxx_column_info, report_items=_xxx_report_items, report_filter_items=_xxx_report_filter_items, )
def get_chameleon_tests(): if not chameleon: return [] from chameleon.zpt.template import PageTemplate chameleon_template = PageTemplate("""\ <table> <tr tal:repeat="row table"> <i tal:omit-tag="" tal:repeat="key row"> <td>${key}</td><td>${row[key]}</td> </i> </tr> </table> """) ctx = {'table': TABLE_DATA} def test_chameleon(): """chameleon""" return chameleon_template.render(**ctx) return [ test_chameleon, ]
def test_django(): return django_template.render(Context(ctx)) # region: chameleon try: from chameleon.zpt.template import PageTemplate except ImportError: test_chameleon = None else: chameleon_template = PageTemplate( s("""\ <table> <tr tal:repeat="row table"> <i tal:omit-tag="" tal:repeat="key row"> <td>${key}</td><td>${row[key]}</td> </i> </tr> </table> """)) def test_chameleon(): return chameleon_template.render(**ctx) # region: cheetah try: from Cheetah.Template import Template except ImportError: test_cheetah = None
try: from chameleon.zpt.template import PageTemplate except ImportError: test_chameleon = None else: chameleon_template = PageTemplate("""\ <html xmlns:tal="http://xml.zope.org/namespaces/tal"> <head> <title tal:content="page_title">Page Title</title> </head> <body> <div class="header"> <h1 tal:content="page_title">Page Title</h1> </div> <ul class="navigation"> <li tal:repeat="item sections"><a tal:attributes="href item[0]" tal:content="item[1]">caption</a></li> </ul> <div class="table"> <table> <tr tal:repeat="row table"> <td tal:repeat="cell row" tal:content="row[cell]">cell</td> </tr> </table> </div> </body> </html>\ """) chameleon_context = dict(context) chameleon_context['sections'] = [ ('index.html', 'Index'), ('downloads.html', 'Downloads'), ('products.html', 'Products')
def setFromString(self, string): self._filename = None self._template = PageTemplate(string)
file.close() # input.html file = open("input.html", "r") body = file.read() file.close() class Context: pass context = Context() context.Title = "This is a title" context.Description = "Description" pt = PageTemplate(default_template) output = pt.render(context=context, body=cook_body(body, default_body_stylesheet)) fo_filename = "tmp.fo" file = open(fo_filename, "w") file.write(output) file.close() fop = Fop() output_filename = fop.convert(fo_filename, output_filename="out.pdf") print output_filename
from prambanan.zpt import TemplateRegistry import time dir = os.path.dirname(os.path.realpath(__file__)) zpt_dir = os.path.join(dir, "template") gen_dir = os.path.join(zpt_dir, "gen") #init prambanan registry = TemplateRegistry() registry.register_py("prambanan", "test/template/template.pt") pt = registry.get("prambanan", "test/template/template") #init chameleon file = pkg_resources.resource_filename("prambanan", "test/template/template.pt") cham = PageTemplate(open(file).read()) def render_prambanan(i): el = etree.Element("div") el = pt.render(el, a="lalal", b=["c", "d"], c=True, width=i, anu="4000") return etree.tostring(el) def render_cham(i): return cham.render( a="lalal", b=["c", "d"], c=True, width=i, anu="4000") print render_prambanan(23) print render_cham(23) start = time.time() for i in xrange(0, 10000): s = render_cham(i)