12

I've just started using StringTemplate in my C# project. I went through the documentation, but I can't seem to find a way to implement this simple scenario:

I have a list of simple business objects (let's say Orders) and I want them displayed inside a UL tag inside my html template.

So, my .st template looks like this (pseudo-code):

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

and I want my output to be:

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

I can't figure out how to make this work using StringTemplate. Any ideas?

Alt_Doru
  • 335
  • 4
  • 13
  • There is a nice post that can help you: [Localizable text template engine using StringTemplate 4](http://netmvc.blogspot.com/2012/04/localizable-text-template-engine-using_23.html) – Malkov Apr 23 '12 at 14:08

2 Answers2

29

You should use the following syntax:

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

The documentation about this feature is really hard to find, I found some info here (search for the pipe symbol |).

Ronald Wildenberg
  • 31,634
  • 14
  • 90
  • 133
0

That works for me also. If you are calling StringTemplate from Antlr as a StringTemplateGroup, the syntax is a little different. Replace $ with <>.

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

>>
sebagomez
  • 9,501
  • 7
  • 51
  • 89
  • How do I get it working if for eg: "attribute.type" returns a complex object of type "Type" class. I am trying to implement the same, where the "attribute.type" returns an Object "Type", which has an attribute "typeName". I am unable to retrieve the TypeName. Please help !! – AnirbanDebnath Apr 06 '16 at 12:28