def _convert_to_python(self, value, state): try: kajiki.XMLTemplate(value) except Exception as e: print(e) raise formencode.Invalid(_('Template not valid.'), value, state) return value
def get_render_callable(engine_name, displays_on, src, filename=None): """ Returns a function that takes a template source and kwargs. """ # See the discussion here re: `displays_on` -- http://bit.ly/JRqbRw directory = None if filename: if SEP not in filename and (not ALTSEP or ALTSEP not in filename): filename = _get_dotted_filename(engine_name, filename) directory = os.path.dirname(filename) if engine_name == 'mako': import mako.template args = dict(text=src, imports=["from markupsafe import escape_silent"], default_filters=['escape_silent']) if filename: args['filename'] = relpath(filename, directory) from mako.lookup import TemplateLookup args['lookup'] = TemplateLookup(directories=[directory]) tmpl = mako.template.Template(**args) return lambda kwargs: Markup(tmpl.render(**kwargs)) elif engine_name in ('genshi', 'genshi_abs'): import genshi.template args = dict(source=src, ) if filename: args['loader'] = genshi.template.TemplateLoader([ genshi.template.loader.directory(directory), ]) tmpl = genshi.template.MarkupTemplate(**args) return lambda kwargs: Markup(''.join( tmpl.generate(**kwargs).serialize('xhtml'))) elif engine_name == 'jinja': import jinja2 from jinja_util import htmlbools env = jinja2.environment.Environment(autoescape=True) env.filters['htmlbools'] = htmlbools tmpl = env.from_string(src, template_class=jinja2.Template) tmpl.filename = filename return lambda kwargs: Markup(tmpl.render(**kwargs)) elif engine_name == 'kajiki': import kajiki tmpl = kajiki.XMLTemplate(src, filename=filename) return lambda kwargs: Markup(tmpl(kwargs).render()) elif engine_name == 'chameleon': import chameleon tmpl = chameleon.PageTemplate(src, filename=filename) return lambda kwargs: Markup(tmpl.render(**kwargs).strip()) raise NotImplementedError("Unhandled engine")
def send_email(recipients, sender, mail_model_name, translation=None, data=None, send_async=False): """ Method for sending email in this pluggable. Use this method to send your email, specifying the name of a MailModel and the language of the email (optionally). E.g. send_email('*****@*****.**', '*****@*****.**', 'registration_mail', 'EN') If a language is not specified, the default language passed as Plugin option will be used. :param recipients: An array representing the email address of the recipient of the email :param sender: A string representing the email adress of the sender of the email :param mail_model_name: The name of the MailModel representing an email :param translation: The language of a TemplateTranslation (e.g. 'EN'). If omitted, the default language provided while plugging mailtemplates is used :param data: A dictionary representing the variables used in the email template, like ${name} :param send_async: The email will sent asynchronously if this flag is True """ if 'kajiki' not in config['render_functions']: raise MailTemplatesError('Kajiki must be allowed in your app.') __, mail_models = model.provider.query(model.MailModel, filters=dict(name=mail_model_name)) if not mail_models: raise MailTemplatesError("Mail model '%s' not found." % mail_model_name) mail_model = mail_models[0] language = translation or config['_mailtemplates']['default_language'] __, translations = model.provider.query(model.TemplateTranslation, filters=dict(mail_model=mail_model, language=language)) if not translations: raise MailTemplatesError('Translation for this mail model not found') tr = translations[0] # as the template is already translated, use a fake gettext data.update({'gettext': lambda x: x}) Template = kajiki.XMLTemplate(source=tr.body) Template.loader = tg.config['render_functions']['kajiki'].loader html = Template(data).render() Template = kajiki.TextTemplate(tr.subject) subject = Template(data).render() _send_email(sender, recipients, subject, html, send_async)
def send_test_email(self, **kwargs): if 'kajiki' not in config['render_functions']: raise MailTemplatesError('Kajiki must be allowed in your app.') body = kwargs.get('body') subject = kwargs.get('subject').replace('$', '$$') Template = kj.XMLTemplate(body) Template.loader = tg.config['render_functions']['kajiki'].loader t = Template() t.__kj__.collect = FakeCollect(t) for v in _get_variables_for_template(t): t.__globals__[v] = TemplateFiller(v) html = t.render() _send_email(recipients=[kwargs.get('email')], sender=tg.config.get('mail.username', '*****@*****.**'), subject=subject, html=html) tg.flash(_('Test email sent to %s' % kwargs.get('email'))) return dict(form=EditTranslationForm, values=kwargs)
def _actions(filler, row): template = ''' <div> <a href="${id}/edit" class="btn btn-primary"> <span class="glyphicon glyphicon-pencil"></span> </a> <div class="hidden-lg hidden-md"> </div> <form method="POST" action="${id}" style="display: inline"> <input type="hidden" name="_method" value="DELETE" /> <button type="submit" class="btn btn-danger" onclick="return confirm('${msg}')"> <span class="glyphicon glyphicon-trash"></span> </button> </form> <py:if test="image == None"> <button class="btn btn-primary" ngf-select="vm.upload($$files, ${id})" ngf-multiple="false"> <span class="glyphicon glyphicon-plus"></span> </button> </py:if><py:else> <button class="btn btn-primary" ngf-select="vm.upload($$files, ${id})" ngf-multiple="false"> <span class="glyphicon glyphicon-edit"></span> </button> <button class="btn btn-danger" ng-click="vm.delete(${id})"> <span class="glyphicon glyphicon-trash"></span> </button> </py:else> </div> ''' Template = kajiki.XMLTemplate(template) return Template( dict(id=row.id, image=row.image, msg=l_('Are you sure?'))).render()
import tg from tg import AppConfig from tg import TGController from tg import expose import kajiki page = kajiki.XMLTemplate(u'''<html> <head></head> <body> <div id="isomor"></div> <script py:for="m in g.webassets['bundle.js'].urls()" src="$m"> </script> <script> ReactDOM.render( React.createElement(HelloWorld.HelloWorld, { name: "World" }), document.getElementById('isomor') ); </script> </body> </html> ''', mode='html5') class RootController(TGController): @expose() def index(self): return page(dict( g=tg.app_globals
mail_model = mail_models[0] language = translation or config['_mailtemplates']['default_language'] __, translations = model.provider.query( model.TemplateTranslation, filters=dict(mail_model=mail_model, language=language) ) if not translations: raise MailTemplatesError('Translation for this mail model not found') tr = translations[0] # as the template is already translated, use a fake gettext data.update({'gettext': lambda x: x}) Template = kajiki.XMLTemplate(source=tr.body) Template.loader = tg.config['render_functions']['kajiki'].loader html = Template(data).render() Template = kajiki.TextTemplate(tr.subject) subject = Template(data).render() _send_email(sender, recipients, subject, html, async) def _get_request(): """Tests fails and can't mock get_mailer, so mock this""" return tg.request def _send_email(sender, recipients, subject, html, async=True):
from django.template import Template as DjangoTemplate except ImportError: DjangoContext = DjangoTemplate = None try: from mako.template import Template as MakoTemplate except ImportError: MakoTemplate = None table = [dict(a=1, b=2, c=3, d=4, e=5, f=6, g=7, h=8, i=9, j=10) for x in range(1000)] kajiki_tmpl = kajiki.XMLTemplate(source=""" <table> <tr py:for="row in table"> <td py:for="c in row.values()" py:content="c"/> </tr> </table> """) genshi_tmpl = MarkupTemplate(""" <table xmlns:py="http://genshi.edgewall.org/"> <tr py:for="row in table"> <td py:for="c in row.values()" py:content="c"/> </tr> </table> """) genshi_tmpl2 = MarkupTemplate(""" <table xmlns:py="http://genshi.edgewall.org/">$table</table> """)