I know how to pass the values of single variable fields to an embed, but have a circumstance where it would be efficient to be able to pass the value of a relationship field to the embed, so that the embed could then work with the content of the relationship field. Is this possible? If so, how is the relationship field accessed from within the embed?
I tried the following, but it doesn’t work…
{relationship_1} is the relationship field within a channel entry.
{embed="_partials/_embed_template" rel_field="{relationship_1}"}
The embed contains the following, but gives no output:
{embed:rel_field}
{entry_id}
{/embed:rel_field}
If this cannot be done this way, any suggestions about how I could pass the value of a relationship field to an embed? (or achieve the same result some other way)? Thanks a lot for any help.
Hi James. Thanks for the reply.
I’ve got a simple block of code that builds a image-based switcher (e.g. 1 to 4 buttons in a row with an image behind and text overlaid on each): the switcher takes its content (i.e. images, texts, links) and ordering from the entries referenced in a relationship field.
I’d like to use the image-switcher code in more than one template, and by implication for entries from various relationship fields. Because the name of the relationship is not known, using a code-snippet or passing the entry_id(s) to an embed and having the embed look up the relationship field to get the appropriate values would seem problematic.
However, if I could pass the relationship value itself to the embed, it could simply look up the content directly.
Would value any insight into how else I can achieve this outcome.
Hi,
You can achieve this with Template Partials. Put everything you want output that would normally be inside
{field:realtionship}
ALL THIS GOES INTO A PARTIAL
{/field:realtionship}
Into a Template Partial.
Then anywhere you want to use that Partial, call it in like this.
{exp:channel:entries channel''}
{field:realtionship}
{partial_name}
{/field:realtionship}
{/exp:channel:entries}
This allows you to keep the output DRY, but always show the right data, based on the field. Also allows you to use other parameters on the {field:realtionship}
without having to pass them through an embed.
Ahah - I was thinking that perhaps Template Partials would be cleverer than snippets.
Snippets won’t work in this case because (as far as I know) the content of the snippet is fixed.
With snippets the code within the snippet will only work for the field / template instance it was designed for, because for the relationship field values to be accessed you have to name the field being used - however to be useful in the case outlined, the characteristics of the relationship variable being used may change between different uses sufficiently to break a specific coding solution simply because the names of the variables in the snippet are incorrect in the new location (e.g. if it is in a different template location / nesting depth, or relationship is specified by a field with a different name).
To make a snippet solution generally applicable would require a lot of sniffing and conditionals to work out what is happening in the specific instance - which undermines the pursuit of DRY code… (would be quicker but messier to have multiple versions of nearly the same code in various templates).
A solution where the relationship field value is passed to an embed could avoid this, and be super-DRY - as the variables in the embed are keyed off the embedded value name, rather than the name of the variable being passed through the embed.
Not sure that makes complete sense… but hopefully enough for you to get the idea / issue.
Is this a step too far?
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.