def test_without_substituting_gettext_with_lambda_extending_file(self): # this should use i18n.gettext loader = FileLoader(path=os.path.join(os.path.dirname(__file__), 'data')) tpl = loader.import_('file_child.html') rsp = tpl(dict()).render() assert rsp == '''<div>parent</div><div>child</div>''', rsp
def test_substituting_gettext_with_lambda_extending_file(self): loader = FileLoader(path=os.path.join(os.path.dirname(__file__), 'data'), base_globals=dict(gettext=lambda x: 'egg')) tpl = loader.import_('file_child.html') rsp = tpl(dict()).render() assert rsp == '''<div>egg</div><div>egg</div>''', rsp
def test_without_substituting_gettext_with_lambda_extending_file(self): # this should use i18n.gettext loader = FileLoader( path=os.path.join(os.path.dirname(__file__), "data")) tpl = loader.import_("file_child.html") rsp = tpl(dict()).render() assert rsp == """<div>parent</div><div>child</div>""", rsp
def test_substituting_gettext_with_lambda_extending_file(self): loader = FileLoader( path=os.path.join(os.path.dirname(__file__), "data"), base_globals=dict(gettext=lambda x: "egg"), ) tpl = loader.import_("file_child.html") rsp = tpl(dict()).render() assert rsp == """<div>egg</div><div>egg</div>""", rsp
def kajiki(dirname, verbose=False): from kajiki import FileLoader loader = FileLoader(base=dirname) template = loader.load('template.html') def render(): data = dict(title='Just a test', user='******', items=['Number %d' % num for num in range(1, 15)]) return template(data).render() if verbose: print render() return render
def test_debug(self): loader = FileLoader(base=os.path.join(os.path.dirname(__file__), 'data')) tpl = loader.import_('debug.html') try: tpl().render() assert False, 'Should have raised ValueError' except ValueError: exc_info = sys.exc_info() stack = traceback.extract_tb(exc_info[2]) # Verify we have stack trace entries in the template for fn, lno, func, line in stack: if fn.endswith('debug.html'): break else: assert False, 'Stacktrace is all python'
def kajiki(dirname, verbose=False): from kajiki import FileLoader loader = FileLoader(base=dirname) template = loader.load('template.html') def render(): data = dict(title='Just a test', user='******', items=['Number %d' % num for num in range(1, 15)]) return template(data).render() if verbose: print(render()) return render
def test_debug(self): loader = FileLoader( path=os.path.join(os.path.dirname(__file__), 'data')) tpl = loader.import_('debug.html') try: tpl().render() assert False, 'Should have raised ValueError' except ValueError: exc_info = sys.exc_info() stack = traceback.extract_tb(exc_info[2]) # Verify we have stack trace entries in the template for fn, lno, func, line in stack: if fn.endswith('debug.html'): break else: assert False, 'Stacktrace is all python'
def test_debug(self): loader = FileLoader(path=os.path.join(os.path.dirname(__file__), "data")) tpl = loader.import_("debug.txt") try: tpl().render() except ValueError: exc_info = sys.exc_info() stack = traceback.extract_tb(exc_info[2]) else: assert False, "Should have raised ValueError" # Verify we have stack trace entries in the template for fn, lno, func, line in stack: if fn.endswith("debug.txt"): break else: assert False, "Stacktrace is all python"
def template_loader(self): """A :class:`kajiki.loader.template.Loader` that loads templates from the same places as Flask. """ path = os.path.join(self.app.root_path, self.app.template_folder) return FileLoader( path, # TODO #auto_reload=self.app.debug, #callback=self.callback, )
def test_code_error(self): for strip_text in (False, True): try: child = FileLoader( os.path.join(os.path.dirname(__file__), "data")).load("error.html", strip_text=strip_text) child().render() except ZeroDivisionError: exn_info = traceback.format_exception(*sys.exc_info()) last_line = exn_info[-2] assert "${3/0}" in last_line, last_line else: assert False
def get_login_ui(self): """ Returns XHTML containing a login form. class members used: login_fields_path: String, contains the path to the file that contains the XHTML. Args: None. Returns: An XHTML fragment containing the login form. """ # Empty quotes need to be there to specify that this loader searches # from the program root loader = FileLoader('') loader.extension_map['xhtml'] = XMLTemplate page_template = loader.load(self.login_fields_path) page_fragment = page_template({ 'post_location': UserManagementInterfaceBase.LOGIN_UI_POST_LOCATION, }) return page_fragment.render()
def test_code_error(self): for strip_text in (False, True): try: child = FileLoader( os.path.join(os.path.dirname(__file__), 'data')).load('error.html', strip_text=strip_text) child().render() except ZeroDivisionError as exc: import traceback, sys l = traceback.format_exception(*sys.exc_info()) last_line = l[-2] assert '${3/0}' in last_line, last_line else: assert False
def renderer_factory(info): '''*info* contains:: name = Attribute('The value passed by the user as the renderer name') package = Attribute('The "current package" when the renderer ' 'configuration statement was found') type = Attribute('The renderer type name') registry = Attribute('The "current" application registry when the ' 'renderer was created') settings = Attribute('The ISettings dictionary related to the ' 'current app') ''' registry = info.registry settings = info.settings if not hasattr(registry, 'kajiki_loader'): from kajiki import FileLoader registry.kajiki_loader = FileLoader( base=abspath_from_resource_spec(settings['kajiki.directory']), reload=asbool(settings.get('reload_templates')), force_mode=asbool(settings.get('kajiki.force_mode')), autoescape_text=asbool(settings.get('kajiki.autoescape')), ) return KajikiTemplateRenderer(info)
""" Defines the Kajiki View Decorator """ import functools from kajiki import FileLoader, XMLTemplate loader = FileLoader('template') loader.extension_map['xhtml'] = XMLTemplate def kajiki_view(template_name): """ Defines a kajiki_view decorator When a function is annotated with this decorator, if the function returns a dict, those values will be passed into the specified template and the rendered template will become the output of the function. Notes: Used code example from here: https://buxty.com/b/2013/12/jinja2-templates-and-bottle/ but customized for Kajiki instead Args: template_name: the name of the xhtml file to use as the template Returns: decorator: the decorated function """ def decorator(view_func): @functools.wraps(view_func)
#! /usr/bin/env python3 """ Defines the Kajiki View Decorator """ import functools from showandtell import db, model from showandtell.helpers import util from kajiki import FileLoader, XMLTemplate loader = FileLoader('showandtell/template') loader.extension_map['xhtml'] = XMLTemplate def kajiki_view(template_name): """ Defines the kajiki_view decorator Used code example from here: https://buxty.com/b/2013/12/jinja2-templates-and-bottle/ but customized for kajiki instead """ def decorator(view_func): @functools.wraps(view_func) def wrapper(*args, **kwargs): response = view_func(*args, **kwargs) if isinstance(response, dict): # If the decorated function returns a dictionary, throw that to # the template