StringTemplate Variable with Object Properties

Posted by David Higgins on Stack Overflow See other posts from Stack Overflow or by David Higgins
Published on 2010-03-25T20:29:01Z Indexed on 2010/03/25 20:33 UTC
Read the original article Hit count: 551

Filed under:
|
|

I am starting to use StringTemplate for the first time, and am stuck trying to figure out how to get StringTemplate to do something like the following:

article.st

$elemenets:article/elements()$

article/elements.st

$if($it.is_type)$ $it:article/type()$
$elseif($it.is_type2)$ $it:article/type2()$
// also tried: $it.value:article/type2()$, same result
$endif$

article/type.st

<type>$it.value$</type>

article/type2.st

<h1>$it.value.title</h1>
<type2>$it.value.text</type2>

program.cs

StringTemplateGroup group = new StringTemplateGroup("article", "Templates");
StringTemplate template = group.GetInstanceOf("Article");
template.SetAttribute("elements", new Element() { is_type = true, value = "<p>Hello Text</p>" });
template.SetAttribute("elements", new Element() { is_type2 = true, value = new { title = "Type 2 Title", text = "Type2 Text" } });
return template.ToString();

Problem here is ... the if(it.is_type) works fine, and the article/type.st works perfectly. However, when I pass an object to the value property for 'Element' I get this error:

Class ClassName has no such attribute: text in template context [Article article/element elseif(it.is_type2)_subtemplate article/type2]

So - my question is, how do i access the properties/fields of an object, within an object using StringTemplate?

© Stack Overflow or respective owner

Related posts about stringtemplate

Related posts about c#