Created
May 15, 2012 18:15
-
-
Save abuisman/2703897 to your computer and use it in GitHub Desktop.
A modular (AMD) template file loader for for example underscore.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
A modular (AMD) template file loader for for example underscore.js. | |
You can use the templater to load up html files as a string in order to use them as templates | |
for your client side javascript templating. | |
Pay notice to the 'base_path'. Set this to where your templates reside. The path below is | |
assuming a Rails application. | |
You could also not use the base path and just pass it in fully every time. | |
The script will return an object called whatever you want, then you can call the 'get' | |
method on that returned object and pass in the name of the template that you want. | |
I have added the extension '.tpl.html' to all my templates: | |
assets/ | |
app/ | |
templates/ | |
todos.tpl.html | |
todo.tpl.html | |
task.tpl.html | |
etc... | |
*/ | |
define [], -> | |
template = (-> | |
_private = | |
base_path: '/assets/app/templates/' | |
get: (templateName) -> | |
return jQuery.ajax | |
url: _private.base_path + templateName + ".tpl.html" | |
type: 'get' | |
dataType: 'text' | |
async: false | |
.responseText | |
get: (args) -> | |
args = Array.prototype.slice.call(arguments) | |
if args[0] | |
return _private.get(args[0]) | |
)(); | |
return template |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# The non-modular version of it. | |
window.templater = (-> | |
_private = | |
base_path: '/assets/pos/templates/' | |
get: (templateName) -> | |
return jQuery.ajax | |
url: _private.base_path + templateName + ".tpl.html" | |
type: 'get' | |
dataType: 'text' | |
async: false | |
.responseText | |
get: (args) -> | |
args = Array.prototype.slice.call(arguments) | |
if args[0] | |
return _private.get(args[0]) | |
)(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment