Display post content in piranha cms

  Kiến thức lập trình

I am trying to display the content of the page as the used has saved in the manager part.

In my view i have the following code:

@foreach (var block in Model.Blocks)
    {
      if (block is HtmlBlock htmlBlock)
      {
        <partial name="../Cms/DisplayTemplates/HtmlBlock.cshtml" for="@htmlBlock" />
      }
      if (block is HtmlColumnBlock columnBlock)
      {
        <partial name="../Cms/DisplayTemplates/HtmlColumnBlock.cshtml" for="@columnBlock" />
      }
      if (block is ImageBlock imageBlock)
      {
        <partial name="../Cms/DisplayTemplates/ImageBlock.cshtml" for="@imageBlock" />
      }
      if (block is QuoteBlock quoteBlock)
      {
        <partial name="../Cms/DisplayTemplates/ImageBlock.cshtml" for="@quoteBlock" />
      }
      if (block is TextBlock textBlock)
      {
        <partial name="../Cms/DisplayTemplates/TextBlock.cshtml" for="@textBlock" />
      }
    }

I am pretty sure there is an easier way to do this, but I can’t find anything else.

So, if I use this approach, when i create new elements in the manager, i would have to keep extending this foreach ? Something doesn’t feel right.

If i use @Html.DisplayFor(m => m.Blocks) it only displays the namespace name. Any thoughts ?

Found the solution:
In order to use @Html.DisplayFor(m => m.Blocks) you have to have the display templates for the used blocks either in the Shared Views folder, or as a sub folder inside the folder where you use @Html.DisplayFor(m => m.Blocks).

For example we have the following folders:

Views
Articles
Index.cshtml
Shared
SomeOtherFolder

The possible solutions for using @Html.DisplayFor(m => m.Blocks) in the Index.cshtml file inside the Articles folder are:

Having the DisplayTemplates(with the block templates) folder inside the Articles of Shared folder.

Hope this helps

LEAVE A COMMENT