具有对象属性的 StringTemplate 变量
我第一次开始使用 StringTemplate,并且一直在尝试弄清楚如何让 StringTemplate 执行如下操作:
/
$elemenets:article/elements()$
elements.starticle
$if($it.is_type)$ $it:article/type()$
$elseif($it.is_type2)$ $it:article/type2()$
// also tried: $it.value:article/type2()$, same result
$endif$
/type.starticle
<type>$it.value$</type>
/type2.stprogram.cs
<h1>$it.value.title</h1>
<type2>$it.value.text</type2>
article.starticle
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();
这里的问题是...... if(it.is_type) 工作正常,而article/type.st 工作完美。但是,当我将对象传递给“Element”的 value 属性时,我收到此错误:
Class ClassName has no such attribute: text in template context [Article article/element elseif(it.is_type2)_subtemplate article/type2]
所以 - 我的问题是,如何使用 StringTemplate 在对象内访问对象的属性/字段?
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.valuelt;/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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
似乎 StringTemplate 不支持:
当我将其转换为:
它工作得很好......所以现在我的元素可以嵌套。
Appears that StringTemplate does not support:
When I converted this to:
It worked just fine ... so now my elements can nest through.