Developer forum

Forum » Templates » Number of paragraphs

Number of paragraphs

Thomas Schroll
Reply

Hi

Does anybody know a way to either get or with razor calculate the number of paragraph or items in a dwcontent div?

I would like to use the same template on all paragraphs whether there is 1, 2 or 3 in the section eg. if 3 paragraphs use span4, if 2 paragraphs use span6, if 1 paragraph use span12 in a bootstrap template.

Regards Thomas


Replies

 
Mikkel Ricky
Reply

Great idea and very useful. I've created a product backlog item for this (#12668).

Best regards,
Mikkel

 

 

 
Thomas Schroll
Reply

Great. Thanks.

Regards Thomas

 

 
Ben Doorn
Reply

Thomas,

 

With Razor, you can. This should work.

 

@using Dynamicweb.Frontend;
@using Dynamicweb.Content;
 

......

 

@Paragraph.GetParagraphsByPageID(PageView.Current().Page.ID).FindAll(par => "your-div-id".Equals(par.Container) && par.ShowParagraph).Count

 

Regards,

Ben

 

 
Mikkel Ricky
Reply
This post has been marked as an answer

That's a really cool trick, Ben.

You can use the container id of the current paragraph rather than hardcoding it:

@{
var paragraphContainer = @GetString("ParagraphContainer");
var paragraphsInContainer = Dynamicweb.Content.Paragraph.GetParagraphsByPageID(Pageview.Page.ID).FindAll(p => paragraphContainer == p.Container && p.ShowParagraph);
}

<div class="paragraph">
	@GetValue("ParagraphContainerSort")/@paragraphsInContainer.Count (@paragraphContainer)
	…
</div>

 

Votes for this answer: 1
 
Thomas Schroll
Reply

Thanks guys

Ben, I couldn't get your solution to work, but I'm a newbie in .net, so I have probably made a mistake.

Regards Thomas

 

 

 
Nicolai Høeg Pedersen
Reply

Beware of performance here. Paragraphs are taken from database again when making this call resulting in a possible performance issue on higher loads and possible slower page load times

 
Ben Doorn
Reply

Hi Nicolai,

 

I am aware of the system loading the paragraphs again from the database with this code. It would be great if the complete page model could be available within the Razor templates. As far as I know this is currently the only way to retrieve the paragraph data within a Razor template.

 

Kind regards,

Ben

 

You must be logged in to post in the forum