Copied to clipboard

Flag this post as spam?

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


  • Tom Engan 430 posts 1173 karma points
    Feb 10, 2016 @ 14:39
    Tom Engan
    0

    How to render part of text from Grid

    I have a template named RSS Feeder who looks like this:

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    @{
        Layout = null;
        Response.ContentType = "text/xml";
        var siteURL = "http://" + Request.Url.Host;
        var HomePage = Model.Content.AncestorOrSelf(1);
        var pubDate = HomePage.Descendants("Hjem").OrderBy("updateDate desc");
    }<?xml version="1.0" encoding="us-ascii" ?>
    <rss version="2.0" 
         xmlns:atom="http://www.w3.org/2005/Atom" 
         xmlns:content="http://purl.org/rss/1.0/modules/content/" 
         xmlns:wfw="http://wellformedweb.org/CommentAPI/" 
         xmlns:dc="http://purl.org/dc/elements/1.1/">
      <channel>
        <atom:link href="http://mittfirma.no/rss" rel="self" type="application/rss+xml" />
        <title>MittFirma</title>
        <link>@siteURL</link>
        <pubdate></pubdate>
        <description>MittFirma i Lister</description>
        @{
            foreach (var release in HomePage.Children.OrderBy("updateDate desc").Take(10))
            {
                <item>
                    <title>@release.Name</title>
                    @Html.Raw("<link>")@[email protected](release.Url)@Html.Raw("</link>")
                    <description>@release.GetPropertyValue("content")</description>
                    <pubdate>@release.CreateDate.ToString("r")</pubdate>
                    <guid>@[email protected](release.Url)</guid>
                </item>
            }
        }
      </channel>
    </rss>
    

    And here is my result:

    <rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
    <channel>
      <atom:link href="http://mittfirma.no/rss" rel="self" type="application/rss+xml"/>
      <title>MittFirma</title>
      <link>http://mittfirma.no</link>;
      <pubdate/>
      <description>MittFirma i Lister</description>
      <item>
        <title>Konsulent</title>
        <link>http://mittfirma.no/konsulent/</link>;
        <description>
        { "name": "1 column layout", "sections": [ { "grid": 12, "rows": [ { "name": "Full width", "areas": [ { "grid": 12, "allowAll": false, "allowed": [ "rte", "media", "macro", "embed", "carousel", "modal", "portofolio" ], "config": { "class": "siteheader" }, "styles": { "background-image": "url(/media/1002/blue_keyboard.jpg)" }, "controls": [ { "value": "<h2>IT konsulenttjenester</h2>\n<p>Når din bedrift har behov for å hente inn ekstra ressurser til et prosjekt: HTML5 CCS3 JavaScript, ASP.NET WebForms MVC, EF, SQL databasedesign, Adobe CC, Office, Autocad, LCI mm.</p>", "editor": { "name": "Rich text editor", "alias": "rte", "view": "rte", "render": null, "icon": "icon-article", "config": {} } } ] } ], "id": "2788a6b4-4968-e4b1-7647-de9867402494" }, { "name": "Two column (3/9)", "areas": [ { "grid": 3, "allowAll": false, "allowed": [ "rte", "media", "macro", "embed", "carousel", "modal", "portofolio" ], "config": { "class": "thumbnail" }, "styles": {}, "controls": [ { "value": { "macroAlias": "SiblingMenu", "macroParamsDictionary": {} }, "editor": { "name": "Macro", "alias": "macro", "view": "macro", "render": null, "icon": "icon-settings-alt", "config": {} } } ] }, { "grid": 9, "allowAll": false, "allowed": [ "rte", "media", "macro", "embed", "carousel", "modal", "portofolio" ], "config": { "class": "thumbnail contentPadding" }, "styles": {}, "controls": [ { "value": { "macroAlias": "breadcrumb", "macroParamsDictionary": {} }, "editor": { "name": "Macro", "alias": "macro", "view": "macro", "render": null, "icon": "icon-settings-alt", "config": {} } }, { "value": "<p>MittFirma har erfaring fra en rekke disipliner. Alle erfaringer viser at oftest vil det være behov for mer enn én enkeltdisiplin ute hos oppdragsgiver. Oppdrag kan også utføres på MittFirma sitt eget kontor om ønskelig.</p>\n<p>I menyen til høyre finnes detaljer om de ulike disiplinene, hvor lang erfaringstid, eller hvilken tidsperiode arbeidserfaringen gjelder fra.</p>\n<p>Finner din bedrift én disiplin, eller en kombinasjon som passer til ditt oppdrag, eller om du kun finner beskrevet delvis det du ser etter: Nøl ikke med å ta kontakt eller send en forespørsel for nøyere gjennomgang - det kan være kompetansen som ikke er beskrevet allikevel forekommer.</p>", "editor": { "name": "Rich text editor", "alias": "rte", "view": "rte", "render": null, "icon": "icon-article", "config": {} } }, { "value": { "macroAlias": "SocialButtons", "macroParamsDictionary": {} }, "editor": { "name": "Macro", "alias": "macro", "view": "macro", "render": null, "icon": "icon-settings-alt", "config": {} } } ] } ], "id": "53119279-84c6-bf1d-fd2e-5389cf22fc31" } ] } ] }
        </description>
        <pubdate>Thu, 03 Dec 2015 11:04:11 GMT</pubdate>
        <guid>http://mittfirma.no/konsulent/</guid>;
      </item>
    </channel>
    </rss>
    

    <description>@release.GetPropertyValue("content")</description> are now getting the whole the text from the Grid. But how can I get some a part of this text, like eg:

    MittFirma har erfaring fra en rekke disipliner. Alle erfaringer viser at oftest vil det være behov for mer enn én enkeltdisiplin ute hos oppdragsgiver. Oppdrag kan også utføres på MittFirma sitt eget kontor om ønskelig. I menyen til høyre finnes detaljer om de ulike disiplinene, hvor lang erfaringstid, eller hvilken tidsperiode arbeidserfaringen gjelder fra. Finner din bedrift én disiplin, eller en kombinasjon som passer til ditt oppdrag, eller om du kun finner beskrevet delvis det du ser etter: Nøl ikke med å ta kontakt eller send en forespørsel for nøyere gjennomgang - det kan være kompetansen som ikke er beskrevet allikevel forekommer.

    what I consider to be the main text (look after { "value": )?

  • Jan Skovgaard 11280 posts 23678 karma points MVP 10x admin c-trib
    Feb 10, 2016 @ 16:55
    Jan Skovgaard
    0

    Hi Tom

    I have not really gotten my feet wet using the grid for real yet - However I just did a quick search and think that you should have a look in this answer from Sebastiaan https://our.umbraco.org/forum/umbraco-7/using-umbraco-7/53635-how-to-render-the-grid-data-type-i-72-alpha#comment-196930

    If I understand it correctly you should be able to write the following in your code above @release.GetGridHtml("myGridPropertyAlias") - Does that work?

    If not I'm not sure how to go about it - But I'm confident others will chime in if so :)

    /Jan

  • Tom Engan 430 posts 1173 karma points
    Feb 11, 2016 @ 12:50
    Tom Engan
    0

    I have succeeded in print the entire contents of the Grid to an HTML page, but what if I only want to print part of the content, as in the example of the text above?

    The whole JSON in the RSS' <description> is rather long:

    { "name": "1 column layout", "sections": [ { "grid": 12, "rows": [ { "name": "Full width", "areas": [ { "grid": 12, "allowAll": false, "allowed": [ "rte", "media", "macro", "embed", "carousel", "modal", "portofolio" ], "config": { "class": "siteheader" }, "styles": { "background-image": "url(/media/1002/blue_keyboard.jpg)" }, "controls": [ { "value": "<h2>IT konsulenttjenester</h2>\n<p>Når din bedrift har behov for å hente inn ekstra ressurser til et prosjekt: HTML5 CCS3 JavaScript, ASP.NET WebForms MVC, EF, SQL databasedesign, Adobe CC, Office, Autocad, LCI mm.</p>", "editor": { "name": "Rich text editor", "alias": "rte", "view": "rte", "render": null, "icon": "icon-article", "config": {} } } ] } ], "id": "2788a6b4-4968-e4b1-7647-de9867402494" }, { "name": "Two column (3/9)", "areas": [ { "grid": 3, "allowAll": false, "allowed": [ "rte", "media", "macro", "embed", "carousel", "modal", "portofolio" ], "config": { "class": "thumbnail" }, "styles": {}, "controls": [ { "value": { "macroAlias": "SiblingMenu", "macroParamsDictionary": {} }, "editor": { "name": "Macro", "alias": "macro", "view": "macro", "render": null, "icon": "icon-settings-alt", "config": {} } } ] }, { "grid": 9, "allowAll": false, "allowed": [ "rte", "media", "macro", "embed", "carousel", "modal", "portofolio" ], "config": { "class": "thumbnail contentPadding" }, "styles": {}, "controls": [ { "value": { "macroAlias": "breadcrumb", "macroParamsDictionary": {} }, "editor": { "name": "Macro", "alias": "macro", "view": "macro", "render": null, "icon": "icon-settings-alt", "config": {} } }, { "value": "<p>MittFirma har erfaring fra en rekke disipliner. Alle erfaringer viser at oftest vil det være behov for mer enn én enkeltdisiplin ute hos oppdragsgiver. Oppdrag kan også utføres på MittFirma sitt eget kontor om ønskelig.</p>\n<p>I menyen til høyre finnes detaljer om de ulike disiplinene, hvor lang erfaringstid, eller hvilken tidsperiode arbeidserfaringen gjelder fra.</p>\n<p>Finner din bedrift én disiplin, eller en kombinasjon som passer til ditt oppdrag, eller om du kun finner beskrevet delvis det du ser etter: Nøl ikke med å ta kontakt eller send en forespørsel for nøyere gjennomgang - det kan være kompetansen som ikke er beskrevet allikevel forekommer.</p>", "editor": { "name": "Rich text editor", "alias": "rte", "view": "rte", "render": null, "icon": "icon-article", "config": {} } }, { "value": { "macroAlias": "SocialButtons", "macroParamsDictionary": {} }, "editor": { "name": "Macro", "alias": "macro", "view": "macro", "render": null, "icon": "icon-settings-alt", "config": {} } } ] } ], "id": "53119279-84c6-bf1d-fd2e-5389cf22fc31" } ] } ] }
    

    But what if I want to print only:

    MittFirma har erfaring fra en rekke disipliner. Alle erfaringer viser at oftest vil det være behov for mer enn én enkeltdisiplin ute hos oppdragsgiver. Oppdrag kan også utføres på MittFirma sitt eget kontor om ønskelig. I menyen til høyre finnes detaljer om de ulike disiplinene, hvor lang erfaringstid, eller hvilken tidsperiode arbeidserfaringen gjelder fra. Finner din bedrift én disiplin, eller en kombinasjon som passer til ditt oppdrag, eller om du kun finner beskrevet delvis det du ser etter: Nøl ikke med å ta kontakt eller send en forespørsel for nøyere gjennomgang - det kan være kompetansen som ikke er beskrevet allikevel forekommer.

    Have the same desire from a search result. It's easy to print content from a single field, like for example title, but what if I want to define a part of content (here called <description>) from the Grid to be listed out, whether it is search result or RSS to be listed out.

    NOTE: I've actually been able to put every page on my website into one single Grid (except top menu and footer which is best done with partial views). No additional fields have been necessary.

  • Dennis Aaen 4499 posts 18254 karma points admin hq c-trib
    Feb 11, 2016 @ 14:32
    Dennis Aaen
    0

    Hi Tom,

    Try to see this about the limitions that there is when you are using the GridLayout.

    https://our.umbraco.org/documentation/getting-started/backoffice/property-editors/built-in-property-editors/Grid-Layout/Grid-Layout-Best-Practices

    /Dennis

  • Tom Engan 430 posts 1173 karma points
    Feb 11, 2016 @ 16:13
    Tom Engan
    0

    https://our.umbraco.org/documentation/getting-started/backoffice/property-editors/built-in-property-editors/Grid-Layout/Grid-Layout-Best-Practices

    *The editor offers many configuration options, and as a website implementor, you could easily be tempted to use the grid for nearly every kind of content entry - this is however not encouraged.

    Limitations

    With the above usage scenarios in mind, consider the grids limitations. First of all, all content entered into the grid is stored as a single property value on the content node, as a big JSON object. This means that as soon as the values are stored in the database, there is no managed api to drill into the grid content and target specific cell content - so a grid layout is not a recommended storage of reusable content - it simply wasn't designed for this scenario. If you wish to reuse content in multiple pages, it is still recommended that you store these pieces of content as separate content nodes, so they can be stored, cached and queried as usual.*

    Hmm, I see. Then I must think further if there are other solutions. The goal must still be to avoid storing the same text more than once

  • Nik 1591 posts 7148 karma points MVP 6x c-trib
    Feb 11, 2016 @ 14:17
    Nik
    0

    Hi Tom,

    I think you are possibly going to struggle with this concept. What you could consider is extending one of the grid property editors and see how feasible (if at all) it is to save that value into a second property on your page.

    The Grid is more like an RTE on steroids, based on my understanding, that can be used to give editors a bit of control over things like layout and structure of a pages content. However trying to access individual properties in the grid is much more complex.

    One possibility could be to create a custom layout file that excludes all content except for what you wish to be displayed, effectively nullifying the grid concept. You could then use the GetGridHtml method and name the alternative template to use. This may or may not work but you would, most likely, need to have a specific grid property editor that is only used once on the page.

    Other people might have better ideas than those above, but I thought I'd share my thoughts.

    Nik

  • Ian 178 posts 752 karma points
    Feb 11, 2016 @ 14:56
    Ian
    0

    Hi Tom this is maybe only half an answer but you could look at Skybrud.Umbraco.GridData. I think you will need to access items in the grid at an object level to achieve what you want sensibly, and then apply some constraints in the configuration of the grid so you know you are looking in row 1 column 2, or alternatively looking for the first control of type 'rte'. What ever logic you create to select the items you want it looks like the grid data package will at least help.

    I don't know but this could also provide part of the answer for your use case (though i don't think it would be absolutely necessary) Doc Type Grid Editor

  • Ismael 71 posts 354 karma points
    Oct 13, 2016 @ 19:25
    Ismael
    0

    In case anyone else comes across this, I used Skybrud's GridData package and something like this in my razor file

        var bodyText = Model.Content.GetGridModel("bodyText");
    
    
        @(bodyText.GetAllControls("rte").FirstOrDefault().GetValue<GridControlRichTextValue>().HtmlValue)
    

    Of course you'll want to check for nulls and what-not..

Please Sign in or register to post replies

Write your reply to:

Draft