Copied to clipboard

Flag this post as spam?

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


  • Dominic Resch 45 posts 115 karma points
    Oct 18, 2023 @ 07:52
    Dominic Resch
    0

    Render blocks based on an "IsPublic" property

    Greetings,

    the following scenario: I am currently working with the Block Grid, which supports as Blocks the RichTextBlock as well as a TwoColumnLayout Block (Block with 2 Areas, left and right).

    Now I have created a page, with this Block Grid and added some RichTextBlocks as well as TwoColumnLayouts. Also, I gave both blocks (RichTextBlock and TwoColumnLayout) a bool property called "IsPublic".

    Now I want to render the blocks based on the IsPublic property. I have managed that so far:

    HomePage.cshtml:

    <div class="container">
            @{
                var grid = Model.Value<BlockGridModel>("mainContent");
    
                var gridColumns = grid.GridColumns;
    
                foreach (var item in grid)
                {
                    var content = item.Content;
    
                    var isPublic = true;
                    if (content.HasValue("isPublic")){
                        isPublic = content.Value<bool>("isPublic");
                    }
    
                    if (!isPublic) {
                        continue;
                    }
    
                    @await Html.PartialAsync("Partials/blockgrid/Components/" + content.ContentType.Alias, item);
                }
            }
    </div>
    

    TwoColumnLayout.cshtml:

    <div>
        @{
            var tempAreas = new List<BlockGridArea>();
    
            foreach (var area in Model.Areas)
            {
                var tempElements = new List<BlockGridItem>();
    
                foreach (var item in area)
                {
                    var content = item.Content;
    
                    var isPublic = true;
                    if (content.HasValue("isPublic"))
                    {
                        isPublic = content.Value<bool>("isPublic");
                    }
    
                    if (!isPublic)
                    {
                        continue;
                    }
    
                    tempElements.Add(item);
                }
    
                tempAreas.Add(new BlockGridArea(tempElements, area.Alias, area.RowSpan, area.ColumnSpan));
            }
    
            var tempModel = Model;
            tempModel.Areas = tempAreas;
    
            @await Html.GetBlockGridItemAreasHtmlAsync(tempModel);
        }
    </div>
    

    And although it works, I don't like the solution, especially the one in TwoColumnLayout.

    My questions now are:

    1. Currently all blocks from the database are queried anyway, since I'm currently only working with the frontend here. Can I somehow query only those blocks from the database where the IsPublic property is "true" (or even those blocks where the property does not exist at all), and then render them without my logic in the views?

    2. If that doesn't work (which I can't imagine) or the effort to do so is too great, what would be a better option?

    For info: currently it may be only a boolean property IsPublic, but later this will of course become more. So, that only certain users/groups can see certain blocks, or that non-public blocks are shown anyway, based on the role of the user, etc.

Please Sign in or register to post replies

Write your reply to:

Draft