我目前正在尝试在C#中使用DotLiquid,我观察到了一个我不太理解的行为。因为我不太熟悉C#,所以我不能确定我的问题是C#本身的问题,还是DotLiquid的问题,所以请耐心等待。=)
我有一个非常基本的index.liquid,我正试图将一个Table-object传递给它。为了开始,我已经覆盖了toString()来简单地创建一个表示,现在,我将在稍后使用实际的对象。在尝试使用模板时,我是这样做的:
public static void createHTML(DataTable table)
{
string templatePath = @"C:\Path\To\index.liquid";
var template = Template.Parse(templatePath);
template.Render(Hash.FromAnonymousObject(new
{
table = DataMapper.createTable(table).toString()
});
using (StreamWrite file = new StreamWriter(@"C:\Some\Path\test.html"))
{
file.write(template.Render());
}
}现在,当我打开这个新创建的test.html时,它包含的所有内容都是C:\Path\To\index.liquid,这意味着我不知何故没有正确加载我的模板。查看Try to use DotLiquid with c#,我会认为我正确地加载了模板,并且使用File.ReadAllText(templatePath));向我显示templatePath指向了正确的文件。
这表明我对Template.Parse()或Template.Render()的一些非常基础的东西并不了解,因为源代码不能为我提供我所缺少的洞察力,所以希望您能帮助我。
发布于 2018-04-21 04:27:42
希望能让其他人在这个问题上不会被绊倒。输出是文件路径的真正原因是因为Template.Parse(字符串源)需要实际的模板内容,而不是文件路径。
为了完成你正在尝试的东西,你需要这样使用它:
Template template = Template.Parse(File.ReadAllText(templatePath));发布于 2017-06-29 17:20:19
如果没有index.liquid的内容就很难判断,但是已经有一件事需要解决:调用Render两次,第二次没有对象。
试试这个:
public static void createHTML(DataTable table)
{
string templatePath = @"C:\Path\To\index.liquid";
var template = Template.Parse(templatePath);
using (StreamWrite file = new StreamWriter(@"C:\Some\Path\test.html"))
{
file.write(template.Render(Hash.FromAnonymousObject(new
{
table = DataMapper.createTable(table).toString()
})));
}
}如果这不起作用,请更新您的问题以添加index.liquid的内容。
https://stackoverflow.com/questions/44758773
复制相似问题