Ejemplo n.º 1
0
    def test_template_filter(self):
        import jstc
        with fso.push() as overlay:
            self.writecontent({
                'test/hello.hbs':
                'hello!',
                'test/goodbye.hbs':
                '''\
##! __here__
  so long!
##! friend
  ciao!
'''
            })
            self.assertEqual(
                jstc.render_assets('jstc:test/**.hbs',
                                   force_inline=True,
                                   force_precompile=False), '''\
<script type="text/x-handlebars" data-template-name="goodbye">so long!</script>\
<script type="text/x-handlebars" data-template-name="goodbye/friend">ciao!</script>\
<script type="text/x-handlebars" data-template-name="hello">hello!</script>\
''')
            self.assertEqual(
                jstc.render_assets(
                    'jstc:test/**.hbs',
                    force_inline=True,
                    force_precompile=False,
                    template_filter=lambda text, attrs: 'ciao' not in text),
                '''\
<script type="text/x-handlebars" data-template-name="goodbye">so long!</script>\
<script type="text/x-handlebars" data-template-name="hello">hello!</script>\
''')
Ejemplo n.º 2
0
    def test_asset_filter(self):
        import jstc
        with fso.push() as overlay:
            self.writecontent({
                'test/hello.hbs': 'hello!',
                'test/goodbye.hbs': 'so long!',
            })
            self.assertEqual(
                jstc.render_assets('jstc:test/**.hbs',
                                   force_inline=True,
                                   force_precompile=False), '''\
<script type="text/x-handlebars" data-template-name="goodbye">so long!</script>\
<script type="text/x-handlebars" data-template-name="hello">hello!</script>\
''')
            self.assertEqual(
                jstc.render_assets(
                    'jstc:test/**.hbs',
                    force_inline=True,
                    force_precompile=False,
                    asset_filter=lambda name: name == 'test/hello.hbs'), '''\
<script type="text/x-handlebars" data-template-name="hello">hello!</script>\
''')
            self.assertEqual(
                jstc.render_assets(
                    'jstc:test/**.hbs',
                    force_inline=True,
                    force_precompile=False,
                    asset_filter=lambda name: name != 'test/hello.hbs'), '''\
<script type="text/x-handlebars" data-template-name="goodbye">so long!</script>\
''')
Ejemplo n.º 3
0
    def test_script_wrapper(self):
        import jstc
        with fso.push() as overlay:
            self.writecontent({
                'test/hello.hbs': 'hello, world!',
                'test/hello/name.hbs': 'hello, {{name}}!',
            })
            compiled = jstc.render_assets(
                'jstc:test/**.hbs',
                force_inline=True,
                force_precompile=True,
                script_wrapper=lambda script, *args, **kw: '<SCRIPT>' + script
                + '</SCRIPT>')
            if 'text/x-handlebars' in compiled:
                raise unittest.SkipTest(
                    'handlebars executable not available (use "npm install handlebars")'
                )
            self.assertMultiLineEqual(
                compiled, '''\
<SCRIPT>(function(){var t=Handlebars.template,ts=Handlebars.templates=Handlebars.templates||{};ts["hello"]=t({"compiler":[7,">= 4.0.0"],"main":function(container,depth0,helpers,partials,data) {
    return "hello, world!";
},"useData":true});ts["hello/name"]=t({"compiler":[7,">= 4.0.0"],"main":function(container,depth0,helpers,partials,data) {
    var helper;

  return "hello, "
    + container.escapeExpression(((helper = (helper = helpers.name || (depth0 != null ? depth0.name : depth0)) != null ? helper : helpers.helperMissing),(typeof helper === "function" ? helper.call(depth0 != null ? depth0 : {},{"name":"name","hash":{},"data":data}) : helper)))
    + "!";
},"useData":true});})();</SCRIPT>''')
Ejemplo n.º 4
0
    def test_name_transform(self):
        import jstc
        with fso.push() as overlay:
            self.writecontent({
                'test/hello.hbs': 'hello!',
                'test/goodbye.hbs': 'so long!',
            })

            def mynt(name, root):
                return (name[2:].replace('d', 'd-').split('.')[0],
                        'text/x-mustache')

            self.assertEqual(
                jstc.render_assets('jstc:test/**.hbs',
                                   force_inline=True,
                                   force_precompile=False,
                                   name_transform=mynt), '''\
<script type="text/x-mustache" data-template-name="st/good-bye">so long!</script>\
<script type="text/x-mustache" data-template-name="st/hello">hello!</script>\
''')
Ejemplo n.º 5
0
    def test_template_transform(self):
        import jstc
        with fso.push() as overlay:
            self.writecontent({
                'test/hello.hbs': 'hello!',
                'test/goodbye.hbs': 'so long!',
            })

            def mytt(text, attrs):
                if attrs.name == 'hello':
                    text = 'hello, world!'
                    attrs.id = 'HW'
                else:
                    attrs.type = 'template/jst'
                return (text, attrs)

            self.assertEqual(
                jstc.render_assets('jstc:test/**.hbs',
                                   force_inline=True,
                                   force_precompile=False,
                                   template_transform=mytt), '''\
<script type="template/jst" data-template-name="goodbye">so long!</script>\
<script type="text/x-handlebars" data-template-name="hello" id="HW">hello, world!</script>\
''')