Comparing version 0.4.0 to 0.4.1
81
index.js
@@ -9,9 +9,9 @@ /** | ||
var fs = require('fs'); | ||
var vm = require('vm'); | ||
var p = require('path'); | ||
const fs = require('fs'); | ||
const vm = require('vm'); | ||
const p = require('path'); | ||
var odesza = {}; | ||
var blocks = {}; | ||
var cache = { | ||
const odesza = {}; | ||
const blocks = {}; | ||
const cache = { | ||
templates: {}, | ||
@@ -21,2 +21,8 @@ paths: {} | ||
// cache control | ||
var useCache = true; | ||
// matches keyword statements (block, include, extends) | ||
const re = /(block|extends|include) ([\/\.\w]+)/g; | ||
/** | ||
@@ -92,7 +98,3 @@ * Renders a template with the given variables. | ||
try { | ||
return vm.runInNewContext('`' + template + '`', options).trim(); | ||
} catch (e) { | ||
throw new Error(e); | ||
} | ||
return vm.runInNewContext('`' + template + '`', options).trim(); | ||
}; | ||
@@ -103,3 +105,3 @@ | ||
* | ||
* @param {string} path The path to the template file. | ||
* @param {string} location The location to the template file. | ||
* @param {object} options Options passed in to render the template. | ||
@@ -109,15 +111,11 @@ * @return {string} The rendered template. | ||
odesza.renderFile = function(path, options) { | ||
path = resolvePath(path); | ||
var basePath = path.substr(0, path.lastIndexOf('/') + 1); | ||
odesza.renderFile = function(location, options) { | ||
location = resolvePath(location); | ||
var basePath = location.substr(0, location.lastIndexOf('/') + 1); | ||
var template; | ||
try { | ||
if (cache.templates[path] == null) { | ||
template = fs.readFileSync(path).toString().trim(); | ||
cache.templates[path] = template; | ||
} else { | ||
template = cache.templates[path]; | ||
} | ||
} catch (e) { | ||
throw new Error(e); | ||
if (useCache && cache.templates[location] != null) { | ||
template = cache.templates[location]; | ||
} else { | ||
template = fs.readFileSync(location).toString().trim(); | ||
cache.templates[location] = template; | ||
} | ||
@@ -128,4 +126,15 @@ return odesza.render(template, options, basePath); | ||
/** | ||
* Disables template and path caching. | ||
* | ||
* @public | ||
*/ | ||
odesza.disableCache = function() { | ||
useCache = false; | ||
}; | ||
/** | ||
* Adds support for express. | ||
* | ||
* @public | ||
* @param {string} path | ||
@@ -144,6 +153,11 @@ * @param {object} options | ||
// matches keyword statements | ||
const re = /(block|extends|include) ([\/\.\w]+)/g; | ||
/** | ||
* Returns an object of keyword statements for a given template string. | ||
* | ||
* @private | ||
* @param {string} template The template string to find keywords in. | ||
* @return {object} An object ontaining extends, block, and include statements | ||
* found in the template string. | ||
*/ | ||
// returns keyword statements found in the template | ||
function getStatements(template) { | ||
@@ -162,8 +176,15 @@ var s = { | ||
// resolves the template file path, throwing an error if anything is wrong | ||
/** | ||
* Resolves the template file path, throwing an error if anything is wrong | ||
* | ||
* @private | ||
* @param {string} path The relative path to the file. | ||
* @return {string} The resolved path. | ||
*/ | ||
function resolvePath(path) { | ||
if (typeof path != 'string') { | ||
throw new TypeError('path must be a string'); | ||
throw new TypeError('invalid path: input must be a string'); | ||
} | ||
if (cache.paths[path] != null) { | ||
if (useCache && cache.paths[path] != null) { | ||
return cache.paths[path]; | ||
@@ -170,0 +191,0 @@ } |
{ | ||
"name": "odesza", | ||
"version": "0.4.0", | ||
"version": "0.4.1", | ||
"description": "Write clean, expressive templates with just HTML and inline JavaScript", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
109
README.md
@@ -13,3 +13,3 @@ # Odesza | ||
### Variables & Expressions | ||
### Syntax | ||
Variables are passed in when Odesza templates are rendered. Scope is maintained through includes and extends. You can also treat `${}` as a function statement. | ||
@@ -19,6 +19,12 @@ | ||
```javascript | ||
<title>${title}</title> | ||
<p> | ||
Welcome, ${names.map(n => `<i>${n}</i>`).join(', ')}! | ||
</p> | ||
<html> | ||
<head> | ||
<title>${title}</title> | ||
</head> | ||
<body> | ||
<p> | ||
Welcome, ${names.join(', ')}! | ||
</p> | ||
</body> | ||
</html> | ||
``` | ||
@@ -36,48 +42,14 @@ **code** | ||
```html | ||
<title>hello world</title> | ||
<p> | ||
Welcome, <i>foo</i>, <i>bar</i> | ||
</p> | ||
<html> | ||
<head> | ||
<title>hello world</title> | ||
</head> | ||
<p> | ||
Welcome, foo, bar | ||
</p> | ||
</body> | ||
</html> | ||
``` | ||
### Inline JavaScript | ||
Odesza makes it easy to write inline JavaScript in your templates. Under the hood, templates are evaluated as ES6 template strings, which means you have access to `${}` expressions. If you need more flexibility with inline js, you can create a self-executing function expression with code inside it like this: `${(() => { ... })()}`. | ||
**greetings.ode** | ||
```javascript | ||
<h2>welcome ${names.join(', ')}!</h2> | ||
${(() => { | ||
// this is a self-executing function expression inside an ES6 template string. | ||
// essentially that means you can write any inline js you want here. the | ||
// following code is to demonstrate how you can programatically generate HTML. | ||
var items = []; | ||
names.forEach((name, index) => { | ||
items.push(`<div>${index + 1}: ${name}</div>`); | ||
}); | ||
return items.join('<br/>'); | ||
})()} | ||
``` | ||
**code** | ||
```javascript | ||
var vars = { | ||
names: ['wells', 'joe', 'dom'] | ||
}; | ||
odesza.renderFile('greetings.ode', vars); | ||
``` | ||
**output** | ||
```html | ||
<h2>welcome wells, joe, dom!</h2> | ||
<div>1: wells</div><br/> | ||
<div>2: joe</div><br/> | ||
<div>3: dom</div> | ||
``` | ||
### Partials | ||
### Including Partials | ||
Odesza makes it easy to nest templates within each other. You can include templates as many levels deep as you like. Variables maintain scope in included files. | ||
@@ -184,2 +156,41 @@ | ||
### Inline JavaScript | ||
Odesza makes it easy to write inline JavaScript in your templates. Under the hood, templates are evaluated as ES6 template strings, which means you have access to `${}` expressions. If you need more flexibility with inline js, you can create a self-executing function expression with code inside it like this: `${(() => { ... })()}`. | ||
**greetings.ode** | ||
```javascript | ||
<h2>welcome ${names.join(', ')}!</h2> | ||
${(() => { | ||
// this is a self-executing function expression inside an ES6 template string. | ||
// essentially that means you can write any inline js you want here. the | ||
// following code is to demonstrate how you can programatically generate HTML. | ||
var items = []; | ||
names.forEach((name, index) => { | ||
items.push(`<div>${index + 1}: ${name}</div>`); | ||
}); | ||
return items.join('<br/>'); | ||
})()} | ||
``` | ||
**code** | ||
```javascript | ||
var vars = { | ||
names: ['wells', 'joe', 'dom'] | ||
}; | ||
odesza.renderFile('greetings.ode', vars); | ||
``` | ||
**output** | ||
```html | ||
<h2>welcome wells, joe, dom!</h2> | ||
<div>1: wells</div><br/> | ||
<div>2: joe</div><br/> | ||
<div>3: dom</div> | ||
``` | ||
### Express Support | ||
@@ -186,0 +197,0 @@ **index.js** |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
18006
288
218