![JAR search and dependency download from the Maven repository](/logo.png)
com.mitchellbosecke.pebble.node.IfNode Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of pebble Show documentation
Show all versions of pebble Show documentation
Templating engine for Java.
/*******************************************************************************
* This file is part of Pebble.
*
* Copyright (c) 2014 by Mitchell Bösecke
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
******************************************************************************/
package com.mitchellbosecke.pebble.node;
import java.io.IOException;
import java.io.Writer;
import java.util.List;
import com.mitchellbosecke.pebble.error.PebbleException;
import com.mitchellbosecke.pebble.extension.NodeVisitor;
import com.mitchellbosecke.pebble.node.expression.Expression;
import com.mitchellbosecke.pebble.template.EvaluationContext;
import com.mitchellbosecke.pebble.template.PebbleTemplateImpl;
import com.mitchellbosecke.pebble.utils.Pair;
public class IfNode extends AbstractRenderableNode {
private final List, BodyNode>> conditionsWithBodies;
private final BodyNode elseBody;
public IfNode(int lineNumber, List, BodyNode>> conditionsWithBodies) {
this(lineNumber, conditionsWithBodies, null);
}
public IfNode(int lineNumber, List, BodyNode>> conditionsWithBodies, BodyNode elseBody) {
super(lineNumber);
this.conditionsWithBodies = conditionsWithBodies;
this.elseBody = elseBody;
}
@Override
public void render(PebbleTemplateImpl self, Writer writer, EvaluationContext context) throws PebbleException,
IOException {
boolean satisfied = false;
for (Pair, BodyNode> ifStatement : conditionsWithBodies) {
Object result = ifStatement.getLeft().evaluate(self, context);
if (result != null)
satisfied = (Boolean) result;
if (satisfied) {
ifStatement.getRight().render(self, writer, context);
break;
}
}
if (!satisfied && elseBody != null) {
elseBody.render(self, writer, context);
}
}
@Override
public void accept(NodeVisitor visitor) {
visitor.visit(this);
}
public List, BodyNode>> getConditionsWithBodies() {
return conditionsWithBodies;
}
public BodyNode getElseBody() {
return elseBody;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy