Skip to content

Commit

Permalink
- Fixed #58 {% import %} ignored after {% extends %}
Browse files Browse the repository at this point in the history
  • Loading branch information
soywiz committed Mar 6, 2013
1 parent a415b10 commit 01da342
Show file tree
Hide file tree
Showing 11 changed files with 1,549 additions and 1,367 deletions.
1,202 changes: 604 additions & 598 deletions lib/lang/DefaultTags.js

Large diffs are not rendered by default.

51 changes: 30 additions & 21 deletions lib/lang/DefaultTags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ export class ParserNodeAutoescape extends ParserNode.ParserNodeStatement {
super();
}

generateCode() {
generateCode(context: ParserNode.ParserNodeGenerateCodeContext) {
return (
'runtimeContext.autoescape(' + this.expression.generateCode() + ', function() {' +
this.inner.generateCode() +
'runtimeContext.autoescape(' + this.expression.generateCode(context) + ', function() {' +
this.inner.generateCode(context) +
'}, true);'
);
}
Expand All @@ -68,21 +68,21 @@ export class ParserNodeExpressionFilter extends ParserNode.ParserNodeExpression
});
}

generateCode() {
generateCode(context: ParserNode.ParserNodeGenerateCodeContext) {
var out = '';

this.filters.reverse().forEach((filter) => {
out += 'runtimeContext.filter(' + JSON.stringify(filter.name) + ', [';
});

out += 'runtimeContext.captureOutput(function () {'
out += this.inner.generateCode();
out += this.inner.generateCode(context);
out += '})';

this.filters.reverse().forEach((filter) => {
if (filter.parameters && filter.parameters.expressions.length > 0) {
out += ',';
out += filter.parameters.generateCode();
out += filter.parameters.generateCode(context);
}
out += '])';
});
Expand All @@ -96,8 +96,8 @@ export class ParserNodeScopeSet extends ParserNode.ParserNodeStatement {
super();
}

generateCode() {
return 'runtimeContext.scope.set(' + JSON.stringify(this.key) + ', ' + this.value.generateCode() + ');';
generateCode(context: ParserNode.ParserNodeGenerateCodeContext) {
return 'runtimeContext.scope.set(' + JSON.stringify(this.key) + ', ' + this.value.generateCode(context) + ');';
}
}

Expand All @@ -122,15 +122,15 @@ export class ParserNodeIf extends ParserNode.ParserNodeStatement {
this.conditions[this.conditions.length - 1].code.add(node);
}

generateCode() {
generateCode(context: ParserNode.ParserNodeGenerateCodeContext) {
var out = '';

for (var n = 0; n < this.conditions.length; n++) {
var condition = this.conditions[n];
if (out != '') out += 'else ';
if (condition.expression != null) out += 'if (' + condition.expression.generateCode() + ')';
if (condition.expression != null) out += 'if (' + condition.expression.generateCode(context) + ')';
out += '{ ';
out += condition.code.generateCode();
out += condition.code.generateCode(context);
out += '}';
}

Expand All @@ -143,30 +143,30 @@ export class ParserNodeFor extends ParserNode.ParserNodeStatement {
super();
}

generateCode() {
generateCode(context: ParserNode.ParserNodeGenerateCodeContext) {
var out = '';
out += ('runtimeContext.createScope((function() { ');
out += (' var list = ' + this.nodeList.generateCode() + ';');
out += (' var list = ' + this.nodeList.generateCode(context) + ';');
out += (' if (!runtimeContext.emptyList(list)) {');
out += (' runtimeContext.each(list, function(k, v) { ');
out += (' ' + (new ParserNode.ParserNodeAssignment(this.valueId, new ParserNode.ParserNodeRaw("v"))).generateCode() + ';');
out += (' ' + (new ParserNode.ParserNodeAssignment(this.valueId, new ParserNode.ParserNodeRaw("v"))).generateCode(context) + ';');
if (this.keyId !== undefined) {
out += (' ' + (new ParserNode.ParserNodeAssignment(this.keyId, new ParserNode.ParserNodeRaw("k"))).generateCode() + ';');
out += (' ' + (new ParserNode.ParserNodeAssignment(this.keyId, new ParserNode.ParserNodeRaw("k"))).generateCode(context) + ';');
}
if (this.condId) {
out += (' if (' + this.condId.generateCode() + ') { ');
} else {
out += (' if (true) { ');
}

out += this.forCode.generateCode();
out += this.forCode.generateCode(context);

out += ('}'); // if condition

out += (' });'); // each
out += ('} else {');
{
out += this.elseCode.generateCode();
out += this.elseCode.generateCode(context);
}
out += ('} '); // if/else
out += ('}));'); // createScope
Expand Down Expand Up @@ -366,7 +366,14 @@ export class DefaultTags {
checkNoMoreTokens(expressionTokenReader);

return new ParserNode.ParserNodeStatementExpression(
new ParserNode.ParserNodeAssignment(aliasNode, new ParserNode.ParserNodeRaw('runtimeContext.import(' + fileNameNode.generateCode() + ')'))
new ParserNode.ParserNodeAssignment(
aliasNode,
new ParserNode.ParserNodeContainerExpression([
new ParserNode.ParserNodeRaw('runtimeContext.import('),
fileNameNode,
new ParserNode.ParserNodeRaw(')'),
])
)
);
}

Expand Down Expand Up @@ -573,19 +580,21 @@ export class DefaultTags {

tokenParserContext.setBlock(blockName, innerNode);

return new ParserNode.ParserNodeRaw('runtimeContext.putBlock(' + JSON.stringify(blockName) + ');');
return new ParserNode.ParserNodeRaw('runtimeContext.putBlock(' + JSON.stringify(blockName) + ');', false);
}

// EXTENDS
static $extends(blockType: string, templateParser: TemplateParser.TemplateParser, tokenParserContext: TokenParserContext.TokenParserContext, templateTokenReader: TokenReader.TokenReader, expressionTokenReader: TokenReader.TokenReader) {
var expressionNode = (new ExpressionParser.ExpressionParser(expressionTokenReader, tokenParserContext)).parseExpression();
checkNoMoreTokens(expressionTokenReader);

return new ParserNode.ParserNodeContainer([
tokenParserContext.addAfterMainNode(new ParserNode.ParserNodeContainer([
new ParserNode.ParserNodeRaw('return runtimeContext.extends('),
expressionNode,
new ParserNode.ParserNodeRaw(');')
]);
]));

return new ParserNode.ParserNodeRaw('');
}

// http://twig.sensiolabs.org/doc/tags/for.html
Expand Down
Loading

0 comments on commit 01da342

Please sign in to comment.