Forum

Thread tagged as: Question

ID naming in multiple template includes

Hi,

I know one can include templates in templates as described in the docs, however I wonder what happens to the IDs when one includes multiple, like so:

mytemplate.html

<perch:template path="content/myinclude.html" />
<perch:template path="content/myinclude.html" />

myinclude.html

<perch:content id="test" type="text" />

If I would now do a perch_content_custom on mytemplate.html with a skip-template: true;, what would be the ids in the array?

Thanks

Jelmer Borst

Jelmer Borst 0 points

  • 7 years ago
Rachel Andrew

Rachel Andrew 394 points
Perch Support

The template is compiled before it is used. So it is exactly the same as if you created a template with multiple IDs in it.

Docs: https://docs.grabaperch.com/docs/templates/includes/

At runtime, the included template is inserted before any further processing is done. It’s as if the inner template was always part of the outer.

The content editor would only get that field to fill in once and it would display multiple times in the output.

Perch takes the reused ID to mean that you want to reuse that content - for example entering a website URL and outputting it within the href attribute and also in the clickable text of the link.

Is there any way to set it up so that a sub-templated can be included multiple times in the same template?

For example, I might want to define a small sub-template for an image with an optional link around it, and then use that on various templates, and more than once in the same template, as separate fields

I don't think there's a way to do this because the IDs would be the same.

I thought maybe if there were a way to pass data in to the template when including, we could achieve this.

Like:

main template:

<perch:template path='content/sub_template.html' params="link_id:mylinkid" />

sub_template.html:

<perch:macro>
<a href='<perch:content id="<perch:param name="link_id" type="text" />'>
   ...
</a>
...
</perch:macro>

Or something similar. That would be one approach.

Another approach might be something like

<perch:template path='content/sub_template.html' scope="my_scope_1" />

And then anything inside that sub-template would be accessible with "my_scope_1.id", etc., to avoid conflicts.

I have looked at perch:blocks, and it's close, and a really nice feature that I'll use elsewhere, but unfortunately each template can only have one perch:blocks section ... so I'd have to do the whole template in blocks. Not the same goal as trying to modularize the templates more flexibly.

Maybe if a template could include more than one blocks section, it could work for this. Not sure.

Or is there some other approach to do this that I haven't yet discovered?

Thanks guys,

Aaron

Hi Aaron

Did you ever find a solution to this problem? I am trying to do the same thing.

Thanks

James