mirror of
https://github.com/marko-js/marko.git
synced 2025-12-08 19:26:05 +00:00
Fixed #33
This commit is contained in:
parent
e286b4805a
commit
4d9cdfa706
@ -271,6 +271,7 @@ function scanTagsDir(tagsConfigPath, tagsConfigDirname, dir, taglib) {
|
||||
var tagFile = nodePath.join(dir, childFilename, 'marko-tag.json');
|
||||
var tag = null;
|
||||
var rendererFile = nodePath.join(dir, childFilename, 'renderer.js');
|
||||
var indexFile = nodePath.join(dir, childFilename, 'index.js');
|
||||
var templateFile = nodePath.join(dir, childFilename, 'template.marko');
|
||||
var tagDef = null;
|
||||
|
||||
@ -284,6 +285,8 @@ function scanTagsDir(tagsConfigPath, tagsConfigDirname, dir, taglib) {
|
||||
if (!tagDef.renderer && !tagDef.template) {
|
||||
if (fs.existsSync(rendererFile)) {
|
||||
tagDef.renderer = rendererFile;
|
||||
} else if (fs.existsSync(indexFile)) {
|
||||
tagDef.renderer = indexFile;
|
||||
} else if (fs.existsSync(templateFile)) {
|
||||
tagDef.template = templateFile;
|
||||
} else if (fs.existsSync(templateFile + ".html")) {
|
||||
@ -298,19 +301,12 @@ function scanTagsDir(tagsConfigPath, tagsConfigDirname, dir, taglib) {
|
||||
taglib.addTag(tag);
|
||||
} else {
|
||||
// marko-tag.json does *not* exist... checking for a 'renderer.js'
|
||||
|
||||
var rendererJSFile;
|
||||
|
||||
if (fs.existsSync(rendererFile)) {
|
||||
var rendererCode = fs.readFileSync(rendererFile, {encoding: 'utf8'});
|
||||
tagDef = tagDefFromCode.extractTagDef(rendererCode);
|
||||
if (!tagDef) {
|
||||
tagDef = createDefaultTagDef();
|
||||
}
|
||||
|
||||
tagDef.renderer = rendererFile;
|
||||
tag = buildTag(tagDef, tagsConfigPath, taglib, tagDirname);
|
||||
tag.name = childFilename;
|
||||
taglib.addTag(tag);
|
||||
rendererJSFile = rendererFile;
|
||||
} else if (fs.existsSync(indexFile)) {
|
||||
rendererJSFile = indexFile;
|
||||
} else {
|
||||
var exTemplateFile;
|
||||
if (fs.existsSync(templateFile)) {
|
||||
@ -330,6 +326,19 @@ function scanTagsDir(tagsConfigPath, tagsConfigDirname, dir, taglib) {
|
||||
}
|
||||
}
|
||||
|
||||
if (rendererJSFile) {
|
||||
var rendererCode = fs.readFileSync(rendererJSFile, {encoding: 'utf8'});
|
||||
tagDef = tagDefFromCode.extractTagDef(rendererCode);
|
||||
if (!tagDef) {
|
||||
tagDef = createDefaultTagDef();
|
||||
}
|
||||
|
||||
tagDef.renderer = rendererJSFile;
|
||||
tag = buildTag(tagDef, tagsConfigPath, taglib, tagDirname);
|
||||
tag.name = childFilename;
|
||||
taglib.addTag(tag);
|
||||
}
|
||||
|
||||
if (tagDef) {
|
||||
tag = buildTag(tagDef, tagsConfigPath, taglib, tagDirname);
|
||||
tag.name = childFilename;
|
||||
|
||||
@ -88,6 +88,10 @@ module.exports = {
|
||||
a: attr,
|
||||
|
||||
as: attrs,
|
||||
|
||||
/**
|
||||
* Loads a template
|
||||
*/
|
||||
l: function(path) {
|
||||
if (typeof path === 'string') {
|
||||
if (markoRegExp.test(path)) {
|
||||
@ -102,47 +106,60 @@ module.exports = {
|
||||
return runtime.load(path);
|
||||
}
|
||||
},
|
||||
/**
|
||||
* Returns the render function for a tag handler
|
||||
*/
|
||||
r: function(handler) {
|
||||
var renderFunc;
|
||||
if (typeof handler === 'function') {
|
||||
renderFunc = handler;
|
||||
} else {
|
||||
renderFunc = handler.renderer || handler.render;
|
||||
}
|
||||
|
||||
/* Helpers that require a context below: */
|
||||
if (typeof renderFunc !== 'function') {
|
||||
throw new Error('Invalid handler: ' + handler);
|
||||
}
|
||||
|
||||
t: function (context, handler, props, body) {
|
||||
if (!props) {
|
||||
props = {};
|
||||
return renderFunc;
|
||||
},
|
||||
|
||||
// ----------------------------------
|
||||
// Helpers that require an out below:
|
||||
// ----------------------------------
|
||||
|
||||
|
||||
/**
|
||||
* Invoke a tag handler render function
|
||||
*/
|
||||
t: function (out, renderFunc, input, body) {
|
||||
if (!input) {
|
||||
input = {};
|
||||
}
|
||||
|
||||
if (body) {
|
||||
props.invokeBody = body;
|
||||
input.invokeBody = body;
|
||||
}
|
||||
|
||||
var func;
|
||||
|
||||
if (!(func = handler.process || handler.render)) {
|
||||
if (typeof handler === 'function') {
|
||||
func = handler;
|
||||
} else {
|
||||
throw new Error('Invalid handler: ' + handler);
|
||||
}
|
||||
}
|
||||
|
||||
func.call(handler, props, context);
|
||||
renderFunc(input, out);
|
||||
},
|
||||
c: function (context, func) {
|
||||
var output = context.captureString(func);
|
||||
c: function (out, func) {
|
||||
var output = out.captureString(func);
|
||||
return {
|
||||
toString: function () {
|
||||
return output;
|
||||
}
|
||||
};
|
||||
},
|
||||
i: function(context, path, data) {
|
||||
i: function(out, path, data) {
|
||||
if (!path) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (typeof path === 'string') {
|
||||
runtime.render(path, data, context);
|
||||
runtime.render(path, data, out);
|
||||
} else if (typeof path.render === 'function') {
|
||||
path.render(data, context);
|
||||
path.render(data, out);
|
||||
} else {
|
||||
throw new Error('Invalid template');
|
||||
}
|
||||
|
||||
@ -188,26 +188,6 @@ if (stream) {
|
||||
require('raptor-util/inherit')(Readable, stream.Readable);
|
||||
}
|
||||
|
||||
/**
|
||||
* NOTE: This method can be removed in the very near future.
|
||||
* It is only needed to make sure old templates that compiled
|
||||
* to `module.exports = function(helpers) { ... }` will still
|
||||
* load correctly even though new templates are compiled to
|
||||
* `exports.create = function(helpers) { ... }`
|
||||
*
|
||||
*/
|
||||
function wrapLegacyCompiledTemplate(loadedTemplate) {
|
||||
if (typeof loadedTemplate === 'function') {
|
||||
return {
|
||||
create: function(helpers) {
|
||||
return loadedTemplate(helpers);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
return loadedTemplate;
|
||||
}
|
||||
|
||||
function load(templatePath, options) {
|
||||
var cache = exports.cache;
|
||||
|
||||
@ -226,14 +206,14 @@ function load(templatePath, options) {
|
||||
// as the first argument to the factory function to produce
|
||||
// the compiled template function
|
||||
template = cache[templatePath] = new Template(
|
||||
wrapLegacyCompiledTemplate(loader(templatePath)).create(helpers), // Load the template factory and invoke it
|
||||
loader(templatePath).create(helpers), // Load the template factory and invoke it
|
||||
options);
|
||||
}
|
||||
} else {
|
||||
// Instead of a path, assume we got a compiled template module
|
||||
// We store the loaded template with the factory function that was
|
||||
// used to get access to the compiled template function
|
||||
template = templatePath._ || (templatePath._ = new Template(wrapLegacyCompiledTemplate(templatePath).create(helpers), options));
|
||||
template = templatePath._ || (templatePath._ = new Template(templatePath.create(helpers), options));
|
||||
}
|
||||
|
||||
return template;
|
||||
|
||||
@ -23,7 +23,7 @@ function addHandlerVar(template, renderer) {
|
||||
var handlerVar = handlerVars[renderer];
|
||||
if (!handlerVar) {
|
||||
handlerVar = renderer.replace(/[.\-\/\\]/g, '_').replace(/^[_]+/g, '');
|
||||
handlerVar = template.addStaticVar(handlerVar, 'require(' + stringify(renderer) + ')');
|
||||
handlerVar = template.addStaticVar(handlerVar, '__helpers.r(require(' + stringify(renderer) + '))');
|
||||
handlerVars[renderer] = handlerVar;
|
||||
}
|
||||
return handlerVar;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user