使用 StringTemplate 4 将一个模板嵌套在另一个模板中

发布于 2024-12-21 20:23:16 字数 736 浏览 4 评论 0原文

我在理解如何将另一个模板嵌套在另一个模板中时遇到问题。例如,我有 3 个模板:application.stgwrapper.stgcore.stg

core.stg:

page(data1, data2) ::= <<
  <h1>$data1$</h1>
  <h1>$data2$</h1>
>>

wrapper.stg:

page(data3, data4) ::= <<
  <h1>$data3$</h1>

  <!----- CORE.STG GOES HERE ------->

  <h1>$data4$</h1>
>>

application.stg:

   page(data7, data8) ::= <<
      <h1>$data7$</h1>

      <!----- WRAPPER.STG GOES HERE ------->

      <h1>$data8$</h1>
    >>

文档似乎暗示这是可能的(条件子模板部分),但抱歉,我根本无法弄清楚语法。请帮帮我。

I am having problem understanding how to nest another template inside another. For example, I have 3 templates: application.stg, wrapper.stg and core.stg.

core.stg:

page(data1, data2) ::= <<
  <h1>$data1
lt;/h1>
  <h1>$data2
lt;/h1>
>>

wrapper.stg:

page(data3, data4) ::= <<
  <h1>$data3
lt;/h1>

  <!----- CORE.STG GOES HERE ------->

  <h1>$data4
lt;/h1>
>>

application.stg:

   page(data7, data8) ::= <<
      <h1>$data7
lt;/h1>

      <!----- WRAPPER.STG GOES HERE ------->

      <h1>$data8
lt;/h1>
    >>

The documentation seems to hint that this is possible (Conditional Subtemplate section), but sorry, I could not figure out the syntax at all. Please help me out.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半﹌身腐败 2024-12-28 20:23:16

您可以使用每个模板创建一个完整文件,并确保为其设置 .stg 扩展名。
然后创建一个 TemplateGroupFile 对象并设置属性 data7data8 并告诉它在应用程序模板中启动。

core(data1, data2) ::= <<
  <h1>$data1
lt;/h1>
  <h1>$data2
lt;/h1>
>>

wrapper(data3, data4) ::= <<
  <h1>$data3
lt;/h1>
  core(data3,data4)
  <h1>$data4
lt;/h1>
>>

application(data7, data8) ::= <<
  <h1>$data7
lt;/h1>
  wrapper(data7,data8)
  <h1>$data8
lt;/h1>
>>

这是 C# 代码

TemplateGroupFile grp = new TemplateGroupFile(Template);
Template page_tpl = grp.GetInstanceOf(GroupName);
GroupName = "application";
page_tpl.Add("data7", yourData);
page_tpl.Add("data8", moreOfYourData);
File.WriteAllText(Path.Combine(dir, Path.GetFileName(outputFile)), page_tpl.Render());

You can create a whole file with every template and make sure you set the .stg extension to it.
Then create a TemplateGroupFile object and set the properties data7 and data8 and tell it to start in the application template.

core(data1, data2) ::= <<
  <h1>$data1
lt;/h1>
  <h1>$data2
lt;/h1>
>>

wrapper(data3, data4) ::= <<
  <h1>$data3
lt;/h1>
  core(data3,data4)
  <h1>$data4
lt;/h1>
>>

application(data7, data8) ::= <<
  <h1>$data7
lt;/h1>
  wrapper(data7,data8)
  <h1>$data8
lt;/h1>
>>

This is the C# code

TemplateGroupFile grp = new TemplateGroupFile(Template);
Template page_tpl = grp.GetInstanceOf(GroupName);
GroupName = "application";
page_tpl.Add("data7", yourData);
page_tpl.Add("data8", moreOfYourData);
File.WriteAllText(Path.Combine(dir, Path.GetFileName(outputFile)), page_tpl.Render());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文