Package com.google.template.soy.data

Examples of com.google.template.soy.data.SoyMapData


  @SuppressWarnings("ConstantConditions"// for IntelliJ
  private void visitCallNodeHelper(CallNode node, TemplateNode callee) {

    // ------ Build the call data. ------
    SoyMapData dataToPass;
    if (node.isPassingAllData()) {
      dataToPass = data;
    } else if (node.isPassingData()) {
      SoyData dataRefValue = eval(node.getDataExpr());
      if (!(dataRefValue instanceof SoyMapData)) {
        throw new RenderException(
            "In 'call' command " + node.toSourceString() +
            ", the data reference does not resolve to a SoyMapData.");
      }
      dataToPass = (SoyMapData) dataRefValue;
    } else {
      dataToPass = null;
    }

    SoyMapData callData;
    if (!node.isPassingData()) {
      // Case 1: Not passing data. Start with a fresh SoyMapData object.
      callData = new SoyMapData();
    } else if (node.numChildren() == 0) {
      // Case 2: No params. Just pass in the current data.
      callData = dataToPass;
    } else {
      // Case 3: Passing data and adding params. Need to augment the current data.
      callData = new AugmentedSoyMapData(dataToPass);
    }

    for (CallParamNode child : node.getChildren()) {

      if (child instanceof CallParamValueNode) {
        callData.putSingle(
            child.getKey(), eval(((CallParamValueNode) child).getValueExprUnion().getExpr()));

      } else if (child instanceof CallParamContentNode) {
        CallParamContentNode childCpcn = (CallParamContentNode) child;
        SoyData renderedBlock = renderBlock(childCpcn);

        // If the param node has a content kind attribute, it will have been autoescaped in the
        // corresponding context by the strict contextual autoescaper. Hence, the result of
        // evaluating the param block is wrapped in SanitizedContent of the specified kind.
        if (childCpcn.getContentKind() != null) {
          renderedBlock = UnsafeSanitizedContentOrdainer.ordainAsSafe(
              renderedBlock.stringValue(), childCpcn.getContentKind());
        }

        callData.putSingle(child.getKey(), renderedBlock);

      } else {
        throw new AssertionError();
      }
    }
View Full Code Here


        "First argument to augmentMap() function is not SoyMapData.");
    Preconditions.checkArgument(arg1 instanceof SoyMapData,
        "Second argument to augmentMap() function is not SoyMapData.");

    AugmentedSoyMapData augmentedMap = new AugmentedSoyMapData((SoyMapData) arg0);
    SoyMapData additionalMap = (SoyMapData) arg1;
    for (String key : additionalMap.getKeys()) {
      augmentedMap.putSingle(key, additionalMap.getSingle(key));
    }
    return augmentedMap;
  }
View Full Code Here

      if (nextCollectionData == null) {
        // Create the SoyData object that will be bound to keys.get(i). We need to check the first
        // part of keys[i+1] to know whether to create a SoyMapData or SoyListData (checking the
        // first char is sufficient).
        nextCollectionData =
            (Character.isDigit(keys.get(i + 1).charAt(0))) ? new SoyListData() : new SoyMapData();
        collectionData.putSingle(keys.get(i), nextCollectionData);
      }
      collectionData = nextCollectionData;
    }
View Full Code Here


  public void testComputeForTofu() {

    KeysFunction keysFunction = new KeysFunction();
    SoyData map = new SoyMapData("boo", "bar", "foo", 2, "goo", new SoyMapData("moo", 4));
    SoyData result = keysFunction.computeForTofu(ImmutableList.of(map));

    assertTrue(result instanceof SoyListData);
    SoyListData resultAsList = (SoyListData) result;
    assertEquals(3, resultAsList.length());
View Full Code Here


  public void testCompute() {

    AugmentMapFunction augmentMapFunction = new AugmentMapFunction();
    SoyMapData origMap =
        new SoyMapData("aaa", "blah", "bbb", "bleh", "ccc", new SoyMapData("xxx", 2));
    SoyMapData additionalMap =
        new SoyMapData("aaa", "bluh", "ccc", new SoyMapData("yyy", 5));
    SoyMapData augmentedMap = (SoyMapData) augmentMapFunction.compute(
        ImmutableList.<SoyData>of(origMap, additionalMap));

    assertEquals("bluh", augmentedMap.getString("aaa"));
    assertEquals("bleh", augmentedMap.getString("bbb"));
    assertEquals(5, augmentedMap.getInteger("ccc.yyy"));
    assertEquals(null, augmentedMap.get("ccc.xxx"));
  }
View Full Code Here

    SoyTofu simpleTofu = tofu.forNamespace("soy.examples.simple");

    // Example 2.
    writeExampleHeader();
    System.out.println(simpleTofu.newRenderer(".helloName")
        .setData(new SoyMapData("name", "Ana")).render());

    // Example 3.
    writeExampleHeader();
    System.out.println(simpleTofu.newRenderer(".helloNames")
        .setData(new SoyMapData("names", new SoyListData("Bob", "Cid", "Dee")))
        .render());
  }
View Full Code Here

    writeExampleHeader("demoRawTextCommands");
    System.out.println(tofu.newRenderer(DEMO_RAW_TEXT_COMMANDS).setMsgBundle(msgBundle).render());

    writeExampleHeader("demoPrint");
    System.out.println(tofu.newRenderer(DEMO_PRINT)
        .setData(new SoyMapData(DEMO_PRINT.BOO, "Boo!", DEMO_PRINT.TWO, 2))
        .setMsgBundle(msgBundle)
        .render());

    writeExampleHeader("demoPrintDirectives");
    System.out.println(tofu.newRenderer(DEMO_PRINT_DIRECTIVES)
        .setData(ImmutableMap.of(DEMO_PRINT_DIRECTIVES.LONG_VAR_NAME,
                                      "thisIsSomeRidiculouslyLongVariableName",
                                 DEMO_PRINT_DIRECTIVES.ELEMENT_ID, "my_element_id",
                                 DEMO_PRINT_DIRECTIVES.CSS_CLASS, "my_css_class"))
         .setMsgBundle(msgBundle)
         .render());

    writeExampleHeader("demoAutoescapeTrue");
    System.out.println(tofu.newRenderer(DEMO_AUTOESCAPE_TRUE)
        .setData(new SoyMapData(DEMO_AUTOESCAPE_TRUE.ITALIC_HTML, "<i>italic</i>"))
        .setMsgBundle(msgBundle)
        .render());

    writeExampleHeader("demoAutoescapeFalse");
    System.out.println(tofu.newRenderer(DEMO_AUTOESCAPE_FALSE)
        .setData(new SoyMapData(DEMO_AUTOESCAPE_FALSE.ITALIC_HTML, "<i>italic</i>"))
        .setMsgBundle(msgBundle)
        .render());

    writeExampleHeader("demoMsg");
    System.out.println(tofu.newRenderer(DEMO_MSG)
        .setData(ImmutableMap.of(DEMO_MSG.NAME, "Ed",
                                 DEMO_MSG.LABS_URL, "http://labs.google.com"))
        .setMsgBundle(msgBundle)
        .render());

    writeExampleHeader("demoIf");
    System.out.println(tofu.newRenderer(DEMO_IF)
        .setData(new SoyMapData("pi", 3.14159)).setMsgBundle(msgBundle).render());
    System.out.println(tofu.newRenderer(DEMO_IF)
        .setData(new SoyMapData("pi", 2.71828)).setMsgBundle(msgBundle).render());
    System.out.println(tofu.newRenderer(DEMO_IF)
        .setData(new SoyMapData("pi", 1.61803)).setMsgBundle(msgBundle).render());

    writeExampleHeader("demoSwitch");
    System.out.println(tofu.newRenderer(DEMO_SWITCH)
        .setData(ImmutableMap.of("name", "Fay")).setMsgBundle(msgBundle).render());
    System.out.println(tofu.newRenderer(DEMO_SWITCH)
        .setData(ImmutableMap.of("name", "Go")).setMsgBundle(msgBundle).render());
    System.out.println(tofu.newRenderer(DEMO_SWITCH)
        .setData(ImmutableMap.of("name", "Hal")).setMsgBundle(msgBundle).render());
    System.out.println(tofu.newRenderer(DEMO_SWITCH)
        .setData(ImmutableMap.of("name", "Ivy")).setMsgBundle(msgBundle).render());

    writeExampleHeader("demoForeach");
    SoyListData persons = new SoyListData();
    persons.add(new SoyMapData("name", "Jen", "numWaffles", 1));
    persons.add(new SoyMapData("name", "Kai", "numWaffles", 3));
    persons.add(new SoyMapData("name", "Lex", "numWaffles", 1));
    persons.add(new SoyMapData("name", "Mel", "numWaffles", 2));
    System.out.println(tofu.newRenderer(DEMO_FOREACH)
        .setData(new SoyMapData(DEMO_FOREACH.PERSONS, persons))
        .setMsgBundle(msgBundle)
        .render());

    writeExampleHeader("demoFor");
    System.out.println(tofu.newRenderer(DEMO_FOR)
        .setData(new SoyMapData(DEMO_FOR.NUM_LINES, 3))
        .setMsgBundle(msgBundle)
        .render());

    writeExampleHeader("demoCallWithoutParam");
    System.out.println(tofu.newRenderer(DEMO_CALL_WITHOUT_PARAM)
        .setData(new SoyMapData(
                     DEMO_CALL_WITHOUT_PARAM.NAME, "Neo",
                     DEMO_CALL_WITHOUT_PARAM.TRIP_INFO,
                         new SoyMapData("name", "Neo", "destination", "The Matrix")))
        .setMsgBundle(msgBundle)
        .render());

    writeExampleHeader("demoCallWithParam");
    System.out.println(tofu.newRenderer(DEMO_CALL_WITH_PARAM)
        .setData(ImmutableMap.of(
                 DEMO_CALL_WITH_PARAM.NAME, "Oz",
                 DEMO_CALL_WITH_PARAM.COMPANION_NAME, "Pip",
                 DEMO_CALL_WITH_PARAM.DESTINATIONS,
                 ImmutableList.of("Gillikin Country", "Munchkin Country",
                                  "Quadling Country", "Winkie Country")))
        .setMsgBundle(msgBundle)
        .render());

    writeExampleHeader("demoCallWithParamBlock");
    System.out.println(tofu.newRenderer(DEMO_CALL_WITH_PARAM_BLOCK)
        .setData(new SoyMapData(DEMO_CALL_WITH_PARAM_BLOCK.NAME, "Quo"))
        .setMsgBundle(msgBundle)
        .render());

    writeExampleHeader("demoExpressions");
    SoyListData students = new SoyListData();
    students.add(new SoyMapData("name", "Rob", "major", "Physics", "year", 1999));
    students.add(new SoyMapData("name", "Sha", "major", "Finance", "year", 1980));
    students.add(new SoyMapData("name", "Tim", "major", "Engineering", "year", 2005));
    students.add(new SoyMapData("name", "Uma", "major", "Biology", "year", 1972));
    System.out.println(tofu.newRenderer(DEMO_EXPRESSIONS)
        .setData(new SoyMapData(DEMO_EXPRESSIONS.STUDENTS, students,
                                DEMO_EXPRESSIONS.CURRENT_YEAR, 2008))
        .setMsgBundle(msgBundle)
        .render());

    writeExampleHeader("demoDoubleBraces");
View Full Code Here

     * @param obj The object to convert.
     * @return The created SoyMapData.
     */
    public static SoyMapData objectToSoyDataMap(Object obj) {
      if (obj == null) {
        return new SoyMapData();
      }
      if (obj instanceof SoyMapData) {
        return (SoyMapData) obj;
      }
      return new SoyMapData(toSoyCompatibleMap(obj));
    }
View Full Code Here

     * @return A new SoyMapData object containing data from both source.
     */
    public static SoyMapData mergeSoyMapData(SoyMapData s1, SoyMapData s2) {
        Preconditions.checkNotNull(s1);
        Preconditions.checkNotNull(s2);
        SoyMapData merged = new SoyMapData();
        for (String key: s1.getKeys()) {
            merged.putSingle(key, s1.getSingle(key));
        }
        for (String key: s2.getKeys()) {
            merged.putSingle(key, s2.getSingle(key));
        }
        return merged;
    }
View Full Code Here


            final Locale locale = config.getLocaleResolver().resolveLocale(req);
            final String templateName = path;
           
            SoyMapData model = config.getModelResolver().resolveModel(req);
            SoyMapData globals = null;
            if (config.getRuntimeGlobalsResolver() != null) {
                globals = config.getRuntimeGlobalsResolver().resolveGlobals(req);
            }
           
            // FUTURE: A mime type resolver and character type encoding?
View Full Code Here

TOP

Related Classes of com.google.template.soy.data.SoyMapData

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.