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 tagFile = nodePath.join(dir, childFilename, 'marko-tag.json');
|
||||||
var tag = null;
|
var tag = null;
|
||||||
var rendererFile = nodePath.join(dir, childFilename, 'renderer.js');
|
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 templateFile = nodePath.join(dir, childFilename, 'template.marko');
|
||||||
var tagDef = null;
|
var tagDef = null;
|
||||||
|
|
||||||
@ -284,6 +285,8 @@ function scanTagsDir(tagsConfigPath, tagsConfigDirname, dir, taglib) {
|
|||||||
if (!tagDef.renderer && !tagDef.template) {
|
if (!tagDef.renderer && !tagDef.template) {
|
||||||
if (fs.existsSync(rendererFile)) {
|
if (fs.existsSync(rendererFile)) {
|
||||||
tagDef.renderer = rendererFile;
|
tagDef.renderer = rendererFile;
|
||||||
|
} else if (fs.existsSync(indexFile)) {
|
||||||
|
tagDef.renderer = indexFile;
|
||||||
} else if (fs.existsSync(templateFile)) {
|
} else if (fs.existsSync(templateFile)) {
|
||||||
tagDef.template = templateFile;
|
tagDef.template = templateFile;
|
||||||
} else if (fs.existsSync(templateFile + ".html")) {
|
} else if (fs.existsSync(templateFile + ".html")) {
|
||||||
@ -298,19 +301,12 @@ function scanTagsDir(tagsConfigPath, tagsConfigDirname, dir, taglib) {
|
|||||||
taglib.addTag(tag);
|
taglib.addTag(tag);
|
||||||
} else {
|
} else {
|
||||||
// marko-tag.json does *not* exist... checking for a 'renderer.js'
|
// marko-tag.json does *not* exist... checking for a 'renderer.js'
|
||||||
|
var rendererJSFile;
|
||||||
|
|
||||||
if (fs.existsSync(rendererFile)) {
|
if (fs.existsSync(rendererFile)) {
|
||||||
var rendererCode = fs.readFileSync(rendererFile, {encoding: 'utf8'});
|
rendererJSFile = rendererFile;
|
||||||
tagDef = tagDefFromCode.extractTagDef(rendererCode);
|
} else if (fs.existsSync(indexFile)) {
|
||||||
if (!tagDef) {
|
rendererJSFile = indexFile;
|
||||||
tagDef = createDefaultTagDef();
|
|
||||||
}
|
|
||||||
|
|
||||||
tagDef.renderer = rendererFile;
|
|
||||||
tag = buildTag(tagDef, tagsConfigPath, taglib, tagDirname);
|
|
||||||
tag.name = childFilename;
|
|
||||||
taglib.addTag(tag);
|
|
||||||
} else {
|
} else {
|
||||||
var exTemplateFile;
|
var exTemplateFile;
|
||||||
if (fs.existsSync(templateFile)) {
|
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) {
|
if (tagDef) {
|
||||||
tag = buildTag(tagDef, tagsConfigPath, taglib, tagDirname);
|
tag = buildTag(tagDef, tagsConfigPath, taglib, tagDirname);
|
||||||
tag.name = childFilename;
|
tag.name = childFilename;
|
||||||
|
|||||||
@ -88,6 +88,10 @@ module.exports = {
|
|||||||
a: attr,
|
a: attr,
|
||||||
|
|
||||||
as: attrs,
|
as: attrs,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Loads a template
|
||||||
|
*/
|
||||||
l: function(path) {
|
l: function(path) {
|
||||||
if (typeof path === 'string') {
|
if (typeof path === 'string') {
|
||||||
if (markoRegExp.test(path)) {
|
if (markoRegExp.test(path)) {
|
||||||
@ -102,47 +106,60 @@ module.exports = {
|
|||||||
return runtime.load(path);
|
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) {
|
return renderFunc;
|
||||||
if (!props) {
|
},
|
||||||
props = {};
|
|
||||||
|
// ----------------------------------
|
||||||
|
// Helpers that require an out below:
|
||||||
|
// ----------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Invoke a tag handler render function
|
||||||
|
*/
|
||||||
|
t: function (out, renderFunc, input, body) {
|
||||||
|
if (!input) {
|
||||||
|
input = {};
|
||||||
}
|
}
|
||||||
|
|
||||||
if (body) {
|
if (body) {
|
||||||
props.invokeBody = body;
|
input.invokeBody = body;
|
||||||
}
|
}
|
||||||
|
|
||||||
var func;
|
renderFunc(input, out);
|
||||||
|
|
||||||
if (!(func = handler.process || handler.render)) {
|
|
||||||
if (typeof handler === 'function') {
|
|
||||||
func = handler;
|
|
||||||
} else {
|
|
||||||
throw new Error('Invalid handler: ' + handler);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func.call(handler, props, context);
|
|
||||||
},
|
},
|
||||||
c: function (context, func) {
|
c: function (out, func) {
|
||||||
var output = context.captureString(func);
|
var output = out.captureString(func);
|
||||||
return {
|
return {
|
||||||
toString: function () {
|
toString: function () {
|
||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
i: function(context, path, data) {
|
i: function(out, path, data) {
|
||||||
if (!path) {
|
if (!path) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (typeof path === 'string') {
|
if (typeof path === 'string') {
|
||||||
runtime.render(path, data, context);
|
runtime.render(path, data, out);
|
||||||
} else if (typeof path.render === 'function') {
|
} else if (typeof path.render === 'function') {
|
||||||
path.render(data, context);
|
path.render(data, out);
|
||||||
} else {
|
} else {
|
||||||
throw new Error('Invalid template');
|
throw new Error('Invalid template');
|
||||||
}
|
}
|
||||||
|
|||||||
@ -188,26 +188,6 @@ if (stream) {
|
|||||||
require('raptor-util/inherit')(Readable, stream.Readable);
|
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) {
|
function load(templatePath, options) {
|
||||||
var cache = exports.cache;
|
var cache = exports.cache;
|
||||||
|
|
||||||
@ -226,14 +206,14 @@ function load(templatePath, options) {
|
|||||||
// as the first argument to the factory function to produce
|
// as the first argument to the factory function to produce
|
||||||
// the compiled template function
|
// the compiled template function
|
||||||
template = cache[templatePath] = new Template(
|
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);
|
options);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Instead of a path, assume we got a compiled template module
|
// Instead of a path, assume we got a compiled template module
|
||||||
// We store the loaded template with the factory function that was
|
// We store the loaded template with the factory function that was
|
||||||
// used to get access to the compiled template function
|
// 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;
|
return template;
|
||||||
|
|||||||
@ -23,7 +23,7 @@ function addHandlerVar(template, renderer) {
|
|||||||
var handlerVar = handlerVars[renderer];
|
var handlerVar = handlerVars[renderer];
|
||||||
if (!handlerVar) {
|
if (!handlerVar) {
|
||||||
handlerVar = renderer.replace(/[.\-\/\\]/g, '_').replace(/^[_]+/g, '');
|
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;
|
handlerVars[renderer] = handlerVar;
|
||||||
}
|
}
|
||||||
return handlerVar;
|
return handlerVar;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user