Search in sources :

Example 1 with RenderException

use of com.google.template.soy.sharedpasses.render.RenderException in project closure-templates by google.

the class BaseTofu method renderMainHelper.

/**
 * Renders a template and appends the result to a StringBuilder.
 *
 * @param templateRegistry A registry of all templates.
 * @param outputBuf The Appendable to append the rendered text to.
 * @param templateName The full name of the template to render.
 * @param data The data to call the template with. Can be null if the template has no parameters.
 * @param ijData The injected data to call the template with. Can be null if not used.
 * @param activeDelPackageNames The set of active delegate package names.
 * @param msgBundle The bundle of translated messages, or null to use the messages from the Soy
 *     source.
 * @param cssRenamingMap Map for renaming selectors in 'css' tags, or null if not used.
 * @return The template that was rendered.
 */
private TemplateNode renderMainHelper(TemplateRegistry templateRegistry, Appendable outputBuf, String templateName, @Nullable SoyRecord data, @Nullable SoyRecord ijData, Predicate<String> activeDelPackageNames, @Nullable SoyMsgBundle msgBundle, @Nullable SoyIdRenamingMap idRenamingMap, @Nullable SoyCssRenamingMap cssRenamingMap, boolean debugSoyTemplateInfo) {
    TemplateNode template = templateRegistry.getBasicTemplate(templateName);
    if (template == null) {
        throw new SoyTofuException("Attempting to render undefined template '" + templateName + "'.");
    } else if (template.getVisibility() == Visibility.PRIVATE) {
        throw new SoyTofuException("Attempting to render private template '" + templateName + "'.");
    }
    if (data == null) {
        data = SoyValueConverter.EMPTY_DICT;
    }
    if (ijData == null) {
        ijData = SoyValueConverter.EMPTY_DICT;
    }
    try {
        RenderVisitor rv = new RenderVisitor(new EvalVisitorFactoryImpl(), outputBuf, templateRegistry, data, ijData, activeDelPackageNames, msgBundle, idRenamingMap, cssRenamingMap, debugSoyTemplateInfo);
        rv.exec(template);
    } catch (RenderException re) {
        throw new SoyTofuException(re);
    }
    return template;
}
Also used : TemplateNode(com.google.template.soy.soytree.TemplateNode) SoyTofuException(com.google.template.soy.tofu.SoyTofuException) RenderException(com.google.template.soy.sharedpasses.render.RenderException) EvalVisitorFactoryImpl(com.google.template.soy.sharedpasses.render.EvalVisitorFactoryImpl) RenderVisitor(com.google.template.soy.sharedpasses.render.RenderVisitor)

Example 2 with RenderException

use of com.google.template.soy.sharedpasses.render.RenderException in project closure-templates by google.

the class SimplifyExprVisitor method attemptPreeval.

// -----------------------------------------------------------------------------------------------
// Helpers.
/**
 * Attempts to preevaluate a node. If successful, the node is replaced with a new constant node in
 * the tree. If unsuccessful, the tree is not changed.
 */
private void attemptPreeval(ExprNode node) {
    // Note that we need to catch RenderException because preevaluation may fail, e.g. when
    // (a) the expression uses a bidi function that needs bidiGlobalDir to be in scope, but the
    // apiCallScope is not currently active,
    // (b) the expression uses an external function (Soy V1 syntax),
    // (c) other cases I haven't thought up.
    SoyValue preevalResult;
    try {
        preevalResult = preevalVisitor.exec(node);
    } catch (RenderException e) {
        // failed to preevaluate
        return;
    }
    PrimitiveNode newNode = InternalValueUtils.convertPrimitiveDataToExpr((PrimitiveData) preevalResult, node.getSourceLocation());
    if (newNode != null) {
        node.getParent().replaceChild(node, newNode);
    }
}
Also used : PrimitiveNode(com.google.template.soy.exprtree.ExprNode.PrimitiveNode) RenderException(com.google.template.soy.sharedpasses.render.RenderException) SoyValue(com.google.template.soy.data.SoyValue)

Aggregations

RenderException (com.google.template.soy.sharedpasses.render.RenderException)2 SoyValue (com.google.template.soy.data.SoyValue)1 PrimitiveNode (com.google.template.soy.exprtree.ExprNode.PrimitiveNode)1 EvalVisitorFactoryImpl (com.google.template.soy.sharedpasses.render.EvalVisitorFactoryImpl)1 RenderVisitor (com.google.template.soy.sharedpasses.render.RenderVisitor)1 TemplateNode (com.google.template.soy.soytree.TemplateNode)1 SoyTofuException (com.google.template.soy.tofu.SoyTofuException)1