Copied to clipboard

Flag this post as spam?

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


  • Mark 255 posts 612 karma points
    Oct 17, 2013 @ 14:42
    Mark
    0

    Umbraco 6 Razor Usage

    How do we use the categorisation package in Umbraco 6 with razor views? Bearing in mind we use IPublishedContent, not Node.

  • James Patterson 53 posts 192 karma points
    Dec 01, 2013 @ 21:19
    James Patterson
    1

    Hi Mark,

    There is now a few simple extension methods for using in Views. You'll need to upgrade to use version 2.5 of the package which does not support Umbraco v4.x.

    Here are a few examples of each of the extension methods where 'categories' is the alias of the Category Selection property.

    1. Get all categories assigned to the current page.

    @foreach(var category in CurrentPage.GetCategories("categories"))
    {
        <li>@category.Name</li>
    }

    2. Get child pages of the current page which have the category with ID 1 assigned.

    @foreach(var page in CurrentPage.GetChildrenByCategory("categories", 1))
    {
        <li>@page.Name</li>
    }

    3. Get descendants of the current page which have category with ID 2 assigned.

    @foreach(var page in CurrentPage.GetDescendantsByCategory("categories", 2))
    {
        <li>@page.Name</li>
    }

    4. Get decendants of current page which have category with ID 2 assigned and the document type of 'umbTextpage'.

    @foreach(var page in CurrentPage.GetDescendantsByCategory("categories", 2, "umbTextpage"))
    {
        <li>@page.Name</li>
    }

    5. Get all decendants of a known page which have category with ID 1 assigned.

    @foreach(var page in Umbraco.TypedContent(1050).GetDescendantsByCategory("categories", 1))
    {
        <li>@page.Name</li>
    }

    I hope this helps.

    As always, feel free to suggest additional functionality or improvements.

    Thanks,
    James 

  • Zakhar 171 posts 397 karma points
    Mar 27, 2014 @ 16:04
    Zakhar
    0

    Hi James,

    I've just installed v2.5.1, created a couple of categories and assigned them using Category Picker. None of the examples above seem to work.

    Was something changed?

  • Zakhar 171 posts 397 karma points
    Mar 27, 2014 @ 16:54
    Zakhar
    0

    Ok, sorry, it's my fault, my property alias is "category" and I didn't update the snippet. Anyway, I installed 2.5.0 as a whole new section is an overkill, a tree node in Settings is sufficient. Examples work fine, thanks for great package!

Please Sign in or register to post replies

Write your reply to:

Draft