GithubHelp home page GithubHelp logo

erkin-m / tmpl.loader Goto Github PK

View Code? Open in Web Editor NEW

This project forked from stevenmhunt/tmpl.loader

0.0 0.0 0.0 23 KB

Loads external html template files into javascript and renders them using a template engine of your choice.

License: Other

JavaScript 100.00%

tmpl.loader's Introduction

tmpl.loader: Simple Template Loading and Rendering

Version 0.2

Loads external template files and renders them with a template engine in JavaScript.

Features

Allows templates to be broken out into seperate files.

Supports multiple template engines side-by-side on a single page.

Works with jQuery... or not. Your choice.

Notifies your javascript code when templates have loaded with the ready() callbacks.

Supported Templating Engines

  • jsRender
  • jSmart
  • Handlebars
  • Mustache
  • Plates
  • IST
  • doT
  • More to come...!

How To Use

  1. Add your template file as a link:
<link rel="template/jsrender" type="text/html" href="mytemplate.tmpl.html" />

Note that the name of the template is automatically extracted from the file name.

  1. In JavaScript, add a ready callback.
//set a callback so that you can do something with the templates once they are loaded.
tmplLoader.ready(function() {
	
	//now we're ready to start rendering with the template.
	
	//just call tmplLoader.render() and pass the template name and a model to bind with.
	var content = tmplLoader.render('mytemplate', { message: "whatever" });
	
	//let's have it pop up on the screen.
	alert('rendered content: '+content);		
});

Fancy Stuff

You can register a built-in template engine with a custom alias I.E. rel="template/custom-alias":

//jsrender by default uses "jsrender" and is configured automatically.
tmplLoader.engines.jsrender('custom-alias'); 

You can also register a completely custom template engine:

//register a custom engine:
tmplLoader.engines.register('engine-name', {
	//code to register the template with some sort of collection.
	register: function(name, data) {
		...
	}),
	//code to render the template given some set of data input.
	render: function(name, data) {
		...
	}),
	//code to reset the template collection for the engine.
	reset: function() {
		...
	}
});

If you want to have multiple templates in a single large file (production scenarios possibly), you can do the following:

  1. Create your template file with comments indicating the sections:
<!-- template section1 -->
template section 1
...
<!-- template section2 -->
template section 2
...
  1. Add your template with "-multipart" at the end of the REL attribute:
<link rel="template/jsrender-multipart" type="text/html" href="mytemplate_multi.tmpl.html" />
  1. Reference the templates as "file name"_"part name":
tmplLoader.render('mytemplate_multi_section1', { data: "foo" });

Future Releases

Add more templating engines.

Add unit testing to repo.

tmpl.loader's People

Contributors

stevenmhunt avatar jeroenransijn 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.