Coder Social home page Coder Social logo

hellatan / webpack-compile-templates Goto Github PK

View Code? Open in Web Editor NEW

This project forked from robrichard/webpack-compile-templates

0.0 2.0 0.0 122 KB

Compiles underscore templates from HTML script tags into CommonJS in a webpack loader.

JavaScript 100.00%

webpack-compile-templates's Introduction

#webpack-compile-templates

Webpack version of this.

Compiles underscore templates from HTML script tags into CommonJS in a webpack loader. You can add multiple script tags to each file. Requiring the file will return an object with a property for each script tag. The ID attribute is the key and the compiled template function as the value.

See the underscore documentation for more details.

Usage

Install

npm install --save-dev webpack-compile-templates

Create a template file

myTemplates.html

Use the ID attribute to identify the template from your JS source. Use data-variable-name to change the variable name that is used in the underscore template. obj is the default

<script type="text/template" id="template1">
	<h2><%- obj.title %></h2>
</script>

<script type="text/template" id="template2" data-variable-name="data">
	<li><%- data.name %> <<%- data.email %>></li>
</script>

Require the template file

A JS file

var $ = require('jquery');
var templates = require('/path/to/myTemplates');

$('.container').html(templates.template1({ title: 'My Page Title' }));
$('.container').append(templates.template2({
	name: 'Rob',
	email '[email protected]'
}));

Add to webpack

Register the template and tell webpack to look for html extensions

 loaders: [
            { test: /\.html$/, loader: 'webpack-compile-templates' },

Why?

The advantage of this transform over other transforms or plugins is that the templates are backwards compatible with non-CommonJS code. Template files formatted this way can also be included directly in HTML. A UMD module that is shared in both a CommonJS and non-commonJS codebase can be used like this:

(function (root, factory) {
    if (typeof module !== 'undefined') {
        // CommonJS - templates are precompiled and bundled in with JS
        var templates = require('/path/to/myTemplates');
        factory(
            module,
            templates.template1,
            templates.tempalte2
        );
    } else {
        // Browser globals. Templates are included in html and need to be compiled client-side
        var $template1 = $('#template1');
        var $tempalte2 = $('#template2');
        factory(
            _module,
            _.template($template1.html(), null, {variable: $template.attr('data-variable-name')),
            _.template($template2.html(), null, {variable: $template.attr('data-variable-name'))
        );
    }
}(window || global, function (module, template1, template2) {
// Use compliled templates in here
}));

The commonJS environment gets the benefit of the precompiled template. Other environments can still include the file on the page and access it by ID.

webpack-compile-templates's People

Contributors

hellatan avatar robrichard avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.