开发者

StringTemplate Variable with Object Properties

开发者 https://www.devze.com 2022-12-24 00:06 出处:网络
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:

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
开发者_JAVA技巧$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?


Appears that StringTemplate does not support:

public string name { get; set; }

When I converted this to:

public string name;

It worked just fine ... so now my elements can nest through.

0

精彩评论

暂无评论...
验证码 换一张
取 消