Permalink
Browse files

Added templates and mixins (see updated readme)

  • Loading branch information...
forste committed Jun 4, 2012
1 parent 120adaa commit 72070e93301bd399bc87852f2bddb2dfc86fd6f5
View
@@ -18,6 +18,8 @@ Embedded JavaScript templates.
* Filter support for designer-friendly templates
* Client-side support
* Newline slurping with `<% code -%>` or `<% -%>` or `<%= code -%>` or `<%- code -%>`
+ * Templating with <%+ template.html %> and <%~ argumentName %>
+ * Mixins with <%# mixin.html %>
## Example
@@ -121,6 +123,85 @@ ejs.filters.last = function(obj) {
};
```
+## Templates
+
+Path to templates can be set by setting the setting property like
+```js
+renderFile(path, { settings : { views : 'path/to/views' } }, cb)
+```
+
+
+### Template - template.html
+<html>
+<head>
+ <title>My site </title>
+ <link href="/css/mysite.css" rel="stylesheet">
+</head>
+<body>
+ <%~ body %>
+
+ <hr>
+ <footer>
+ <p>&copy; Brand 2012</p>
+ </footer>
+ <script src="/js/jquery.js"></script>
+
+ <%~ scripts %>
+</body>
+
+### Using template (settings.html)
+
+<%+ template.html %>
+<%+ body %>
+<p>settings here</p>
+<%+%>
+<%+ scripts %>
+ <script src="/js/settings.js"></script>
+<%+%>
+
+### Output
+
+<html>
+<head>
+ <title>My site </title>
+ <link href="/css/mysite.css" rel="stylesheet">
+</head>
+<body>
+ <p>settings here</p>
+
+ <hr>
+ <footer>
+ <p>&copy; Brand 2012</p>
+ </footer>
+ <script src="/js/jquery.js"></script>
+ <script src="/js/settings.js"></script>
+</body>
+
+## Mixins
+
+Path to mixins can be set by setting the setting property like
+```js
+renderFile(path, { settings : { views : 'path/to/views' } }, cb)
+```
+
+### Mixin (mixin.html)
+
+<button id="mixin">
+</button>
+
+### Using mixins
+
+<div>
+ <%# mixin.html %>
+</div>
+
+### Output
+
+<div>
+ <button id="mixin">
+ </button>
+</div>
+
## client-side support
include `./ejs.js` or `./ejs.min.js` and `require("ejs").compile(str)`.
View
30 ejs.js
@@ -1,3 +1,4 @@
+exports.test = 'bla';
// CommonJS require()
@@ -31,11 +32,11 @@ require.register = function (path, fn){
require.relative = function (parent) {
return function(p){
if ('.' != p.substr(0, 1)) return require(p);
-
+
var path = parent.split('/')
, segs = p.split('/');
path.pop();
-
+
for (var i = 0; i < segs.length; i++) {
var seg = segs[i];
if ('..' == seg) path.pop();
@@ -70,15 +71,15 @@ exports.version = '0.6.1';
/**
* Filters.
- *
+ *
* @type Object
*/
var filters = exports.filters = require('./filters');
/**
* Intermediate js cache.
- *
+ *
* @type Object
*/
@@ -139,11 +140,11 @@ function rethrow(err, str, filename, lineno){
// Alter exception message
err.path = filename;
- err.message = (filename || 'ejs') + ':'
- + lineno + '\n'
- + context + '\n\n'
+ err.message = (filename || 'ejs') + ':'
+ + lineno + '\n'
+ + context + '\n\n'
+ err.message;
-
+
throw err;
}
@@ -165,13 +166,13 @@ var parse = exports.parse = function(str, options){
, "\nwith (locals) {"
, "\n buf.push('"
];
-
+
var lineno = 1;
for (var i = 0, len = str.length; i < len; ++i) {
if (str.slice(i, open.length + i) == open) {
i += open.length
-
+
var prefix, postfix, line = '__stack.lineno=' + lineno;
switch (str.substr(i, 1)) {
case '=':
@@ -228,12 +229,12 @@ var parse = exports.parse = function(str, options){
var compile = exports.compile = function(str, options){
options = options || {};
-
+
var input = JSON.stringify(str)
, filename = options.filename
? JSON.stringify(options.filename)
: 'undefined';
-
+
// Adds the fancy stack trace meta info
str = [
'var __stack = { lineno: 1, input: ' + input + ', filename: ' + filename + ' };',
@@ -244,7 +245,7 @@ var compile = exports.compile = function(str, options){
' rethrow(err, __stack.input, __stack.filename, __stack.lineno);',
'}'
].join("\n");
-
+
if (options.debug) console.log(str);
var fn = new Function('locals, filters, escape', str);
return function(locals){
@@ -563,5 +564,6 @@ exports.escape = function(html){
.replace(/>/g, '&gt;')
.replace(/"/g, '&quot;');
};
-
+
}); // module: utils.js
+
Oops, something went wrong.

0 comments on commit 72070e9

Please sign in to comment.