kavin
I'm new here

Inculde the code fragment based on the section template usage

Hello All,

We would like to include the particular code fragment in the page template, if the page uses the specific section template.

Is this possible in the page template?

Thank you!

3 Replies
mikula
Crownpeak employee

As Developer I do not really understand why you would like to do this.
Is it not possible to add the desired functionallity to the section template you are using?

0 Kudos

Hi,

I can imagine, you want to add CSS or JS for an image gallery only, if a gallery section is in the page. And the answer is "yes, you can". There are, as always, many ways to do so. First: do I understand you correctly? If not please clarify your use case.

Cheers, Heiko

0 Kudos

Hi,

a usual way is something like the following code:

$CMS_IF(

  #global.page.getChildren(class("de.espirit.firstspirit.access.store.pagestore.Section"), true).toList

    .filter(section->section.isInOutputIncluded(#global.language))

    .map(section->section.template.uid)

    .filter(templateUid->["one_template_uid","another_uid"].contains(templateUid))

    .size()>0

)$

Code to be output if there is at least one "active" section based on one of the given templates.

$CMS_END_IF$

This version also supports multiple section uids. Of course you could write the same a little shorter by using a combined condition (&& instead of chained filter/map) but I prefer code like the above because I think it is better readable as it kind of describes the steps.

You could also start with !#global.page... and use isEmpty() at the end but that little ! is too easy to miss in my opinion.

Michael