【发布时间】:2015-03-01 22:19:49
【问题描述】:
有兴趣了解一下 StringTemplate 引擎,并创建一个 HTML 页面,该页面可以在 Visual Studio 2013 中使用 C# 使用 StringTemplate。
我已经进行了相当广泛的搜索,但已经能够将一些简单的代码组合在一起,以便能够使用库,但是我收到了一个运行时错误说明。
内容[匿名]属性字符串未定义
我以为我在正确的位置和正确的文件路径中添加了分隔符,但似乎仍然出现此错误,我还是编程新手,我是否遗漏了代码中的一些明显内容?
我一直在努力寻找有关图书馆、视频示例等的各种信息。
提前致谢。
我在下面添加了代码。
static void Main(string[] args)
{
var System = new Program();
System.HelloWorld();
Console.ReadLine();
}
public string HelloWorld()
{
Template template;
var path = @"C:\TemplateTester\index.st";
var file = new FileInfo(path);
using (StreamReader reader = file.OpenText())
template = new Template(reader.ReadToEnd(), '$', '$'); // custom delimeter (defaults are "<" and ">")
template.Add("title", "Hello World!");
template.Add("array", new string[] { "Record A", "Record B", "Record C" });
template.Add("object", new { PropertyA = "A", PropertyB = "B" });
return template.Render();
}
我的 ST 文件
<html>
<body>
<h1>Hello World</h1>
$! file: C:\TemplateTester\index.st !$
String:
$string$
Array:
$array:{it|$it$ ($i$)};separator=", "$
Object:
Property A = $object.PropertyA$
Property B = $object.PropertyB$
</body>
</html>
由于我目前的声誉,我无法添加我的 FilePath 的图像,但是,ST 文件只是位于 Application 文件夹中,因为路径会与 Visual Studio 文件一起显示。程序文件夹(包含 bin、obj、属性等)和 Package 文件夹(包含 Antlr 引用)的外部。
【问题讨论】:
标签: c# template-engine stringtemplate