Developer forum

Forum » Templates » Loop through paragraphs

Loop through paragraphs

Jim Trebbien
Reply

I have a page with some global elements. Is it possible to loop through the paragraphs and add them to a page? Instead of adding them seperatly with <!--@Global:Paragraph.Content(16086)-->


Replies

 
Mikkel Ricky
Reply
This post has been marked as an answer

You can do this in a Razor template using @RenderParagraphContent:

@{
var paragraphIds = new List<int>() { 4, 7, 8, 9, 87, 112 }; // @TODO Get the real list of ids
  foreach (var id in paragraphIds) {
    @: @RenderParagraphContent(id)
  }
}

A better solution might be to use items for the content you want to include in your template. Then you can add an item publisher in your template

@RenderItemList(new {
    ItemType = "Paragraph",
    ListSourceType = "Page",
    ListSourcePage = @Pageview.ID, // @TODO Use the real page id (probably using a Website item property)
    IncludeParagraphItems = true,
    ItemFieldsList = "*",
    ListTemplate = "ItemPublisher/List/ListStuff.cshtml",
    ListPageSize = 10
})

or using HTML syntax

<!--@Item.RenderList(ItemType:Paragraph;
                     ListSourceType:Page;
                     ListSourcePage = 15158; // TODO Use the real page id
                     IncludeParagraphItems:true;
                     ItemFieldsList:*;
                     ListTemplate:ItemPublisher/List/ListStuff.cshtml)-->

 

See http://templates.dynamicweb.com/TemplateTags/Dynamicweb-template-tags/Module-tags/Item-publisher/Item-Render.aspx for details.

Best regards,
Mikkel

Votes for this answer: 1
 
Umar Farooq
Reply

Hi Mikkel,

Your solution worked for like a charm but still i have a problem with global elements they are not rendering on a page how can i fix it? i am using the following code to get the paragraphs content

 foreach (Dynamicweb.Content.Paragraph i2 in Dynamicweb.Content.Paragraph.GetParagraphsByPageID(i.ID))
                               {
@:                                @RenderParagraphContent(i2.ID)
                               }

Thanks in advance,

umar

 

You must be logged in to post in the forum