about render a JSON string by dotliquid

1,545 views
Skip to first unread message

Jiannan Wu

unread,
Sep 16, 2012, 11:37:30 PM9/16/12
to dotl...@googlegroups.com

I've been rendering strongly typed models in templates. But now I need to render a JSON string. I cant convert the json to an object because it is near impossible to convert json toanonymous type.but can convert to a dynamic object easily,but DotLiquid cannot render a dynamic, so converting to dynamic is also useless. 

Are there any extentions out there for .net that can do this? 

For eg.

string json = "{ Name: \"Some Name\" }";

This goes to the template renderer;

<p> Hello {{ Name }} </p>

becomes:

<p>Hello Some Name</p>

Michael Elson

unread,
Aug 28, 2014, 12:32:50 AM8/28/14
to dotl...@googlegroups.com
Hi Jiannan, 

Did you ever find a solution for rendering JSON in DotLiquid?  Curious to know how you solved this?

Mike Bridge

unread,
Dec 15, 2014, 11:49:41 AM12/15/14
to dotl...@googlegroups.com
You can convert JSON to nested Lists and Dictionaries:

        [Test]
        public void Liquid_Should_Parse_Nested_Object()
        {
            const string testIt = "Test It";

            var nestedDict = new Dictionary<String, Object> {
                {"nested", testIt}
            };
           var rootDict = new Dictionary<String, Object> {
                {"root", nestedDict}
            };


            Template template = Template.Parse("{{root.nested}}");
            var liquidHash = Hash.FromDictionary(rootDict);


            Assert.That(testIt, Is.EqualTo(template.Render(liquidHash)));
        }

        [Test]
        public void Liquid_Should_Parse_Nested_Array()
        {

            const string testIt = "Test It";

            var nestedArray = new List<String> { testIt };

            var rootDict = new Dictionary<String, Object> {
                {"root", nestedArray}
            };


            Template template = Template.Parse("{% for item in root %}{{ item }}{% endfor %} ");
            var liquidHash = Hash.FromDictionary(rootDict);


            Assert.That(testIt, Is.EqualTo(template.Render(liquidHash)));
Reply all
Reply to author
Forward
0 new messages