Comparing version 0.0.1 to 0.0.3
18
index.js
@@ -6,5 +6,15 @@ /** | ||
var Swig = require('swig').Swig; | ||
var path = require('path'); | ||
module.exports = function (res, options) { | ||
if (!res.fis) { | ||
throw new Error('must `yog-resource-api` is loaded.'); | ||
} | ||
module.exports = function (res, options) { | ||
if (!options || !options['viewdir']) { | ||
throw new Error('must set `options.viewdir`.'); | ||
} | ||
//add responseWriter to the context of swig. | ||
Swig.prototype._r = function () { | ||
@@ -14,2 +24,8 @@ return res; | ||
Swig.prototype._compileFile = function (id, w_args, opt) { | ||
opt.resolveFrom = ''; | ||
var p = path.join(options['viewdir'], res.fis.load(id)); | ||
return this.compileFile(p, opt); | ||
}; | ||
var swig = new Swig(options); | ||
@@ -16,0 +32,0 @@ |
{ | ||
"name": "yog-swig", | ||
"version": "0.0.1", | ||
"version": "0.0.3", | ||
"description": "custom swig template, add some tag or filter ", | ||
@@ -25,3 +25,6 @@ "main": "index.js", | ||
}, | ||
"homepage": "https://github.com/fex-team/yog-swig" | ||
"homepage": "https://github.com/fex-team/yog-swig", | ||
"dependencies": { | ||
"swig": "1.3.2" | ||
} | ||
} |
var exports = module.exports; | ||
/** | ||
* @alias body | ||
* @example | ||
* {%body%} something the page partial {%/body%} | ||
*/ | ||
exports.compile = function(compiler, args, content, parents, options, blockName) { | ||
@@ -4,0 +10,0 @@ content.unshift('<body>'); |
@@ -6,6 +6,6 @@ var exports = module.exports; | ||
content.push('</html>'); | ||
var code = 'var _res = _swig._r(); _res.fis.framework = _res.fis.getUri(' + args.pop() + ');' | ||
+ '_output += _res.fis.render((function() { var _output = "";' | ||
+ compiler(content, parents, options, blockName) | ||
+ 'return _output;})());'; | ||
var code = 'var _res = _swig._r(); _res.fis.framework = _res.fis.getUri(' + args.pop() + ');' + | ||
'_output += _res.fis.render((function() { var _output = "";' + | ||
compiler(content, parents, options, blockName) + | ||
'return _output;})());'; | ||
return code; | ||
@@ -12,0 +12,0 @@ }; |
var exports = module.exports; | ||
/** | ||
* trigger the load of FIS, it means add a js/css file to the page. | ||
* | ||
* @alias require | ||
* | ||
* @example | ||
* // if `namespace` = `user` | ||
* // load mod.js | ||
* {%require "user:static/mod.js"%} | ||
* | ||
* @param {string|var} id the resource `id` of the FIS system. | ||
*/ | ||
exports.compile = function(compiler, args, content, parents, options, blockName) { | ||
@@ -4,0 +16,0 @@ var code = 'var _res = _swig._r(); _res.fis.load(' + args.pop() + ');' |
var exports = module.exports; | ||
exports.compile = function(compiler, args, content, parents, options, blockName) { | ||
var code = 'var _res = _swig._r(); _res.fis.addScript((function () { var _output = "";' | ||
+ compiler(content, parents, options, blockName) | ||
+ ' return _output; })());'; | ||
var code = 'var _res = _swig._r(); _res.fis.addScript((function () { var _output = "";' + | ||
compiler(content, parents, options, blockName) + | ||
' return _output; })());'; | ||
return code; | ||
@@ -8,0 +8,0 @@ }; |
var exports = module.exports; | ||
exports.compile = function(compiler, args, content, parents, options, blockName) { | ||
var code = 'var _res = _swig._r(); _res.fis.addStyle((function () { var _output = "";' | ||
+ compiler(content, parents, options, blockName) | ||
+ ' return _output; })());'; | ||
var code = 'var _res = _swig._r(); _res.fis.addStyle((function () { var _output = "";' + | ||
compiler(content, parents, options, blockName) + | ||
' return _output; })());'; | ||
return code; | ||
@@ -8,0 +8,0 @@ }; |
@@ -1,12 +0,140 @@ | ||
var exports = module.exports; | ||
var ignore = 'ignore', | ||
missing = 'missing', | ||
only = 'only', | ||
id = "id", | ||
group = "group", | ||
mode = "mode"; | ||
exports.compile = function(compiler, args, content, parents, options, blockName) { | ||
var code = compiler(content, parents, options, blockName); | ||
return code; | ||
/** | ||
* Includes a template partial in place. The template is rendered within the current locals variable context. | ||
* | ||
* @alias widget | ||
* | ||
* @example | ||
* // food = 'burritos'; | ||
* // drink = 'lemonade'; | ||
* {% widget "./partial.html" %} | ||
* // => I like burritos and lemonade. | ||
* | ||
* @example | ||
* // my_obj = { food: 'tacos', drink: 'horchata' }; | ||
* {% widget "./partial.html" id="pagelet_id" mode="async" with my_obj%} | ||
* // => I like tacos and horchata. | ||
* | ||
* @example | ||
* {% widget "/this/file/does/not/exist" ignore missing %} | ||
* // => (Nothing! empty string) | ||
* | ||
* @param {string|var} file The path, relative to the template root, to render into the current context. | ||
* @param {literal} [with] Literally, "with". | ||
* @param {object} [context] Local variable key-value object context to provide to the included file. | ||
* @param {literal} [only] Restricts to <strong>only</strong> passing the <code>with context</code> as local variables–the included template will not be aware of any other local variables in the parent template. For best performance, usage of this option is recommended if possible. | ||
* @param {literal} [ignore missing] Will output empty string if not found instead of throwing an error. | ||
*/ | ||
exports.compile = function(compiler, args) { | ||
var file = args.shift(), | ||
onlyIdx = args.indexOf(only), | ||
onlyCtx = onlyIdx !== -1 ? args.splice(onlyIdx, 1) : false, | ||
parentFile = (args.pop() || '').replace(/\\/g, '\\\\'), | ||
ignore = args[args.length - 1] === missing ? (args.pop()) : false, | ||
w = args.filter(function(o) { | ||
return !o.k; | ||
}).join(' '), | ||
w_args = {}; | ||
args.map(function(w) { | ||
if (w.k) w_args[w.k] = w.v; | ||
}); | ||
return (ignore ? ' try {\n' : '') + | ||
'_output += _swig._compileFile(' + file + ',' + JSON.stringify(w_args) + ', {' + | ||
'resolveFrom: "' + parentFile + '"' + | ||
'})(' + | ||
((onlyCtx && w) ? w : (!w ? '_ctx' : '_utils.extend({}, _ctx, ' + w + ')')) + | ||
');\n' + | ||
(ignore ? '} catch (e) {}\n' : ''); | ||
}; | ||
exports.parse = function(str, line, parser, types) { | ||
exports.parse = function(str, line, parser, types, stack, opts) { | ||
var file, w, k; | ||
parser.on(types.STRING, function(token) { | ||
if (!file) { | ||
file = token.match; | ||
this.out.push(file); | ||
return; | ||
} | ||
var out = { | ||
v: '', | ||
k: '' | ||
}; | ||
if (k == id || k == group || k == mode) { | ||
out.v = token.match.replace(/^['"]|['"]$/g, ''); | ||
out.k = k; | ||
this.out.push(out); | ||
v = ''; //reset | ||
} | ||
}); | ||
parser.on(types.VAR, function(token) { | ||
if (!file) { | ||
k = ''; | ||
file = token.match; | ||
return true; | ||
} | ||
if (token.match === id) { | ||
k = token.match; | ||
return false; | ||
} | ||
if (token.match === mode) { | ||
k = token.match; | ||
return false; | ||
} | ||
if (token.match === group) { | ||
k = token.match; | ||
return false; | ||
} | ||
if (!w && token.match === 'with') { | ||
w = true; | ||
return; | ||
} | ||
if (w && token.match === only && this.prevToken.match !== 'with') { | ||
this.out.push(token.match); | ||
return; | ||
} | ||
if (token.match === ignore) { | ||
return false; | ||
} | ||
if (token.match === missing) { | ||
if (this.prevToken.match !== ignore) { | ||
throw new Error('Unexpected token "' + missing + '" on line ' + line + '.'); | ||
} | ||
this.out.push(token.match); | ||
return false; | ||
} | ||
if (this.prevToken.match === ignore) { | ||
throw new Error('Expected "' + missing + '" on line ' + line + ' but found "' + token.match + '".'); | ||
} | ||
return true; | ||
}); | ||
parser.on('end', function() { | ||
this.out.push(opts.filename || null); | ||
}); | ||
return true; | ||
}; | ||
exports.ends = true; | ||
exports.ends = false; |
@@ -10,2 +10,6 @@ { | ||
"type": "js" | ||
}, | ||
"ns:widget/widget01.tpl": { | ||
"uri": "/widget01.tpl", | ||
"type": "tpl" | ||
} | ||
@@ -12,0 +16,0 @@ }, |
@@ -7,5 +7,11 @@ var Swig = require('../../'); | ||
var swig = Swig(r); | ||
var swig = Swig(r, { | ||
viewdir: __dirname + '/tpls' | ||
}); | ||
console.log(swig.renderFile(__dirname + '/tpls/test01.tpl')); | ||
console.log(swig.renderFile(__dirname + '/tpls/test01.tpl', { | ||
obj_var: { | ||
title: 'it\'s a widget' | ||
} | ||
})); | ||
console.log(r); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
12225
17
278
1
+ Addedswig@1.3.2
+ Addedamdefine@1.0.1(transitive)
+ Addedasync@0.2.10(transitive)
+ Addedcamelcase@1.2.1(transitive)
+ Addeddecamelize@1.2.0(transitive)
+ Addedminimist@0.0.10(transitive)
+ Addedoptimist@0.6.1(transitive)
+ Addedsource-map@0.1.34(transitive)
+ Addedswig@1.3.2(transitive)
+ Addeduglify-js@2.4.24(transitive)
+ Addeduglify-to-browserify@1.0.2(transitive)
+ Addedwindow-size@0.1.0(transitive)
+ Addedwordwrap@0.0.20.0.3(transitive)
+ Addedyargs@3.5.4(transitive)