def test_render_template_with_i18n(self): app = Tipfy( config={ 'tipfyext.jinja2': { 'templates_dir': templates_dir, 'environment_args': dict( autoescape=True, extensions=[ 'jinja2.ext.autoescape', 'jinja2.ext.with_', 'jinja2.ext.i18n' ], ), }, 'tipfy.sessions': { 'secret_key': 'secret', }, }) local.request = Request.from_values() local.request.app = app handler = RequestHandler(local.request) jinja2 = Jinja2(app) message = 'Hello, i18n World!' res = jinja2.render_template(handler, 'template2.html', message=message) self.assertEqual(res, message)
def test_engine_factory(self): def get_jinja2_env(): app = handler.app cfg = app.get_config('tipfyext.jinja2') loader = FileSystemLoader(cfg.get('templates_dir')) return Environment(loader=loader) app = Tipfy( config={ 'tipfyext.jinja2': { 'templates_dir': templates_dir, 'engine_factory': get_jinja2_env, } }) local.request = Request.from_values() local.request.app = app handler = RequestHandler(local.request) jinja2 = Jinja2(app) message = 'Hello, World!' res = jinja2.render_template(handler, 'template1.html', message=message) self.assertEqual(res, message)
def test_engine_factory3(self): app = Tipfy() request = Request.from_values() local.current_handler = handler = RequestHandler(app, request) _globals = {'message': 'Hey there!'} filters = {'ho': lambda e: e + ' Ho!'} jinja2 = Jinja2(app, _globals=_globals, filters=filters) template = jinja2.environment.from_string("""{{ message|ho }}""") self.assertEqual(template.render(), 'Hey there! Ho!')
def test_get_template_attribute(self): app = Tipfy( config={'tipfyext.jinja2': { 'templates_dir': templates_dir }}) request = Request.from_values() local.current_handler = handler = RequestHandler(app, request) jinja2 = Jinja2(app) hello = jinja2.get_template_attribute('hello.html', 'hello') self.assertEqual(hello('World'), 'Hello, World!')
def compile_templates(argv=None): """Compiles templates for better performance. This is a command line script. From the buildout directory, run: bin/jinja2_compile It will compile templates from the directory configured for 'templates_dir' to the one configured for 'templates_compiled_target'. At this time it doesn't accept any arguments. """ if argv is None: argv = sys.argv set_gae_sys_path() from config import config app = Tipfy(config=config) template_path = app.get_config('tipfyext.jinja2', 'templates_dir') compiled_path = app.get_config('tipfyext.jinja2', 'templates_compiled_target') if compiled_path is None: raise ValueError('Missing configuration key to compile templates.') if isinstance(template_path, basestring): # A single path. source = os.path.join(app_path, template_path) else: # A list of paths. source = [os.path.join(app_path, p) for p in template_path] target = os.path.join(app_path, compiled_path) # Set templates dir and deactivate compiled dir to use normal loader to # find the templates to be compiled. app.config['tipfyext.jinja2']['templates_dir'] = source app.config['tipfyext.jinja2']['templates_compiled_target'] = None if target.endswith('.zip'): zip_cfg = 'deflated' else: zip_cfg = None old_list_templates = FileSystemLoader.list_templates FileSystemLoader.list_templates = list_templates env = Jinja2.factory(app, 'jinja2').environment env.compile_templates(target, extensions=None, filter_func=filter_templates, zip=zip_cfg, log_function=logger, ignore_errors=False, py_compile=False) FileSystemLoader.list_templates = old_list_templates
def test_render_template(self): app = Tipfy( config={'tipfyext.jinja2': { 'templates_dir': templates_dir }}) request = Request.from_values() local.current_handler = handler = RequestHandler(app, request) jinja2 = Jinja2(app) message = 'Hello, World!' res = jinja2.render_template(local.current_handler, 'template1.html', message=message) self.assertEqual(res, message)
def test_after_environment_created_using_string(self): app = Tipfy( config={ 'tipfyext.jinja2': { 'after_environment_created': 'resources.jinja2_after_environment_created.after_creation' } }) request = Request.from_values() local.current_handler = handler = RequestHandler(app, request) jinja2 = Jinja2(app) template = jinja2.environment.from_string("""{{ 'Hey'|ho }}""") self.assertEqual(template.render(), 'Hey, Ho!')
def test_after_environment_created(self): def after_creation(environment): environment.filters['ho'] = lambda x: x + ', Ho!' app = Tipfy(config={ 'tipfyext.jinja2': { 'after_environment_created': after_creation } }) request = Request.from_values() local.current_handler = handler = RequestHandler(app, request) jinja2 = Jinja2(app) template = jinja2.environment.from_string("""{{ 'Hey'|ho }}""") self.assertEqual(template.render(), 'Hey, Ho!')
def test_jinja2_mixin_render_template(self): class MyHandler(RequestHandler, Jinja2Mixin): pass app = Tipfy( config={'tipfyext.jinja2': { 'templates_dir': templates_dir }}) local.request = Request.from_values() local.request.app = app handler = MyHandler(local.request) jinja2 = Jinja2(app) message = 'Hello, World!' response = handler.render_template('template1.html', message=message) self.assertEqual(response, message)
def test_render_response(self): app = Tipfy( config={'tipfyext.jinja2': { 'templates_dir': templates_dir }}) request = Request.from_values() local.current_handler = handler = RequestHandler(app, request) jinja2 = Jinja2(app) message = 'Hello, World!' response = jinja2.render_response(local.current_handler, 'template1.html', message=message) self.assertEqual(isinstance(response, Response), True) self.assertEqual(response.mimetype, 'text/html') self.assertEqual(response.data, message)
def test_jinja2_mixin_render_template(self): class MyHandler(RequestHandler, Jinja2Mixin): def __init__(self, app, request): self.app = app self.request = request self.context = {} app = Tipfy( config={'tipfyext.jinja2': { 'templates_dir': templates_dir }}) request = Request.from_values() local.current_handler = handler = MyHandler(app, request) jinja2 = Jinja2(app) message = 'Hello, World!' response = handler.render_template('template1.html', message=message) self.assertEqual(response, message)
def test_render_response_force_compiled(self): app = Tipfy(config={ 'tipfyext.jinja2': { 'templates_compiled_target': templates_compiled_target, 'force_use_compiled': True, } }, debug=False) request = Request.from_values() local.current_handler = handler = RequestHandler(app, request) jinja2 = Jinja2(app) message = 'Hello, World!' response = jinja2.render_response(local.current_handler, 'template1.html', message=message) self.assertEqual(isinstance(response, Response), True) self.assertEqual(response.mimetype, 'text/html') self.assertEqual(response.data, message)
def test_translations(self): app = Tipfy( config={ 'tipfyext.jinja2': { 'environment_args': { 'extensions': [ 'jinja2.ext.i18n', ], }, }, 'tipfy.sessions': { 'secret_key': 'foo', }, }) request = Request.from_values() local.current_handler = handler = RequestHandler(app, request) jinja2 = Jinja2(app) template = jinja2.environment.from_string( """{{ _('foo = %(bar)s', bar='foo') }}""") self.assertEqual(template.render(), 'foo = foo')
def test_engine_factory2(self): old_sys_path = sys.path[:] sys.path.insert(0, current_dir) app = Tipfy( config={ 'tipfyext.jinja2': { 'templates_dir': templates_dir, 'engine_factory': 'resources.get_jinja2_env', } }) request = Request.from_values() local.current_handler = handler = RequestHandler(app, request) jinja2 = Jinja2(app) message = 'Hello, World!' res = jinja2.render_template(local.current_handler, 'template1.html', message=message) self.assertEqual(res, message) sys.path = old_sys_path
def compile_templates(argv=None): """Compiles templates for better performance. This is a command line script. From the buildout directory, run: bin/jinja2_compile It will compile templates from the directory configured for 'templates_dir' to the one configured for 'templates_compiled_target'. At this time it doesn't accept any arguments. """ if argv is None: argv = sys.argv base_path = os.getcwd() app_path = os.path.join(base_path, 'app') gae_path = os.path.join(base_path, 'var/parts/google_appengine') extra_paths = [ app_path, os.path.join(app_path, 'lib'), os.path.join(app_path, 'lib', 'dist'), gae_path, # These paths are required by the SDK. os.path.join(gae_path, 'lib', 'antlr3'), os.path.join(gae_path, 'lib', 'django'), os.path.join(gae_path, 'lib', 'ipaddr'), os.path.join(gae_path, 'lib', 'webob'), os.path.join(gae_path, 'lib', 'yaml', 'lib'), ] sys.path = extra_paths + sys.path from config import config app = Tipfy(config=config) template_path = app.get_config('tipfyext.jinja2', 'templates_dir') compiled_path = app.get_config('tipfyext.jinja2', 'templates_compiled_target') if compiled_path is None: raise ValueError('Missing configuration key to compile templates.') if isinstance(template_path, basestring): # A single path. source = os.path.join(app_path, template_path) else: # A list of paths. source = [os.path.join(app_path, p) for p in template_path] target = os.path.join(app_path, compiled_path) # Set templates dir and deactivate compiled dir to use normal loader to # find the templates to be compiled. app.config['tipfyext.jinja2']['templates_dir'] = source app.config['tipfyext.jinja2']['templates_compiled_target'] = None if target.endswith('.zip'): zip_cfg = 'deflated' else: zip_cfg = None old_list_templates = FileSystemLoader.list_templates FileSystemLoader.list_templates = list_templates env = Jinja2.factory(app, 'jinja2').environment env.compile_templates(target, extensions=None, filter_func=filter_templates, zip=zip_cfg, log_function=logger, ignore_errors=False, py_compile=False) FileSystemLoader.list_templates = old_list_templates
def jinja2(self): return Jinja2.factory(self.app, 'jinja2', filters=custom_filters)