我有一张城市名称地图=>距离原点.
我想使用这个地图的部分,并创建这样的东西:
<ul>
<li>city1: distance1</li>
<li>city2: distance2</li>
<li>city3: distance3</li>
</ul>
使用StringTemplate执行此操作的规范方法是什么?
谢谢.
我试图运行Python/cminus示例.从http://pypi.python.org/pypi/stringtemplate3/3.1,我为python安装了stringtemplate3 sudo python setup.py install.
当我运行以此代码开头的cminus.py时.
import sys
import antlr3
import stringtemplate3
我有错误.
Traceback (most recent call last):
  File "cminus.py", line 3, in <module>
    import stringtemplate3
  File "/Library/Python/2.7/site-packages/stringtemplate3/__init__.py", line 14, in <module>
    from stringtemplate3.templates import *
  File "/Library/Python/2.7/site-packages/stringtemplate3/templates.py", line 35, in <module>
    import antlr
ImportError: No module named antlr
看起来stringtemplate3使用的是antlr而不是antlr3.
我该如何解决这个问题?
假设有以下字符串模板,会得到一个 Java Bean 对象列表:
<ul>$people:{p|<li>$p.name$ $p.email</li>}$</ul>
即人员列表可能包含Person您可能或可能没有能力增强/扩展的对象:
class Person {
    ....
    public getName() { ... }
    public getEmail() { ... }
}
该getName()和getEmail()方法不返回消毒(编码的HTML实体)。你如何解决这个问题?
在stringtemplate中处理美元符号的最佳方法是什么。
例如,我需要以下模板:
You can purchase item $name$ for $5
如果数组不为空,如何使用StringTemplate检查?
下面的示例不起作用:
<if(teams.length > 0)>
  <ul>
    <teams:{team | <li><team></li> }>
  </ul>
<endif>
其他(无效)示例:
String content = "<if(teams)>list: <teams;separator=\", \"><endif>";
ST template = new ST(content);
template.add("teams", new Long[]{123L, 124L});
System.out.println(template.render());
System.out.println("--------");
content = "<if(teams)>list: <teams;separator=\", \"><endif>";
template = new ST(content);
template.add("teams", new Long[]{});
System.out.println(template.render());
输出:
list: 123, 124
--------
list: 
以下StringTemplate给出了一个"无效字符'}'"异常,因为之后的大括号return null;:
$StatementList:{statement | 
public T $statement$(X x) { return null; }  }$
我希望有一个输出像:
public T statement1(X x) {return null; }
public T statement2(X x) {return null; }
我怎么能逃脱这个闭合的大括号?
在python中,我经常使用字符串作为模板,例如
templateUrl = '{host}/api/v3/{container}/{resourceid}'  
params = {'host': 'www.api.com', 'container': 'books', 'resourceid': 10}  
api.get(templateUrl.format(**params))  
这允许简单的基类设置等。我怎样才能在飞镖中做同样的事情?
我假设我需要创建一个实用程序函数来解析模板并手动替换,但真的希望有一些可以使用的东西。
也许是一个 TemplateString 类,其format方法采用Map名称/值对替换为字符串。
注意:目标是拥有一个通用的“格式”或“插值”功能,不需要事先知道模板中将存在哪些标签或名称。
进一步说明:模板本身在设置时并未解析。具体来说,模板在代码中的一个地方定义,然后在许多其他地方使用。
使用StringTemplate,有一个标准布局模板的正确方法是什么,例如:
<head>
..
</head>
<html>
$body()$
</html>
我可以从我的应用程序设置主体模板,以便我使用的每个模板都使用这个基本布局?
谢谢.
我在Java中使用StringTemplate.
我想以一定的精度呈现十进制数(例如小数点后的3位数).
是否有可能为ST对象做到这一点?如何?
编辑:澄清一下,这在渲染对象时尤为重要.我的代码看起来像
String renderMe(String template, Collection<MyClass> items)
{
  // render the items here using the template.... 
}
renderMe()不必了解MyClass的字段,特别是它不必知道哪些字段是浮点.我正在寻找一种能够保持这种脱钩的解决方案.
是否有任何原因或我做错了什么,为什么 RazorEngine 仅解析 100 个不同的模板就这么慢?我正在研究 StringTemplate,并执行了两个测试进行比较,如下所示。
    [Test]
    public void TestStringTemplate()
    {
        CsStopwatch stopwatch = new CsStopwatch();
        stopwatch.Start();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 100; i++)
        {
            string template = @"Hello there my name is <Name> <Surname> "  + i;
            TextParseTests.TestModel model = new TextParseTests.TestModel();
            model.Name = "Karl";
            model.Surname = "Cassar";
            Template t = new Template(template);
            t.Add("Name", model.Name);
            t.Add("Surname", model.Surname);
            var result = t.Render();
            sb.AppendLine(result);
        }
        stopwatch.Stop();
        var ms = stopwatch.ElapsedMilliseconds;
        int k = 5; …