StringTemplate - 如何遍历业务对象列表并输出简单的HTML?

12

我刚刚开始在我的C#项目中使用StringTemplate。我查阅了文档,但似乎找不到实现以下简单场景的方法:

我有一系列简单的业务对象(比如Orders),并且我想在我的html模板中将它们显示在UL标签内。

所以,我的.st模板看起来像这样(伪代码):

<html> 一些文本 <ul>[Order模板]<li>[订单名称]</li>[/Order模板]</ul></html>

而我希望输出结果是:

<html> 一些文本 <ul><li>订单1</li><li>订单2</li>...</ul></html>

我无法通过使用StringTemplate来实现这个目标,请问有什么好的解决方案吗?


有一篇很好的文章可以帮助你:使用StringTemplate 4的本地化文本模板引擎 - Malkov
2个回答

29

你应该使用以下语法:

<ul>
    $orders: {order|
        <li>Order $order.OrderId$</li>
    }$
</ul>

关于这个功能的文档真的很难找,我在这里找到了一些信息(搜索管道符号|)。


0

这对我也适用。如果你是从Antlr中作为StringTemplateGroup调用StringTemplate,语法会有一点不同。将$替换为<>。

group DTO;

assign1(m, attributes) ::= <<
package demo;
import java.io.Serializable;

public class <m> implements Serializable {
    public <m>() {
        super();
    }

<attributes : {attribute |
protected <attribute.type> <attribute.name>;

public <attribute.type> get<attribute.name>() {
    return <attribute.name>;
}

public void set<attribute.name>(<attribute.type> <attribute.name>) {
    this.<attribute.name> = <attribute.name>;
}
}>
}

>>

如果例如:"attribute.type" 返回一个类型为 "Type" 的复杂对象,我该如何让它工作?我正在尝试实现相同的功能,其中 "attribute.type" 返回一个对象 "Type",它具有属性 "typeName"。但是我无法检索 TypeName。请帮忙!! - AnirbanDebnath

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接