Search in sources :

Example 6 with CallParamNode

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

the class GenJsCodeVisitorAssistantForMsgs method genGoogMsgPlaceholder.

/**
 * Returns a code chunk for the given placeholder node.
 */
protected CodeChunk.WithValue genGoogMsgPlaceholder(MsgPlaceholderNode msgPhNode) {
    List<CodeChunk.WithValue> contentChunks = new ArrayList<>();
    for (StandaloneNode contentNode : msgPhNode.getChildren()) {
        if (contentNode instanceof MsgHtmlTagNode && !isComputableAsJsExprsVisitor.exec(contentNode)) {
            // This is a MsgHtmlTagNode that is not computable as JS expressions. Visit it to
            // generate code to define the 'htmlTag<n>' variable.
            visit(contentNode);
            contentChunks.add(id("htmlTag" + contentNode.getId()));
        } else if (contentNode instanceof CallNode) {
            // If the CallNode has any CallParamContentNode children that are not computable as JS
            // expressions, visit them to generate code to define their respective 'param<n>' variables.
            CallNode callNode = (CallNode) contentNode;
            for (CallParamNode grandchild : callNode.getChildren()) {
                if (grandchild instanceof CallParamContentNode && !isComputableAsJsExprsVisitor.exec(grandchild)) {
                    visit(grandchild);
                }
            }
            CodeChunk.WithValue call = genCallCodeUtils.gen(callNode, templateAliases, translationContext, errorReporter);
            contentChunks.add(call);
        } else {
            List<CodeChunk.WithValue> chunks = genJsExprsVisitor.exec(contentNode);
            contentChunks.add(CodeChunkUtils.concatChunks(chunks));
        }
    }
    return CodeChunkUtils.concatChunks(contentChunks);
}
Also used : StandaloneNode(com.google.template.soy.soytree.SoyNode.StandaloneNode) CallParamContentNode(com.google.template.soy.soytree.CallParamContentNode) CodeChunk(com.google.template.soy.jssrc.dsl.CodeChunk) CallParamNode(com.google.template.soy.soytree.CallParamNode) ArrayList(java.util.ArrayList) MsgHtmlTagNode(com.google.template.soy.soytree.MsgHtmlTagNode) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) CallNode(com.google.template.soy.soytree.CallNode)

Example 7 with CallParamNode

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

the class GenPyCallExprVisitor method genObjToPass.

/**
 * Generates the Python expression for the object to pass in a given call. This expression will be
 * a combination of passed data and additional content params. If both are passed, they'll be
 * combined into one dictionary.
 *
 * @param callNode The call to generate code for.
 * @return The Python expression for the object to pass in the call.
 */
public String genObjToPass(CallNode callNode) {
    TranslateToPyExprVisitor translator = new TranslateToPyExprVisitor(localVarStack, errorReporter);
    // Generate the expression for the original data to pass.
    String dataToPass;
    if (callNode.isPassingAllData()) {
        dataToPass = "data";
    } else if (callNode.isPassingData()) {
        dataToPass = translator.exec(callNode.getDataExpr()).getText();
    } else {
        dataToPass = "{}";
    }
    // Case 1: No additional params.
    if (callNode.numChildren() == 0) {
        return dataToPass;
    }
    // Build an object literal containing the additional params.
    Map<PyExpr, PyExpr> additionalParams = new LinkedHashMap<>();
    for (CallParamNode child : callNode.getChildren()) {
        PyExpr key = new PyStringExpr("'" + child.getKey().identifier() + "'");
        if (child instanceof CallParamValueNode) {
            CallParamValueNode cpvn = (CallParamValueNode) child;
            additionalParams.put(key, translator.exec(cpvn.getExpr()));
        } else {
            CallParamContentNode cpcn = (CallParamContentNode) child;
            PyExpr valuePyExpr;
            if (isComputableAsPyExprVisitor.exec(cpcn)) {
                valuePyExpr = PyExprUtils.concatPyExprs(genPyExprsVisitorFactory.create(localVarStack, errorReporter).exec(cpcn));
            } else {
                // This is a param with content that cannot be represented as Python expressions, so we
                // assume that code has been generated to define the temporary variable 'param<n>'.
                String paramExpr = "param" + cpcn.getId();
                // The param can be assumed to be a list at this point since it was created as an output
                // variable.
                valuePyExpr = new PyListExpr(paramExpr, Integer.MAX_VALUE);
            }
            // Param content nodes require a content kind in strict autoescaping, so the content must be
            // wrapped as SanitizedContent.
            valuePyExpr = InternalPyExprUtils.wrapAsSanitizedContent(cpcn.getContentKind(), valuePyExpr.toPyString());
            additionalParams.put(key, valuePyExpr);
        }
    }
    PyExpr additionalParamsExpr = PyExprUtils.convertMapToPyExpr(additionalParams);
    // Cases 2 and 3: Additional params with and without original data to pass.
    if (callNode.isPassingData()) {
        // make a shallow copy so we don't accidentally modify the param
        dataToPass = "dict(" + dataToPass + ")";
        return "runtime.merge_into_dict(" + dataToPass + ", " + additionalParamsExpr.getText() + ")";
    } else {
        return additionalParamsExpr.getText();
    }
}
Also used : PyExpr(com.google.template.soy.pysrc.restricted.PyExpr) CallParamContentNode(com.google.template.soy.soytree.CallParamContentNode) CallParamNode(com.google.template.soy.soytree.CallParamNode) PyListExpr(com.google.template.soy.pysrc.restricted.PyListExpr) CallParamValueNode(com.google.template.soy.soytree.CallParamValueNode) PyStringExpr(com.google.template.soy.pysrc.restricted.PyStringExpr) LinkedHashMap(java.util.LinkedHashMap)

Example 8 with CallParamNode

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

the class SoyNodeCompiler method prepareParamsHelper.

private Expression prepareParamsHelper(CallNode node, Label reattachPoint) {
    if (node.numChildren() == 0) {
        // Easy, just use the data attribute
        return getDataExpression(node, reattachPoint);
    } else {
        // Otherwise we need to build a dictionary from {param} statements.
        Expression paramStoreExpression = getParamStoreExpression(node, reattachPoint);
        for (CallParamNode child : node.getChildren()) {
            String paramKey = child.getKey().identifier();
            Expression valueExpr;
            if (child instanceof CallParamContentNode) {
                valueExpr = lazyClosureCompiler.compileLazyContent("param", (CallParamContentNode) child, paramKey);
            } else {
                valueExpr = lazyClosureCompiler.compileLazyExpression("param", child, paramKey, ((CallParamValueNode) child).getExpr());
            }
            // ParamStore.setField return 'this' so we can just chain the invocations together.
            paramStoreExpression = MethodRef.PARAM_STORE_SET_FIELD.invoke(paramStoreExpression, BytecodeUtils.constant(paramKey), valueExpr);
        }
        return paramStoreExpression;
    }
}
Also used : SoyExpression(com.google.template.soy.jbcsrc.restricted.SoyExpression) Expression(com.google.template.soy.jbcsrc.restricted.Expression) CallParamContentNode(com.google.template.soy.soytree.CallParamContentNode) CallParamNode(com.google.template.soy.soytree.CallParamNode) CallParamValueNode(com.google.template.soy.soytree.CallParamValueNode)

Aggregations

CallParamNode (com.google.template.soy.soytree.CallParamNode)8 CallParamContentNode (com.google.template.soy.soytree.CallParamContentNode)7 CodeChunk (com.google.template.soy.jssrc.dsl.CodeChunk)4 CallParamValueNode (com.google.template.soy.soytree.CallParamValueNode)4 ImmutableList (com.google.common.collect.ImmutableList)2 SanitizedContentKind (com.google.template.soy.base.internal.SanitizedContentKind)2 ContentKind (com.google.template.soy.data.SanitizedContent.ContentKind)1 SoyRecord (com.google.template.soy.data.SoyRecord)1 SoyValue (com.google.template.soy.data.SoyValue)1 AugmentedParamStore (com.google.template.soy.data.internal.AugmentedParamStore)1 BasicParamStore (com.google.template.soy.data.internal.BasicParamStore)1 ParamStore (com.google.template.soy.data.internal.ParamStore)1 Expression (com.google.template.soy.jbcsrc.restricted.Expression)1 SoyExpression (com.google.template.soy.jbcsrc.restricted.SoyExpression)1 PyExpr (com.google.template.soy.pysrc.restricted.PyExpr)1 PyListExpr (com.google.template.soy.pysrc.restricted.PyListExpr)1 PyStringExpr (com.google.template.soy.pysrc.restricted.PyStringExpr)1 SoyPrintDirective (com.google.template.soy.shared.restricted.SoyPrintDirective)1 CallNode (com.google.template.soy.soytree.CallNode)1 MsgHtmlTagNode (com.google.template.soy.soytree.MsgHtmlTagNode)1