Fixes #207 - Marko v3: Re-introduce support for "body-function" in taglib

This commit is contained in:
Patrick Steele-Idem 2016-01-18 14:22:20 -07:00
parent c1dc3de3de
commit 84de017839
7 changed files with 72 additions and 40 deletions

View File

@ -15,7 +15,10 @@ function removeExt(filename) {
}
function buildInputProps(node, context) {
var tagDef = node.tagDef;
var inputProps = {};
var builder = context.builder;
node.forEachAttribute((attr) => {
var attrName = attr.name;
@ -65,8 +68,20 @@ function buildInputProps(node, context) {
});
if (node.body && node.body.length) {
var renderBodyFunction = context.builder.renderBodyFunction(node.body);
inputProps.renderBody = renderBodyFunction;
if (tagDef.bodyFunction) {
let bodyFunction = tagDef.bodyFunction;
let bodyFunctionName = bodyFunction.name;
var bodyFunctionParams = bodyFunction.params.map(function(param) {
return builder.identifier(param);
});
inputProps[bodyFunctionName] = builder.functionDeclaration(bodyFunctionName, bodyFunctionParams, node.body);
} else {
var renderBodyFunction = context.builder.renderBodyFunction(node.body);
inputProps.renderBody = renderBodyFunction;
}
}
return context.builder.literal(inputProps);

View File

@ -459,7 +459,10 @@ function loadTag(tagProps, path, taglib, dirname) {
if (!hasAttributes(tagProps)) {
// allow any attributes if no attributes are declared
tagProps.attributes = {
'*': 'string'
'*': {
type: 'string',
targetProperty: null
}
};
}

View File

@ -1,42 +1,26 @@
{
"tags": {
"layout-use": {
"renderer": "./use-tag",
"attributes": {
"template": {
"type": "template"
},
"*": {
"remove-dashes": true,
"type": "string"
}
},
"body-function": "getContent(__layoutHelper)"
"<layout-use>": {
"@template": "template",
"@*": {
"remove-dashes": true,
"type": "string"
},
"layout-put": {
"renderer": "./put-tag",
"attributes": {
"into": {
"type": "string"
},
"value": {
"type": "string"
}
},
"import-var": {
"layout": "__layoutHelper"
}
},
"layout-placeholder": {
"renderer": "./placeholder-tag",
"attributes": {
"name": {
"type": "string"
}
},
"import-var": {
"content": "data.layoutContent"
}
"renderer": "./use-tag",
"body-function": "getContent(__layoutHelper)"
},
"<layout-put>": {
"@into": "string",
"@value": "string",
"renderer": "./put-tag",
"import-var": {
"layout": "__layoutHelper"
}
},
"<layout-placeholder>": {
"@name": "string",
"renderer": "./placeholder-tag",
"import-var": {
"content": "data.layoutContent"
}
}
}

View File

@ -0,0 +1,20 @@
function create(__helpers) {
var str = __helpers.s,
empty = __helpers.e,
notEmpty = __helpers.ne,
escapeXml = __helpers.x,
__renderer = __helpers.r,
__tag = __helpers.t,
test_body_function_renderer = __renderer(require("../../../taglib/scanned-tags/test-body-function/renderer"));
return function render(data, out) {
__tag(out, test_body_function_renderer, {
"name": "World",
"myBody": function myBody(foo, bar) {
out.w("This is the body content");
}
});
};
}
(module.exports = require("marko").c(__filename)).c(create);

View File

@ -0,0 +1,3 @@
<test-body-function name="World">
This is the body content
</test-body-function>

View File

@ -0,0 +1,4 @@
{
"renderer": "./renderer",
"body-function": "myBody(foo, bar)"
}

View File

@ -0,0 +1,3 @@
module.exports = function(input, out) {
};