Forum
Question regarding the reuse of a template tag with same ID within a template
I've seen that you can reuse the entire perch:content
tag in order to display the data multiple times in a template (i.e. email address in an anchor). I'm not too keen on the full repetition of the tag and all it's attributes (as there is potential for ambiguity if attributes differ on each tag).
Is it valid to only specify the id
of each subsequent tag? It seems to work, but I just want to confirm this is okay to do.
<a href="<perch:blog id="postExternalURL" type="text" label="External URL" required="false" order="3" />">
<perch:blog id="postExternalURL" /> <!-- Only specify ID here -->
</a>
Also, behind the scenes, does the editor only parse the information from the first tag it comes across with a given ID, and effectively ignore the latter ones?
As a minimum, you'll need the
id
andtype
.I think this doc page answers that question now: https://docs.grabaperch.com/docs/templates/attributes/title/
Great! So does that mean that
type
isn't actually required for subsequent tags?Hey Michael,
For the heck of it, I gave it a try, and I found that I was able to get away with not putting a
type
attribute on either the first, or subsequent tags.The behavior I observed is that without a type attribute on the first instance, it defaults to a text field; subsequent tags with only the
id
attribute and no others displayed just fine.I also tried with an image field, and was able to put a second tag with only the id, and the image URL was pulling just fine from that as well.
So despite what is said above, it looks like you can do what you're asking.
I tried this on Perch Runway 2.8.26.
Of course, Drew is the creator and lead developer of Perch, so ... Your Mileage May Vary. : ) I'm only describing the behavior I observed.
I'd say just give it a try. Quicker to try than to post here; but thanks for posting anyway because otherwise I probably wouldn't have tried. : )
-- aaron
In terms of how the template is parsed at edit time,
type
is taken from the first tag with the given ID. You can't have the same ID with different types, so we don't check.So if you have and image with
id="photo"
, we'll look for any other tag withid="photo"
in the template and check it for different image dimensions, for example.At render time, we always need the
id
and thetype
attributes. If thetype
attribute is missing, it's assumed to betext
, which will only work on the most basic of field types.Thanks for the info, Drew!
But only on the first instance, right? Because I'm having no problem just using
id
for subsequent instances ...This is actually quite nice to know ... I can clean up my templates a little bit, and have the edit form fields in different order than the template output ... because I can put them all at the top in the order I want, and use 'suppress="true"', and then reuse them in the actual template, just using the
id
.It's probably worth noting that because it is documented that we require id and type, in the future the engine might become stricter. So to be safe for future upgrades I would always stick to what the docs say - as we try very hard not to break people's templates but we base our assumptions on how we expect people to use Perch.
It's a bit like you relying on the weird way a browser deals with a CSS property, and then finding that in a new version of the browser they fix that bug and your layout breaks :)
Aaron: That's a great idea (adding the tags in order at the top and suppressing them). I currently use the order attribute but it's a mess when you have to insert a new field in the middle!
Rachel: Looking at the docs (Tag Structure -> Attributes), it seems that the
label
attribute is also required, yet wasn't mentioned by Drew above. Would it be possible to perhaps modify the docs to clarify the exact requirements in the "Re-using content in the template" section of the Tag Structure documentation? In the example given there thelabel
attribute is defined multiple times too, which is not ideal and seems also to not actually be "required". Thanks!label
is required for displaying a label. You can leave it out but then you won't have a label. It's obviously only used for editing so only applies to the first tag instance. It's common sense.id
andtype
are always required unless thetype
istext
. If you omit it you're relying on sheer luck to get you through, and as Rachel says we can't guarantee it'll keep working in the same way. When it does break you'll be left with a very hard to debug problem.