5

I'm creating a Razor Component Template in Tridion 2011. In this template I want the output to be in JSON format. Now how do I mix the two languages? JSON should be seen as HTML not as code.

Example:

{
    "timeline":
    {
        @* MAIN SCHEMA FIELDS *@
        @Fields.headline <br />
        @Fields.type <br />
        @Fields.text <br />
        @Fields.startdate.ToString("dd/mm/yy) <br />    
    }
}

The error seems to be related to html in a embedded for each loop:

DOES NOT WORK { "timeline:" {

    @* MAIN SCHEMA FIELDS *@
    "type":"@Fields.type",
    "headline":"@Fields.headline",
    "asset":
        {
            @* SINGLE VALUE EMBEDDED ASSET SCHEMA FIELD *@
            "media":"@Fields.asset.media.ID",
            "credit":"@Fields.asset.credit",
            "caption":"@Fields.asset.caption"
        },
    "text":"@HtmlEncode(Fields.text)",
    "startDate":"@Fields.startdate.ToString("dd/mm/yy")",
    "date": [
        {

            @* LOOP TRUE THE EMBEDDED DATE SCHEMA FIELD *@
            @if (Fields.date.Count > 0) {
                @foreach (var embeddedField in Fields.date)  {

                    ff @embeddedField.startdate.ToString("dd/mm/yy")

                }
            }

        }
    ]


}

}

ERROR:

TemplateCompileException: CS1002: ; expected 
Line 79 Column 24:      if (Fields.date.Count > 0) {      foreach (var embeddedField in Fields.date)  { *       ff @embeddedField.startdate.ToString("dd/mm/yy")     }    }WriteLiteral("\t\t\t\t\n\t\t\t}\n\t\t]\n\n\n\t}\n}");

CS1525: Invalid expression term '.' 
Line 79 Column 24:      if (Fields.date.Count > 0) {      foreach (var embeddedField in Fields.date)  { *       ff @embeddedField.startdate.ToString("dd/mm/yy")     }    }WriteLiteral("\t\t\t\t\n\t\t\t}\n\t\t]\n\n\n\t}\n}");

CS1002: ; expected 
Line 79 Column 25:      if (Fields.date.Count > 0) {      foreach (var embeddedField in Fields.date)  { *       ff @embeddedField.startdate.ToString("dd/mm/yy")     }    }WriteLiteral("\t\t\t\t\n\t\t\t}\n\t\t]\n\n\n\t}\n}");

CS1002: ; expected 
Line 79 Column 55:      if (Fields.date.Count > 0) {      foreach (var embeddedField in Fields.date)  { *       ff @embeddedField.startdate.ToString("dd/mm/yy")     }    }WriteLiteral("\t\t\t\t\n\t\t\t}\n\t\t]\n\n\n\t}\n}");

Stack Trace: 
   at Tridion.Extensions.Mediators.Razor.Templating.Compiler.Compile(IEnumerable`1 entries, IEnumerable`1 assemblyReferences)
   at Tridion.Extensions.Mediators.Razor.Templating.RazorTemplateGenerator.CompileTemplates(IEnumerable`1 assemblyReferences)
   at Tridion.Extensions.Mediators.Razor.RazorHandler.Compile(DateTime revisionDate)Unable to save Template Building Block (tcm:39-270613-2048).

WORKS:

{
"timeline:"
    {

        @* MAIN SCHEMA FIELDS *@
        "type":"@Fields.type",
        "headline":"@Fields.headline",
        "asset":
            {
                @* SINGLE VALUE EMBEDDED ASSET SCHEMA FIELD *@
                "media":"@Fields.asset.media.ID",
                "credit":"@Fields.asset.credit",
                "caption":"@Fields.asset.caption"
            },
        "text":"@HtmlEncode(Fields.text)",
        "startDate":"@Fields.startdate.ToString("dd/mm/yy")",
        "date": [
            {

                @* LOOP TRUE THE EMBEDDED DATE SCHEMA FIELD *@
                @if (Fields.date.Count > 0) {
                    @foreach (var embeddedField in Fields.date)  {

                        @embeddedField.startdate.ToString("dd/mm/yy")

                    }
                }

            }
        ]


    }
}
  • So basically if I understand correctly, you are wondering how to escape the @ signs and brackets? – Bart Koopman Apr 19 '13 at 10:38
  • @bart: Nope, it seems when i type "hallo" @embeddedField.startdate it gives the error mentioned above about the ; when i type just @embeddedField.startdate it works fine. but i need html in the foreach loop. – Roel van Roozendaal Apr 19 '13 at 11:07
  • 2
    Why not use a C# BB to do this for you and put it in the package? .NET has the right libraries (JSON.NET) to serialize an object to JSON. – Albert Romkes Apr 19 '13 at 11:38
7

In Razor you can use a <text> element to explicitly identify content, so try something like:

<text>
{
    "timeline":
    {
</text>
        @* MAIN SCHEMA FIELDS *@
        @Fields.headline <br />
        @Fields.type <br />
        @Fields.text <br />
        @Fields.startdate.ToString("dd/mm/yy) <br />    
<text>
    }
}
</text>

See also this picture, showing how it's normally used in Razor:

enter image description here

I'm still not quite sure what you want to output and where your issue lies, so let me know if my answer is of no use and I'll see if I can adjust it more to your problem.

  • Can i get resolved internal link in this json or any way to do it ? – Rahul Kumar Singh Jul 30 '19 at 11:44
5

Bart's answer works but it's a strange issue. I don't want the <text></text> tag in my HTML.

It seems that applying Bart's solution to the foreach loop my problem is solved and won't show the <text></text> tag in the HTML. Adding the tag to other parts it will show the tag in my HTML. So here is what does what I want:

CODE

{
    "timeline:"
{
        @* MAIN SCHEMA FIELDS *@
        "type":"@Fields.type",
        "headline":"@Fields.headline",
        "asset":
            {
                @* SINGLE VALUE EMBEDDED ASSET SCHEMA FIELD *@
                "media":"@Fields.asset.media.ID",
                "credit":"@Fields.asset.credit",
                "caption":"@Fields.asset.caption",
            },
        "text":"@HtmlEncode(Fields.text)",
        "startDate":"@Fields.startdate.ToString("dd/mm/yy")",
        "date": [
            {
                @* LOOP TRUE THE EMBEDDED DATE SCHEMA FIELD *@
                @if (Fields.date.Count > 0) {
                    @foreach (var embeddedField in Fields.date)  {
                        <text>"startDate":"</text>@embeddedField.startdate.ToString("dd/mm/yy")<text>",</text>
                        <text>"headline":"</text>@HtmlEncode(embeddedField.headline)<text>",</text>
                        <text>"text":"</text>@HtmlEncode(embeddedField.text)<text>"</text>

                        <text>"asset":</text>
                            <text>"media":"</text>@embeddedField.asset.media.ID<text>",</text>
                            <text>"credit":"</text>@embeddedField.asset.credit<text>",</text>
                            <text>"caption":"</text>@embeddedField.asset.caption<text>",</text>
                        <text>}</text>
                    }
                }
            }
        ]
    }
}

HTML OUTPUT (JSON):

{
    "timeline:"
{

        "type":"default",
        "headline":"Sh*t People Say",
        "asset":
            {

                "media":"tcm:39-270610",
                "credit":"",
                "caption":"Writers & Creators: Kyle Humphrey & Graydon Sheppard",
            },
        "text":"When &amp;amp;quot;Shit Girls Say&amp;amp;quot; was uploaded to YouTube it was picked up by Reddit, the Huffington Post, Mashable etc. In a month, it racked up over 12 million views and has spawned a meme of Sh*t People Say.",
        "startDate":"25-52-13",
        "date": [
            {

                        "startDate":"19-23-13",
                        "headline":"Sh*t Politicians Say",
                        "text":"Sh*t Politicians Say landed just hours before Thursday night’s Republican presidential debate and stars actor Joe Leon. In true political fashion, his character rattles off common jargon heard from people running for office.&amp;lt;/p&amp;gt;&amp;lt;p&amp;gt;Do these ring a bell? Moral fiber, family values, trust me, three-point plan, earmarks, tough question, children are our future, Washington outsider, jobs, my opponent — all sound familiar."
                        "asset":
                            "media":"tcm:39-270610",
                            "credit":"sd",
                            "caption":"sd",
                        }
            }
        ]
    }
}
  • Great, you answered your own question ;) – Bart Koopman Apr 19 '13 at 12:10
  • Can i get resolved internal link in this json or any way to do it ? – Rahul Kumar Singh Jul 30 '19 at 11:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.