Allow dynamic attributes for template tags

This commit is contained in:
Patrick Steele-Idem 2014-07-22 22:29:10 -06:00
parent a63d15f26f
commit f420882fdf
7 changed files with 26 additions and 6 deletions

View File

@ -100,10 +100,10 @@ TemplateCompiler.prototype = {
var rootNode;
var templateBuilder;
function handleErrors() {
var message = 'An error occurred while trying to compile template at path "' + filePath + '". Error(s) in template: ';
var message = 'An error occurred while trying to compile template at path "' + filePath + '". Error(s) in template:\n';
var errors = _this.getErrors();
for (var i = 0, len = errors.length; i < len; i++) {
message += + '(' + (i + 1) + ') ' + (errors[i].pos ? '[' + errors[i].pos + '] ' : '') + errors[i].message + ' ';
message += (i + 1) + ') ' + (errors[i].pos ? '[' + errors[i].pos + '] ' : '') + errors[i].message + '\n';
}
var error = new Error(message);
error.errors = _this.getErrors();

View File

@ -67,8 +67,7 @@ function handleAttributes(value, parent, path) {
attrProps = {
type: 'string'
};
}
else if (typeof attrProps === 'string') {
} else if (typeof attrProps === 'string') {
attrProps = {
type: attrProps
};
@ -88,6 +87,13 @@ function buildTag(tagObject, path, taglib, dirname) {
var tag = new Taglib.Tag(taglib);
if (tagObject.attributes == null) {
// allow any attributes if no attributes are declared
tagObject.attributes = {
'*': 'string'
};
}
propertyHandlers(tagObject, {
name: function(value) {
tag.name = value;

View File

@ -416,8 +416,12 @@ module.exports = function transform(node, compiler, template) {
if (attrDef.removeDashes === true) {
name = removeDashes(name);
}
node.addDynamicAttribute(name, value);
node.setDynamicAttributesProperty(attrDef.targetProperty);
if (node.addDynamicAttribute) {
node.addDynamicAttribute(name, value);
node.setDynamicAttributesProperty(attrDef.targetProperty);
} else {
node.setProperty(name, value);
}
} else {
node.setProperty(name, value);
}

View File

@ -392,4 +392,9 @@ describe('raptor-templates/rhtml' , function() {
it("should support scanning a directory for tags", function(done) {
testRender("test-project/rhtml-templates/scanned-tags.rhtml", {}, done);
});
it("should support scanning a directory for tags", function(done) {
testRender("test-project/rhtml-templates/template-tag-dynamic-attributes.rhtml", {}, done);
});
});

View File

@ -80,6 +80,9 @@
"default-value": 100
}
}
},
"test-template-tag-dynamic-attributes": {
"template": "./hello.rhtml"
}
},
"tags-dir": "./scanned-tags"

View File

@ -0,0 +1 @@
<test-template-tag-dynamic-attributes name="Frank"/>