Search in sources :

Example 6 with IfCondNode

use of com.google.template.soy.soytree.IfCondNode in project closure-templates by google.

the class AddHtmlCommentsForDebugPass method createSoyDebug.

/**
 * Generates an AST fragment that looks like:
 *
 * <p>{@code {if debugSoyTemplateInfo()}<!--dta_of...-->{/if}}
 *
 * @param insertionLocation The location where it is being inserted
 * @param nodeIdGen The id generator to use
 * @param htmlComment The content of the HTML comment
 */
private IfNode createSoyDebug(SourceLocation insertionLocation, IdGenerator nodeIdGen, String htmlComment) {
    IfNode ifNode = new IfNode(nodeIdGen.genId(), insertionLocation);
    FunctionNode funcNode = new FunctionNode(DebugSoyTemplateInfoFunction.INSTANCE, insertionLocation);
    funcNode.setType(BoolType.getInstance());
    IfCondNode ifCondNode = new IfCondNode(nodeIdGen.genId(), insertionLocation, "if", funcNode);
    HtmlCommentNode htmlCommentNode = new HtmlCommentNode(nodeIdGen.genId(), insertionLocation);
    // We need to escape the input HTML comments, in cases the file location contains "-->".
    htmlCommentNode.addChild(new RawTextNode(nodeIdGen.genId(), htmlEscaper().escape(htmlComment), insertionLocation));
    ifCondNode.addChild(htmlCommentNode);
    ifNode.addChild(ifCondNode);
    return ifNode;
}
Also used : IfCondNode(com.google.template.soy.soytree.IfCondNode) FunctionNode(com.google.template.soy.exprtree.FunctionNode) IfNode(com.google.template.soy.soytree.IfNode) RawTextNode(com.google.template.soy.soytree.RawTextNode) HtmlCommentNode(com.google.template.soy.soytree.HtmlCommentNode)

Example 7 with IfCondNode

use of com.google.template.soy.soytree.IfCondNode in project closure-templates by google.

the class GenPyExprsVisitor method visitIfNode.

/**
 * If all the children are computable as expressions, the IfNode can be written as a ternary
 * conditional expression.
 */
@Override
protected void visitIfNode(IfNode node) {
    // Create another instance of this visitor for generating Python expressions from children.
    GenPyExprsVisitor genPyExprsVisitor = genPyExprsVisitorFactory.create(localVarExprs, errorReporter);
    TranslateToPyExprVisitor translator = new TranslateToPyExprVisitor(localVarExprs, errorReporter);
    StringBuilder pyExprTextSb = new StringBuilder();
    boolean hasElse = false;
    for (SoyNode child : node.getChildren()) {
        if (child instanceof IfCondNode) {
            IfCondNode icn = (IfCondNode) child;
            // Python ternary conditional expressions modify the order of the conditional from
            // <conditional> ? <true> : <false> to
            // <true> if <conditional> else <false>
            PyExpr condBlock = PyExprUtils.concatPyExprs(genPyExprsVisitor.exec(icn)).toPyString();
            condBlock = PyExprUtils.maybeProtect(condBlock, PyExprUtils.pyPrecedenceForOperator(Operator.CONDITIONAL));
            pyExprTextSb.append(condBlock.getText());
            // Append the conditional and if/else syntax.
            PyExpr condPyExpr = translator.exec(icn.getExpr());
            pyExprTextSb.append(" if ").append(condPyExpr.getText()).append(" else ");
        } else if (child instanceof IfElseNode) {
            hasElse = true;
            IfElseNode ien = (IfElseNode) child;
            PyExpr elseBlock = PyExprUtils.concatPyExprs(genPyExprsVisitor.exec(ien)).toPyString();
            pyExprTextSb.append(elseBlock.getText());
        } else {
            throw new AssertionError("Unexpected if child node type. Child: " + child);
        }
    }
    if (!hasElse) {
        pyExprTextSb.append("''");
    }
    // By their nature, inline'd conditionals can only contain output strings, so they can be
    // treated as a string type with a conditional precedence.
    pyExprs.add(new PyStringExpr(pyExprTextSb.toString(), PyExprUtils.pyPrecedenceForOperator(Operator.CONDITIONAL)));
}
Also used : IfElseNode(com.google.template.soy.soytree.IfElseNode) SoyNode(com.google.template.soy.soytree.SoyNode) ParentSoyNode(com.google.template.soy.soytree.SoyNode.ParentSoyNode) IfCondNode(com.google.template.soy.soytree.IfCondNode) PyExpr(com.google.template.soy.pysrc.restricted.PyExpr) PyStringExpr(com.google.template.soy.pysrc.restricted.PyStringExpr)

Example 8 with IfCondNode

use of com.google.template.soy.soytree.IfCondNode in project closure-templates by google.

the class TemplateParserTest method testParseForeachStmt.

@Test
public void testParseForeachStmt() throws Exception {
    String templateBody = "{@param goose : ?}{@param foo: ?}\n" + "  {for $goo in $goose}\n" + "    {$goose.numKids} goslings.{\\n}\n" + "  {/for}\n" + "  {for $boo in $foo.booze}\n" + "    Scary drink {$boo.name}!\n" + "    {if not isLast($boo)}{\\n}{/if}\n" + "  {ifempty}\n" + "    Sorry, no booze.\n" + "  {/for}\n";
    List<StandaloneNode> nodes = parseTemplateContent(templateBody, FAIL).getChildren();
    assertEquals(2, nodes.size());
    ForNode fn0 = (ForNode) nodes.get(0);
    assertEquals("$goose", fn0.getExpr().toSourceString());
    assertTrue(fn0.getExpr().getRoot() instanceof VarRefNode);
    assertEquals(1, fn0.numChildren());
    ForNonemptyNode fn0fnn0 = (ForNonemptyNode) fn0.getChild(0);
    assertEquals("goo", fn0fnn0.getVarName());
    assertEquals(2, fn0fnn0.numChildren());
    assertEquals("$goose.numKids", ((PrintNode) fn0fnn0.getChild(0)).getExpr().toSourceString());
    assertEquals(" goslings.\n", ((RawTextNode) fn0fnn0.getChild(1)).getRawText());
    ForNode fn1 = (ForNode) nodes.get(1);
    assertEquals("$foo.booze", fn1.getExpr().toSourceString());
    assertTrue(fn1.getExpr().getRoot() instanceof FieldAccessNode);
    assertEquals(2, fn1.numChildren());
    ForNonemptyNode fn1fnn0 = (ForNonemptyNode) fn1.getChild(0);
    assertEquals("boo", fn1fnn0.getVarName());
    assertEquals("$foo.booze", fn1fnn0.getExpr().toSourceString());
    assertEquals("boo", fn1fnn0.getVarName());
    assertEquals(4, fn1fnn0.numChildren());
    IfNode fn1fnn0in = (IfNode) fn1fnn0.getChild(3);
    assertEquals(1, fn1fnn0in.numChildren());
    assertEquals("not isLast($boo)", ((IfCondNode) fn1fnn0in.getChild(0)).getExpr().toSourceString());
    ForIfemptyNode fn1fin1 = (ForIfemptyNode) fn1.getChild(1);
    assertEquals(1, fn1fin1.numChildren());
    assertEquals("Sorry, no booze.", ((RawTextNode) fn1fin1.getChild(0)).getRawText());
}
Also used : StandaloneNode(com.google.template.soy.soytree.SoyNode.StandaloneNode) ForNode(com.google.template.soy.soytree.ForNode) IfCondNode(com.google.template.soy.soytree.IfCondNode) VarRefNode(com.google.template.soy.exprtree.VarRefNode) IfNode(com.google.template.soy.soytree.IfNode) PrintNode(com.google.template.soy.soytree.PrintNode) FieldAccessNode(com.google.template.soy.exprtree.FieldAccessNode) ForIfemptyNode(com.google.template.soy.soytree.ForIfemptyNode) ForNonemptyNode(com.google.template.soy.soytree.ForNonemptyNode) Test(org.junit.Test)

Example 9 with IfCondNode

use of com.google.template.soy.soytree.IfCondNode in project closure-templates by google.

the class SoyNodeCompiler method visitIfNode.

@Override
protected Statement visitIfNode(IfNode node) {
    List<IfBlock> ifs = new ArrayList<>();
    Optional<Statement> elseBlock = Optional.absent();
    for (SoyNode child : node.getChildren()) {
        if (child instanceof IfCondNode) {
            IfCondNode icn = (IfCondNode) child;
            SoyExpression cond = exprCompiler.compile(icn.getExpr()).coerceToBoolean();
            Statement block = visitChildrenInNewScope(icn);
            ifs.add(IfBlock.create(cond, block));
        } else {
            IfElseNode ien = (IfElseNode) child;
            elseBlock = Optional.of(visitChildrenInNewScope(ien));
        }
    }
    return ControlFlow.ifElseChain(ifs, elseBlock);
}
Also used : IfElseNode(com.google.template.soy.soytree.IfElseNode) SoyNode(com.google.template.soy.soytree.SoyNode) IfCondNode(com.google.template.soy.soytree.IfCondNode) SoyExpression(com.google.template.soy.jbcsrc.restricted.SoyExpression) Statement(com.google.template.soy.jbcsrc.restricted.Statement) ArrayList(java.util.ArrayList) IfBlock(com.google.template.soy.jbcsrc.ControlFlow.IfBlock)

Aggregations

IfCondNode (com.google.template.soy.soytree.IfCondNode)9 IfElseNode (com.google.template.soy.soytree.IfElseNode)5 SoyNode (com.google.template.soy.soytree.SoyNode)5 IfNode (com.google.template.soy.soytree.IfNode)4 ParentSoyNode (com.google.template.soy.soytree.SoyNode.ParentSoyNode)4 PrintNode (com.google.template.soy.soytree.PrintNode)3 VarRefNode (com.google.template.soy.exprtree.VarRefNode)2 CodeChunk (com.google.template.soy.jssrc.dsl.CodeChunk)2 RawTextNode (com.google.template.soy.soytree.RawTextNode)2 StandaloneNode (com.google.template.soy.soytree.SoyNode.StandaloneNode)2 ArrayList (java.util.ArrayList)2 Test (org.junit.Test)2 FieldAccessNode (com.google.template.soy.exprtree.FieldAccessNode)1 FunctionNode (com.google.template.soy.exprtree.FunctionNode)1 GreaterThanOpNode (com.google.template.soy.exprtree.OperatorNodes.GreaterThanOpNode)1 IfBlock (com.google.template.soy.jbcsrc.ControlFlow.IfBlock)1 SoyExpression (com.google.template.soy.jbcsrc.restricted.SoyExpression)1 Statement (com.google.template.soy.jbcsrc.restricted.Statement)1 WithValue (com.google.template.soy.jssrc.dsl.CodeChunk.WithValue)1 ConditionalBuilder (com.google.template.soy.jssrc.dsl.ConditionalBuilder)1