12

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

我有一个简单的业务对象列表(比如说订单),我希望它们显示在我的 html 模板内的 UL 标记中。

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

<html> some text <ul>[Order template]<li>[Order name here]</li>[/Order template]</ul></html>

我希望我的输出是:

<html> some text <ul><li>Order 1</li><li>Order 2</li>...</ul></html>

我不知道如何使用 StringTemplate 来完成这项工作。有任何想法吗?

4

2 回答 2

29

您应该使用以下语法:

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

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

于 2009-05-11T09:52:31.107 回答
0

这也适用于我。如果您从 Antlr 调用 StringTemplate 作为 StringTemplateGroup,则语法略有不同。将 $ 替换为 <>。

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>;
}
}>
}

>>
于 2010-03-23T17:17:08.617 回答