Skip to content

Commit

Permalink
Make swig.init() optional for browser mode with no custom settings.
Browse files Browse the repository at this point in the history
  • Loading branch information
paularmstrong committed Oct 22, 2011
1 parent d7f891c commit 0e871e2
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 19 deletions.
4 changes: 3 additions & 1 deletion docs/getting-started.md
Expand Up @@ -4,7 +4,7 @@ Getting Started <a name="getting-started" href="#getting-started">#</a>
Init Swig <a name="init" href="#init">#</a>
---------

In order to start using Swig, you must intialize it. Swig can be configured using the following:
In order to start using Swig, you should initialize it. Swig can be configured using the following:

swig.init({
allowErrors: false,
Expand All @@ -15,6 +15,8 @@ In order to start using Swig, you must intialize it. Swig can be configured usin
tags: {}
});

This step is _optional_, however it is recommended to at least set the `root` key when running Swig from node.js.

### Options

#### allowErrors _optional_
Expand Down
38 changes: 20 additions & 18 deletions index.js
Expand Up @@ -8,20 +8,22 @@ var fs = require('fs'),

_ = require('underscore');

var config,
CACHE = {};

// Call this before using the templates
exports.init = function (options) {
config = _.extend({
var config = {
allowErrors: false,
autoescape: true,
encoding: 'utf8',
root: '/'
}, options);
filters: filters,
root: '/',
tags: tags
},
_config = _.extend({}, config),
CACHE = {};

config.filters = _.extend(filters, options.filters);
config.tags = _.extend(tags, options.tags);
// Call this before using the templates
exports.init = function (options) {
_config = _.extend({}, config, options);
_config.filters = _.extend(filters, options.filters);
_config.tags = _.extend(tags, options.tags);
};

function TemplateError(error) {
Expand All @@ -46,11 +48,11 @@ function createTemplate(data, id) {
render;

// The template token tree before compiled into javascript
if (config.allowErrors) {
template.tokens = parser.parse.call(template, data, config.tags, config.autoescape);
if (_config.allowErrors) {
template.tokens = parser.parse.call(template, data, _config.tags, _config.autoescape);
} else {
try {
template.tokens = parser.parse.call(template, data, config.tags, config.autoescape);
template.tokens = parser.parse.call(template, data, _config.tags, _config.autoescape);
} catch (e) {
return new TemplateError(e);
}
Expand Down Expand Up @@ -79,11 +81,11 @@ function createTemplate(data, id) {
].join(''));

template.render = function (context, parents) {
if (config.allowErrors) {
return render.call(this, context, parents, config.filters, _);
if (_config.allowErrors) {
return render.call(this, context, parents, _config.filters, _);
} else {
try {
return render.call(this, context, parents, config.filters, _);
return render.call(this, context, parents, _config.filters, _);
} catch (e) {
return new TemplateError(e);
}
Expand All @@ -107,12 +109,12 @@ exports.fromFile = function (filepath) {
}

var get = function () {
var file = ((/^\//).test(filepath)) ? filepath : config.root + '/' + filepath,
var file = ((/^\//).test(filepath)) ? filepath : _config.root + '/' + filepath,
data = fs.readFileSync(file, config.encoding);
CACHE[filepath] = createTemplate(data, filepath);
};

if (config.allowErrors) {
if (_config.allowErrors) {
get();
} else {
try {
Expand Down

0 comments on commit 0e871e2

Please sign in to comment.