Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Leopold Kristjansson 3 posts 104 karma points
    Aug 31, 2016 @ 14:50
    Leopold Kristjansson
    0

    How to render RTE content to a JSON object in macro?

    I have created a few macros now that create json objects with their data (I use JS to render that data to the page). Those have been lists of things using simple text fields. But now I have to retrieve data from a RTE field - and I'm experiencing problems doing that.

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    @using System.Globalization
    
    <script>
    
    @{
        var latestArticlesNodeID = Model.MacroParameters["ArticlesFolder"];
        var latestArticlesNode = Umbraco.Content(latestArticlesNodeID);
    
        var latestArticles = new List<dynamic>();
    
        foreach (var article in latestArticlesNode.Children)
        {
            latestArticles.Add(new
            {
                title = article.title,
                date = article.date.ToString("dd.MM.yyyy"),
                content = article.content
            });
        }
        var o = new {
            latestArticles = latestArticles
        };
        string json = Json.Encode(o);
    }
    
    window.latestArticlesObj = @Html.Raw(json);
    
    console.log(window.latestArticlesObj);
    
    </script>
    

    The console.log gives me an object, but the "content" field doesn't give me what I want... any suggestions? Date and title fields are just like they should be.

    This is what the console.log gives me

  • Leopold Kristjansson 3 posts 104 karma points
    Aug 31, 2016 @ 17:32
    Leopold Kristjansson
    100

    Ok, I found out.

    content = article.content
    

    should be

    content = Html.Raw(article.content).ToString(),
    
Please Sign in or register to post replies

Write your reply to:

Draft