Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# DotLiquid-字典<;字符串,字符串>;作为参数_C#_.net_Dictionary_Template Engine_Dotliquid - Fatal编程技术网

C# DotLiquid-字典<;字符串,字符串>;作为参数

C# DotLiquid-字典<;字符串,字符串>;作为参数,c#,.net,dictionary,template-engine,dotliquid,C#,.net,Dictionary,Template Engine,Dotliquid,假设我有以下模板: 你好{{name}}!欢迎回到{{blog}。上次登录日期:{{date} 用户可以同时输入模板和占位符/变量,因此我无法判断占位符是什么样子 我在考虑创造这样的东西: public string Render(string text, Dictionary<string,string> placeholders) { Template template = Template.Parse(text); return template.Render(

假设我有以下模板:

你好{{name}}!欢迎回到{{blog}。上次登录日期:{{date}

用户可以同时输入模板和占位符/变量,因此我无法判断占位符是什么样子

我在考虑创造这样的东西:

public string Render(string text, Dictionary<string,string> placeholders)
{
    Template template = Template.Parse(text);
    return template.Render(Hash.FromSomething(placeholders));
}
公共字符串呈现(字符串文本、字典占位符)
{
Template=Template.Parse(文本);
返回template.Render(Hash.FromSomething(占位符));
}
有一个名为FromDictionary的方法,它接受一个字典,但我真的不明白它是如何工作的。另一种选择是FromAnonymousObject,但我不知道如何将字典转换为一个匿名对象来满足这个目的


任何想法都将不胜感激

哈希。FromDictionary
确实是您想要的方法。我想你已经很接近答案了-你只需要把你的
字典
转换成
字典
。(这些值是
object
,因为除了基本类型之外,还可以在其中包含嵌套对象。)

公共字符串呈现(字符串文本、字典占位符)
{
Template=Template.Parse(文本);
字典转换占位符=
占位符.ToDictionary(kvp=>kvp.Key,kvp=>(对象)kvp.Value);
返回template.Render(Hash.FromDictionary(convertedPlaceholders));
}

(我没有编译就将此输入SO,如果有错误,请道歉。请告诉我,我会更新答案。)

谢谢。有一件事我从一开始就不明白,而且一直在做错事,那就是在字典中我用以下格式传递密钥:{{keyName}},而不仅仅是“keyName”。
public string Render(string text, Dictionary<string,string> placeholders)
{
    Template template = Template.Parse(text);
    Dictionary<string, object> convertedPlaceholders =
        placeholders.ToDictionary(kvp => kvp.Key, kvp => (object) kvp.Value);
    return template.Render(Hash.FromDictionary(convertedPlaceholders));
}