【问题标题】:StringTemplate - How to iterate through list of business objects and output simple html? [closed]StringTemplate - 如何遍历业务对象列表并输出简单的 html? [关闭]
【发布时间】:2009-05-11 09:17:14
【问题描述】:

我刚刚开始在我的 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 来完成这项工作。有什么想法吗?

【问题讨论】:

标签: c# stringtemplate


【解决方案1】:

您应该使用以下语法:

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

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

【讨论】:

    【解决方案2】:

    这也适用于我。如果您从 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>;
    }
    }>
    }
    
    >>
    

    【讨论】:

    • 如果例如:“attribute.type”返回类型为“Type”类的复杂对象,我如何使其工作。我正在尝试实现相同的功能,其中“attribute.type”返回一个具有属性“typeName”的对象“Type”。我无法检索 TypeName。请帮忙!!
    猜你喜欢
    • 2021-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-01
    • 2019-12-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多