【问题标题】:Dictionary with dynamic entries in StringTemplateStringTemplate 中包含动态条目的字典
【发布时间】:2016-01-13 23:12:05
【问题描述】:

我在 Java 中使用 StringTemplate 4.0.8。

StringTemplate-4 documentation,它说

字典字符串也可以是可以引用属性的模板 一旦 字典值已嵌入模板中。

我该怎么做?我可以这样做吗:

output(input) ::= "The output is: <aDicitionary.input>"

aDictionary ::= [
    "someKey":"someValue",
    "someOtherKey":"someOtherValue",
    "aCertainKey": **HERE** i want the value to be <input>,
    default:"doesnt matter"
]

所以output("someKey") 的结果是The output is: someValueoutput(aCertainKey) 导致“输出为:aCertainKey”。如果是这样,语法究竟是什么样的?

我知道我可以通过在一种情况下不传递输入然后检查我是否有输入来实现相同的目的。但这会导致我在 Java 方面出现很多 if

【问题讨论】:

    标签: stringtemplate stringtemplate-4


    【解决方案1】:

    要使用动态字典条目:

    output(input) ::= <%The output is: <aDicitionary.(input)>%>
    

    在模板周围不使用引号并将input 放在括号中以对其进行评估。

    在字典中有动态内容(引用块的主题):

    aDictionary ::= [
      "someKey":"someValue",
      "someOtherKey":"someOtherValue",
      "aCertainKey": {input from scope <inputFromScope>},
      default:"doesnt matter"
    ]
    

    在里面的键和变量(或模板)引用周围使用大括号。现在打电话

    <output(input="aCertainKey", inputFromScope="myInput")>
    

    会输出

    The output is: input from scope myInput
    

    【讨论】:

    • 正是我需要的!非常感谢! :-)
    猜你喜欢
    • 2023-03-22
    • 1970-01-01
    • 1970-01-01
    • 2017-05-12
    • 2018-01-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多