{"id":4731,"date":"2021-03-22T18:33:07","date_gmt":"2021-03-22T17:33:07","guid":{"rendered":"https:\/\/solidt.eu\/site\/?p=4731"},"modified":"2022-07-25T08:31:51","modified_gmt":"2022-07-25T07:31:51","slug":"c-dotliquid-string-templates","status":"publish","type":"post","link":"https:\/\/solidt.eu\/site\/c-dotliquid-string-templates\/","title":{"rendered":"C# Dotliquid string templates"},"content":{"rendered":"\n<p>Language syntax documentation: <a href=\"https:\/\/shopify.github.io\/liquid\/\">https:\/\/shopify.github.io\/liquid\/<\/a><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ Create a Hash, containing all data usable by the template\nvar json = JsonConvert.SerializeObject(model);\nvar dictionary = JsonConvert.DeserializeObject&lt;Dictionary&lt;string, object>>(json, new DictionaryConverter());\nvar hash = Hash.FromDictionary(dictionary);\n\nvar template = Template.Parse(TemplateString);  \/\/ Parses and compiles the template\nvar content = template.Render(hash); \/\/ Executes the variable replacement in the template<\/pre>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">using Newtonsoft.Json;\n\npublic class DictionaryConverter : JsonConverter\n{\n\tpublic override bool CanWrite => false;\n\tpublic override bool CanConvert(Type objectType) => typeof(IDictionary&lt;string, object>).IsAssignableFrom(objectType);\n\tpublic override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { }\n\tpublic override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) => ReadValue(reader);\n\n\tprivate object ReadValue(JsonReader reader)\n\t{\n\t\twhile (reader.TokenType == JsonToken.Comment)\n\t\t{\n\t\t\tif (!reader.Read()) throw new JsonSerializationException(\"Unexpected Token when converting IDictionary&lt;string, object>\");\n\t\t}\n\t\tswitch (reader.TokenType)\n\t\t{\n\t\t\tcase JsonToken.StartObject:\n\t\t\t\treturn ReadObject(reader);\n\t\t\tcase JsonToken.StartArray:\n\t\t\t\treturn this.ReadArray(reader);\n\t\t\tcase JsonToken.Integer:\n\t\t\tcase JsonToken.Float:\n\t\t\tcase JsonToken.String:\n\t\t\tcase JsonToken.Boolean:\n\t\t\tcase JsonToken.Undefined:\n\t\t\tcase JsonToken.Null:\n\t\t\tcase JsonToken.Date:\n\t\t\tcase JsonToken.Bytes:\n\t\t\t\treturn reader.Value;\n\t\t\tdefault:\n\t\t\t\tthrow new JsonSerializationException\n\t\t\t\t\t(string.Format(\"Unexpected token when converting IDictionary&lt;string, object>: {0}\", reader.TokenType));\n\t\t}\n\t}\n\n\tprivate object ReadArray(JsonReader reader)\n\t{\n\t\tIList&lt;object> list = new List&lt;object>();\n\t\twhile (reader.Read())\n\t\t{\n\t\t\tswitch (reader.TokenType)\n\t\t\t{\n\t\t\t\tcase JsonToken.Comment:\n\t\t\t\t\tbreak;\n\t\t\t\tdefault:\n\t\t\t\t\tvar v = ReadValue(reader);\n\t\t\t\t\tlist.Add(v);\n\t\t\t\t\tbreak;\n\t\t\t\tcase JsonToken.EndArray:\n\t\t\t\t\treturn list;\n\t\t\t}\n\t\t}\n\t\tthrow new JsonSerializationException(\"Unexpected end when reading IDictionary&lt;string, object>\");\n\t}\n\n\tprivate object ReadObject(JsonReader reader)\n\t{\n\t\tvar obj = new Dictionary&lt;string, object>();\n\t\twhile (reader.Read())\n\t\t{\n\t\t\tswitch (reader.TokenType)\n\t\t\t{\n\t\t\t\tcase JsonToken.PropertyName:\n\t\t\t\t\tvar propertyName = reader.Value.ToString();\n\t\t\t\t\tif (!reader.Read())\n\t\t\t\t\t{\n\t\t\t\t\t\tthrow new JsonSerializationException(\"Unexpected end when reading IDictionary&lt;string, object>\");\n\t\t\t\t\t}\n\t\t\t\t\tvar v = ReadValue(reader);\n\t\t\t\t\tobj[propertyName] = v;\n\t\t\t\t\tbreak;\n\t\t\t\tcase JsonToken.Comment:\n\t\t\t\t\tbreak;\n\t\t\t\tcase JsonToken.EndObject:\n\t\t\t\t\treturn obj;\n\t\t\t}\n\t\t}\n\t\tthrow new JsonSerializationException(\"Unexpected end when reading IDictionary&lt;string, object>\");\n\t}\n}<\/pre>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">using System;\nusing System.Collections.Generic;\nusing System.Linq;\nusing Domain.Models;\n\nnamespace {{ Namespace }}\n{\n\tpublic class Dm{{ Name }}\n\t{\n{% for p in Properties -%}\n\t\tpublic {{ p.DataType }} {{ p.Name }} { get; set; }\n{% endfor -%}\n\n\t\tinternal static Dm{{Name}} FromDomain({{Name}} src)\n\t\t{\n\t\t\tif (src == null) return null;\n\t\t\treturn new Dm{{Name}} {\n{% for p in Properties -%}\n\t\t\t\t{{ p.Name }} = src.{{ p.Name }}{% if forloop.index != Properties.size %},{% endif -%} \n{% endfor -%} \n\t\t\t};\n\t\t}\n\t\tinternal static {{Name}} ToDomain(Dm{{Name}} src)\n\t\t{\n\t\t\tif (src == null) return null;\n\t\t\treturn new {{Name}} {\n{% for p in Properties -%}\n\t\t\t\t{{ p.Name }} = src.{{ p.Name }}{% if forloop.index != Properties.size %},{% endif -%} \n{% endfor -%} \n\t\t\t};\n\t\t}\n\t}\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Language syntax documentation: https:\/\/shopify.github.io\/liquid\/<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[6,4,1],"tags":[],"class_list":["post-4731","post","type-post","status-publish","format-standard","hentry","category-dotnet","category-programming","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/4731","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/comments?post=4731"}],"version-history":[{"count":4,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/4731\/revisions"}],"predecessor-version":[{"id":4781,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/4731\/revisions\/4781"}],"wp:attachment":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/media?parent=4731"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/categories?post=4731"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/tags?post=4731"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}