How to use handlebars - 10 common examples

To help you get started, we’ve selected a few handlebars examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github nuysoft / Mock / test / mock4xtpl.js View on Github external
{{#each items}}
  <li>{{agree_button}}</li>
  {{/each}}

        */
    })
    var ast = Handlebars.parse(tpl)
    // console.log(JSON.stringify(ast, null, 4));
    var result = Mock4Tpl.gen(ast, null, {
        'items|1-10': [{
                'id|+1': 1,
                name: '@FIRST @LAST',
                emotion: '@SENTENCE'
            }
        ]
    }, Handlebars.helpers)
    // console.log(JSON.stringify(result, null, 4))
    // console.log(Handlebars.helpers)

    test.ok(util.isArray(result.items))
    test.ok(result.items.length &gt;= 1 &amp;&amp; result.items.length &lt;= 10)
    test.ok(!('agree_button' in result.items[0]))
    test.ok('name' in result.items[0])
    test.ok('emotion' in result.items[0])
    test.done()
}
github chrisabrams / The-Tramp / test / modules / handlebars.js View on Github external
.forEach(function(filepath) {
        var src = grunt.file.read(filepath);
        var compiled, filename;
        try {
          compiled = require('handlebars').precompile(src);
          // if configured to, wrap template in Handlebars.template call
          if (options.wrapped) {
            compiled = 'Handlebars.template('+compiled+')\n';
          }
        } catch (e) {
          grunt.log.error(e);
          grunt.fail.warn('Handlebars failed to compile '+filepath+'.');
        }

        // register partial or add template to namespace
        if (isPartial.test(_.last(filepath.split('/')))) {
          filename = processPartialName(filepath);
          partials.push('Handlebars.registerPartial('+JSON.stringify(filename)+', '+compiled+');');
        } else {
          filename = processName(filepath);
          templates.push("Handlebars = require('handlebars')")
github nuysoft / Mock / test / mock4xtpl.js View on Github external
function run(tpl, options) {
    // console.log()
    // console.log(tpl);
    // console.log(options);

    var ast = Handlebars.parse(tpl);
    // console.log(ast);
    // console.log(JSON.stringify(ast, null, 4));

    var data = Mock4Tpl.gen(ast, null, options)
    // console.log(JSON.stringify(data, null, 4));
    return data
}
github nuysoft / Mock / test / nodeunit / mock4xtpl-node.js View on Github external
exports.test_string_sub_tpl = function(test) {
    var tpl4xtpl, data4xtpl, html4xtpl,
        tpl4hdb, data4hdb, html4hdb;

    XTemplate.addSubTpl('sub-tpl-1', '{{title}}')
    Handlebars.registerPartial("sub-tpl-1", '{{title}}');

    tpl4xtpl = '{{include "sub-tpl-1"}}'
    tpl4hdb = '{{> sub-tpl-1}}'
    // test.deepEqual(Handlebars.parse(tpl4xtpl), Handlebars.parse(tpl4hdb))
    // console.log(JSON.stringify(Mock4XTpl.parse(tpl4xtpl), null, 4));
    // console.log(JSON.stringify(Handlebars.parse(tpl4xtpl), null, 4));
    // console.log(JSON.stringify(Handlebars.parse(tpl4hdb), null, 4));

    data4xtpl = Mock4XTpl.mock(tpl4xtpl)
    data4hdb = Mock4XTpl.mock(tpl4xtpl, {}, Handlebars.helpers, Handlebars.partials)
    test.deepEqual(data4xtpl, data4hdb)
    // console.log(JSON.stringify(data4xtpl, null, 4))

    html4xtpl = new XTemplate(tpl4xtpl).render(data4xtpl)
    html4hdb = Handlebars.compile(tpl4hdb)(data4hdb)
    test.deepEqual(html4xtpl, html4hdb)
    // console.log(html)

    test.done()
}
github material-components / material-components-web / test / screenshot / infra / lib / report-writer.js View on Github external
registerPartials_() {
    const partialFilePaths = this.localStorage_.globFiles(path.join(TEST_DIR_RELATIVE_PATH, 'report/_*.hbs'));
    for (const partialFilePath of partialFilePaths) {
      // TODO(acdvorak): What about hyphen/dash characters?
      const name = path.basename(partialFilePath)
        .replace(/^_/, '') // Remove leading underscore
        .replace(/\.\w+$/, '') // Remove file extension
      ;
      const content = this.localStorage_.readTextFileSync(partialFilePath);
      Handlebars.registerPartial(name, content);
    }
  }
github shannonmoeller / handlebars-layouts / test / handlebars-layouts.e2e.js View on Github external
beforeEach(function () {
		hbs = handlebars.create();
		handlebarsLayouts.register(hbs);

		// Register partials
		hbs.registerPartial({
			'deep-a': read(config.partials, 'deep-a.hbs'),
			'deep-b': read(config.partials, 'deep-b.hbs'),
			'deep-c': read(config.partials, 'deep-c.hbs'),
			'parent-context': read(config.partials, 'parent-context.hbs'),
			context: read(config.partials, 'context.hbs'),
			layout2col: read(config.partials, 'layout2col.hbs'),
			layout: read(config.partials, 'layout.hbs'),
			media: read(config.partials, 'media.hbs'),
			user: read(config.partials, 'user.hbs')
		});
	});
github spmjs / spm / test / data / modules / moduleA / build / _tar / moduleA / src / templatable.js View on Github external
function convertTemplateToObject(template) {
        var statements = Handlebars.parse(template).statements;
        var html = '';

        for (var i = 0, len = statements.length; i &lt; len; i++) {
            var stat = statements[i];

            // AST.ContentNode
            if (stat.type === 'content') {
                html += stat.string;
            }
            // AST.MustacheNode or AST.BlockNode
            else {
                html += '{{STAT ' + i + '}}';
            }
        }

        html = encode(html);
github zulip / zulip / frontend_tests / zjsunit / render.js View on Github external
exports.compile_template = function (name) {
    var included_fns = exports.find_included_partials(name);

    _.each(included_fns, function (fn) {
        exports.compile_template(fn);
    });

    if (Handlebars.templates === undefined) {
        Handlebars.templates = {};
    }

    if (_.has(Handlebars.template, name)) {
        // we already compile the template
        return;
    }

    var file = exports.template_finder.get(name);
    var data = fs.readFileSync(file.url).toString();
    Handlebars.templates[name] = Handlebars.compile(data);
};
github pmlopes / vertx-starter / gulpfile.js View on Github external
return callback();
    }

    const contents = file.contents.toString();
    let compiled = null;

    // binary files
    if (['favicon.ico'].indexOf(file.path.substr(file.path.lastIndexOf('/') + 1)) !== -1) {
      // binary file are stored as is
      compiled =
        "{\"compiler\":[7,\">= 4.0.0\"],\"main\":function(container,depth0,helpers,partials,data) {\n" +
        "    return " + JSON.stringify(contents) + ";\n" +
        "},\"useData\":false}\n";
    } else {
      try {
        compiled = handlebars.precompile(
          handlebars.parse(contents),
          compilerOptions
        ).toString();
      } catch (err) {
        console.log(err);
        this.emit('error', new PluginError("Handlebars plugin", err, {
          fileName: file.path
        }));
        return callback();
      }
    }

    file.contents = Buffer.from(compiled);
    file.templatePath = file.relative;
    file.path = gutil.replaceExtension(file.path, '.js');
github pmlopes / vertx-starter / gulpfile.js View on Github external
}

    const contents = file.contents.toString();
    let compiled = null;

    // binary files
    if (['favicon.ico'].indexOf(file.path.substr(file.path.lastIndexOf('/') + 1)) !== -1) {
      // binary file are stored as is
      compiled =
        "{\"compiler\":[7,\">= 4.0.0\"],\"main\":function(container,depth0,helpers,partials,data) {\n" +
        "    return " + JSON.stringify(contents) + ";\n" +
        "},\"useData\":false}\n";
    } else {
      try {
        compiled = handlebars.precompile(
          handlebars.parse(contents),
          compilerOptions
        ).toString();
      } catch (err) {
        console.log(err);
        this.emit('error', new PluginError("Handlebars plugin", err, {
          fileName: file.path
        }));
        return callback();
      }
    }

    file.contents = Buffer.from(compiled);
    file.templatePath = file.relative;
    file.path = gutil.replaceExtension(file.path, '.js');

    callback(null, file);