Copied to clipboard

Flag this post as spam?

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


  • Aly Sebastien 19 posts 36 karma points
    Mar 27, 2014 @ 22:25
    Aly Sebastien
    0

    Razor script to list news articles

    I am trying to find out how I would create a razor script in order to list and link to a collection of articles in Umbraco v6. For example;

    I have an article landing page. Then as a child node of this landing page in my template tree, I have a template and a doctype for an individual article. For this doctype there is simply just a heading (Textstring) and some written content (RichText).

    My goal is to allow a user to create individual articles (This bit is sorted) however, the heading and a synopsis plus a link to the article is pulled through and listed on the article landing page.

    I am aware that a razor file which utilises a un-ordered list with a href link which loops through the content tree is what is needed but I have no idea how to write one.

    Any help would be fantastic.

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Mar 27, 2014 @ 22:52
    Dennis Aaen
    2

    Hi Aly,

    I would try to provide some code for you. To make a list of individual articles you could do something like this:

    I choose to use the Model.IndividualArticles to limit the children document type that I get out. If you don´t want to limit that you could use Model.Children, then you will get all types of children to the currentpage.

    @inherits umbraco.MacroEngines.DynamicNodeContext
    <ul>
        @foreach (var item in Model.IndividualArticles)
        {
       
        <li>
            <a href="@item.Url">@item.Name</a>
         </li>
       
    }
    </ul>

    This should give you an un-ordered list with a href link to the individual articles, with the name of the of the item. If you want to use the heading as the link text and your heading field has the alias of heading you can get the data like this inside the for-each loop:

    @item.Heading 

    And the synopsis could you put in a p tag and get the data like the heading just change the alias for the synopsis field.

    If you don´t use macro razor script and uses MVC let me know and I would provide you some code if you´re running MVC.

    Hope this helps, and if you have other questions keep asking them

    /Dennis

  • Aly Sebastien 19 posts 36 karma points
    Mar 28, 2014 @ 14:11
    Aly Sebastien
    0

    Thanks Dennis. This is great. My understanding is that I am using your razor script and I am in MVC mode, not WebForms etc, but this all seems to work fine. Great stuff. This has really helped me. Thanks.

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Mar 28, 2014 @ 18:19
    Dennis Aaen
    0

    Hi Aly,

    I happy to hear that it works for you. Since you got the code to work that I posted in my post above, then you´re using macro razor script. Try see my comment in this post:

    http://our.umbraco.org/forum/developers/razor/48109-Get-generic-properties-from-a-page?p=0#comment172089

    In addition to this comment that I wrote some time ago, if you create a partial view in the folder Partial Views in the settings sections then you´re also using MVC.

    Hope this can throw some light on when you´reusing MVC razor or when you´re using Razor Macro (DynamicNode).

    /Dnenis

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Mar 28, 2014 @ 22:46
    Jeroen Breuer
    1

    Hello,

    Dennis your example is using the old and soon to be obsolete razor code. Even if your using webforms you can still use Partial View Macro Files which uses the new IPublishedContent. More info here: http://our.umbraco.org/forum/developers/razor/44664-Confused-over-when-DynamicNodeList-is-returned?p=0#comment160678

    If you want an MVC news example have a look at the Hybrid Framework. There the logic is done in the Controller and the view only shows the news. You can read more about it here: http://24days.in/umbraco/2013/hybrid-framework/

    Jeroen

Please Sign in or register to post replies

Write your reply to:

Draft