Try our conversational search powered by Generative AI!

Collection of Static Blocks

Vote:
 

Im trying to mimic functionality similar to the ElencySolutions.MultipleProperty basically i would like to build up a collection of static blocks and was wondering best way to approach this.

I dont want to use a content area as these blocks are static and not available in Edit mode.

#121252
May 05, 2015 17:24
Vote:
 

Hi!

One way of doing this without developing any custom properties is to nest your local blocks in another local block. It could look something like this:

public abstract class BlockCollection : BlockData
{
    public abstract IEnumerable<BlockData> GetBlocks();
}

[ContentType(DisplayName = "My block collection", Description = "", GUID = "EEEB790D-3C10-4EAB-BAB7-ACBE14780DBC", AvailableInEditMode = false)]
public class MyBlockCollection : BlockCollection
{
    public virtual MyFirstBlockType FirstBlock { get; set; }

    public virtual MySecondBlockType SecondBlock { get; set; }

    public override IEnumerable<BlockData> GetBlocks()
    {
        return new BlockData[] { FirstBlock, SecondBlock };
    }
}

Then you can have a block view looking like this in ~/Views/Shared/Blocks/BlockCollection.cshtml:

@model BlockCollection

@foreach (var block in Model.GetBlocks())
{
    @Html.PropertyFor(m => block)
}

You can actually use the same view for all your block collections inheriting from BlockCollection by registering a template model:

[ServiceConfiguration(typeof(IViewTemplateModelRegistrator))]
public class TemplateCoordinator : IViewTemplateModelRegistrator
{
    public const string BlockFolder = "~/Views/Shared/Blocks/";

    public void Register(TemplateModelCollection viewTemplateModelRegistrator)
    {
        // BlockCollection partial
        viewTemplateModelRegistrator.Add(typeof(BlockCollection), new TemplateModel
        {
            Name = "BlockCollectionView",
            Inherit = true,
            AvailableWithoutTag = true,
            Path = BlockPath("BlockCollection.cshtml")
        });
    }

    private static string BlockPath(string fileName)
    {
        return string.Format("{0}{1}", BlockFolder, fileName);
    }
}
#121303
Edited, May 06, 2015 14:14
Vote:
 

Thank you, i will give this a go although for now have used the MultipleProperty

#121391
May 08, 2015 11:44
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.