class TemplateTest(unittest.TestCase): def setUp(self): templates_dir = os.path.join(os.path.dirname(__file__), '..', '..', 'iktomi', 'templates', 'jinja2', 'templates') self.engine = TemplateEngine(templates_dir) self.template = Template(templates_dir, engines={'html': self.engine}) def test_render_textarea(self): widget = Mock(id=101, classname="big", input_name="big_input") rendered = self.template.render('widgets/textarea', widget=widget, readonly=True, value="Sample text") self.assertIn('<textarea', rendered) self.assertIn('id="101"', rendered) self.assertIn('class="big"', rendered) self.assertIn('name="big_input"', rendered) self.assertIn('readonly="readonly"', rendered) self.assertIn('>Sample text<', rendered) def test_resolve(self): filename, engine = self.template.resolve('widgets/textarea') self.assertEqual(filename, 'widgets/textarea.html') self.assertEqual(engine, self.engine) with self.assertRaises(TemplateError): self.template.resolve('nonexsistent/path')
def env(self): DIR = jnj.__file__ DIR = path.dirname(path.abspath(DIR)) TEMPLATES = [path.join(DIR, 'templates')] jinja_loader = TemplateEngine(TEMPLATES) template_loader = Template(engines={'html': jinja_loader}, *TEMPLATES) env = VersionedStorage() env.template = BoundTemplate(env, template_loader) return env
def setUp(self): class TestingBoundTemplate(BoundTemplate): def get_template_vars(self): return {'readonly': True} templates_dir = os.path.join(os.path.dirname(__file__), '..', '..', 'iktomi', 'templates', 'jinja2', 'templates') self.engine = TemplateEngine(templates_dir) template = Template(templates_dir, engines={'html': self.engine}) env = web.AppEnvironment.create() self.bound = TestingBoundTemplate(env, template)
class TemplateTest(unittest.TestCase): def setUp(self): templates_dir = os.path.join( os.path.dirname(__file__), "..", "..", "iktomi", "templates", "jinja2", "templates" ) self.engine = TemplateEngine(templates_dir) self.template = Template(templates_dir, engines={"html": self.engine}) def test_render_textarea(self): widget = Mock(id=101, classname="big", input_name="big_input") rendered = self.template.render("widgets/textarea", widget=widget, readonly=True, value="Sample text") self.assertIn("<textarea", rendered) self.assertIn('id="101"', rendered) self.assertIn('class="big"', rendered) self.assertIn('name="big_input"', rendered) self.assertIn('readonly="readonly"', rendered) self.assertIn(">Sample text<", rendered) def test_resolve(self): filename, engine = self.template.resolve("widgets/textarea") self.assertEqual(filename, "widgets/textarea.html") self.assertEqual(engine, self.engine) with self.assertRaises(TemplateError): self.template.resolve("nonexsistent/path")
def test_render(self): class F(Form): fields = [ Field('first', convs.Int(), initial=1), Field('second', convs.Int()), ] templates_dir = os.path.join(os.path.dirname(__file__), '..', '..', 'iktomi', 'templates', 'jinja2', 'templates') engine = TemplateEngine(templates_dir) template = Template(templates_dir, engines={'html': engine}) env = AppEnvironment.create(template=template) form = F(env) form.accept({'first': '123', 'second': '246'}) self.assertIn('<input', form.render()) self.assertIn('id="first"', form.render()) self.assertIn('value="123"', form.render()) self.assertIn('class="textinput"', form.render()) self.assertIn('id="second"', form.render()) self.assertIn('value="246"', form.render())
def setUp(self): templates_dir = os.path.join( os.path.dirname(__file__), "..", "..", "iktomi", "templates", "jinja2", "templates" ) self.engine = TemplateEngine(templates_dir) self.template = Template(templates_dir, engines={"html": self.engine})
def setUp(self): templates_dir = os.path.join(os.path.dirname(__file__), '..', '..', 'iktomi', 'templates', 'jinja2', 'templates') self.engine = TemplateEngine(templates_dir) self.template = Template(templates_dir, engines={'html': self.engine})
# -*- coding: utf-8 -*- from iktomi import web from iktomi.web.filters import * from iktomi.templates import jinja2, Template from environment import Environment import cfg import handlers as h static = static_files(cfg.STATIC) media = static_files(cfg.MEDIA_DIR, cfg.MEDIA_URL) form_temp = static_files(cfg.FORM_TEMP, cfg.FORM_TEMP_URL) template = Template(cfg.TEMPLATES, jinja2.TEMPLATE_DIR, engines={'html': jinja2.TemplateEngine}) app = web.cases( static, media, form_temp, match('/', 'files') | web.cases( # Playing REST ;) method('GET') | h.list_files, method('POST') | h.post_file, #method('DELETE') | h.delete_files, ), ) wsgi_app = web.Application(app, env_class=Environment)
import logging from iktomi import web from iktomi.unstable.db.files import FileManager from iktomi.templates import Template, BoundTemplate as BaseBoundTemplate from iktomi.templates.jinja2 import TemplateEngine from iktomi.utils.storage import storage_cached_property from iktomi.utils import cached_property #from jinja2 import Markup import cfg logger = logging.getLogger(__name__) static = web.static_files('static') jinja_loader = TemplateEngine(cfg.TEMPLATES) template_loader = Template(engines={'html': jinja_loader}, *cfg.TEMPLATES) file_manager = FileManager(cfg.FORM_TEMP, cfg.MEDIA_DIR, cfg.FORM_TEMP_URL, cfg.MEDIA_URL) class Environment(web.AppEnvironment): cfg = cfg file_manager = file_manager @cached_property def url_for(self): return self.root.build_url @cached_property def url_for_static(self): return static.construct_reverse()