Package org.lilystudio.smarty4j

Examples of org.lilystudio.smarty4j.Template


        .assertNull(
            "break",
            getResult("{foreach from=$array item=\"item\" key=\"key\"}{if $key==4}{break 1.0}{/if}{$key}:{$item}\n{/foreach}"));
    {
      ByteArrayOutputStream out = new ByteArrayOutputStream();
      Template template = new Template(engine, "{bytes $bytes}");
      template.merge(context, out);
      Assert.assertEquals("bytes", out.toString("UTF-8"), "测试字节数组");
    }
    Assert.assertNull("bytes", getResult("{bytes \"key\"}"));
    Assert
        .assertEquals(
View Full Code Here


            "IF STRIKE ISN'T SETTLED QUICKLY IT MAY LAST A WHILE.");
  }

  private String getResult(String data) throws Exception {
    try {
      Template template = new Template(engine, data);
      Writer writer = new StringWriter();
      template.merge(context, writer);
      return writer.toString();
    } catch (Exception e) {
      return null;
    }
  }
View Full Code Here

  @Test
  public void testBean() throws Exception {
    Context c = new Context();
    Bean bean = new Bean();
    c.putBean(bean);
    Template template = new Template(engine, "{$number}");
    Writer writer = new StringWriter();
    template.merge(c, writer);
    Assert.assertEquals("Bean导入", writer.toString(), "10");

    context.set("bean", bean);
    Assert.assertEquals("Bean导入",
        getResult("{$bean.number}{$bean.number}{$bean.aaa}{$bean.aaa}"),
View Full Code Here

  private static Template template;

  @Before
  public void setUp() throws Exception {
    template = new Template(engine, "");
    Method m = Context.class.getDeclaredMethod("setTemplate", Template.class);
    m.setAccessible(true);
    m.invoke(context, template);
  }
View Full Code Here

      }
    }

    // 生成无名模板用于解析字符串
    String text = (String) values[0];
    Template template = templates.get(text);
    if (template == null) {
      template = new Template(context.getTemplate().getEngine(), text);
      templates.put(text, template);
    }
    template.merge(context, writer);

    if (assign != null) {
      context.set((String) assign, writer.toString());
    }
  }
View Full Code Here

  /** 需要回写保存的参数名称 */
  private String[] names;

  public void init(String text, String... objects) throws Exception {
    template = new Template(SmartyResult.getEngine(), text);
    names = objects;
  }
View Full Code Here

  private int type;

  @Override
  public void execute(Context context, Writer writer, Object[] values)
      throws IOException {
    Template template = context.getTemplate();
    FileInputStream in = new FileInputStream(template.getPath(
        (String) values[0], true));
    Properties prop = new Properties();
    try {
      prop.load(in);
    } finally {
      in.close();
    }

    // 取得父容器的配置信息
    Engine engine = template.getEngine();
    Map<String, Object> config = context.getConfigures();
    Context parentContext = context.getParent();
    Map<String, Object> parent;
    if (parentContext != null) {
      parent = parentContext.getConfigures();
View Full Code Here

  }

  public void execute(HttpServletRequest request, HttpServletResponse response,
      IRelay relay) throws Exception {
    setParameters(relay);
    Template template = engine.getTemplate(relay.getRealPath(path));
    Context context = new Context();
    context.putAll(relay.getDataMap());
    response.setContentType(type);
    response.setCharacterEncoding(engine.getEncoding());
    template.merge(context, response.getWriter());
  }
View Full Code Here

    if (assign != null) {
      writer = new StringWriter();
    }

    // 加载子模板, 设置子模板的父容器
    Template template = context.getTemplate();
    String name = template.getPath((String) values[0], true);

    template = template.getEngine().getTemplate(name);
    Context childContext = new Context(context);

    int len = values.length;
    for (int i = 2; i < len; i += 2) {
      childContext.set((String) values[i], values[i + 1]);
    }

    template.merge(childContext, writer);

    if (assign != null) {
      context.set((String) assign, writer.toString());
    }
  }
View Full Code Here

    // 包名形式的路径
    else {
      path = "WEB-INF/" + path.replace('.', '/') + ext;
    }

    Template template = engine.getTemplate(path);
   
    Context ctx = new Context(); // 生成数据容器对象
    ctx.set("obj", obj);
    ctx.set("request", req);
    ctx.set("base", req.getAttribute("base"));
    ctx.set("session", req.getSession());

    template.merge(ctx, resp.getWriter());
  }
View Full Code Here

TOP

Related Classes of org.lilystudio.smarty4j.Template

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.