Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace Nested Content with Block List #64

Open
bjarnef opened this issue Oct 25, 2023 · 3 comments
Open

Replace Nested Content with Block List #64

bjarnef opened this issue Oct 25, 2023 · 3 comments

Comments

@bjarnef
Copy link

bjarnef commented Oct 25, 2023

Issue description

In the code samples Nested Content is used and delivered as IEnumerable<IElement>:

However I can't find any examples how to get content from Block List property and how to loop the blocks or just select content as IEnumerable<IElement> .. and then in razor view or controller cast a specific block type?

Since Nested Content is deprecated and will be removed in an upcoming release or Umbraco (and probably in Heartcore as well), I think it should be practise as use Block List instead.

Based on the roadmap it seems Block Grid soon is supported as well:
https://umbraco.com/products/knowledge-center/roadmap/

@bjarnef
Copy link
Author

bjarnef commented Oct 26, 2023

@rasmusjp do you know about this? 😊

@rasmusjp
Copy link
Member

We currently don't ship with any models for the block list but you can add one your self. It could look something like this:

public class BlockListItem
{
    public Element Content { get; set; }
    public Element? Settings { get; set; }
}

and then use it like this

public class MyModel : Content
{
    public IEnumerable<BlockListItem> MyBlocks { get; set; }
}

If your custom model inherits from Element and has the same name as the Document Type alias or has the ElementModelAttribute with the Document Type alias then you should be able to cast Content or Settings to that model in you view

@bjarnef
Copy link
Author

bjarnef commented Oct 26, 2023

Hi @rasmusjp

Thanks for your help - it works! 🥳🎉

I think it would be great it the .NET Client shipped with BlockListItem model and soon probably BlockGridtem as well.
However for now I have the following:

public class BlockListItem
{
    public Element Content { get; set; } = null!;
    public Element? Settings { get; set; }
}

a ProductPage document type with a BlockList sections.

public class ProductPage : Content, IHideInNavigation
{
    public string Sku { get; set; }

    public decimal Price { get; set; }

    public int Stock { get; set; }

    public IEnumerable<MediaWithCrops> Images { get; set; }

    public IEnumerable<Content> Categories { get; set; }

    public IEnumerable<BlockListItem> Sections { get; set; }

    [JsonProperty("umbracoNaviHide")]
    public bool HideInNavigation { get; set; }

    public bool InStock() => Stock > 0;
}

and then a BlockText model reflecting the text block.

public class BlockText : Element
{
    [JsonConverter(typeof(HtmlContentConverter))]
    public IHtmlContent? Text { get; set; }
}

finally I have this in the product page razor view:

@if (Model.Sections.Any())
{
    foreach (var block in Model.Sections)
    {
        if (block.Content is BlockText blockText)
        {
            <div>
                @Html.Raw(blockText.Text)
            </div>
        }
    }
}

image

image

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants