We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

Is it possible to pass a relationship variable to an embedded template?

How Do I?

JCOGS Design's avatar
JCOGS Design
71 posts
8 years ago
JCOGS Design's avatar JCOGS Design

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.

       
James Mathias's avatar
James Mathias
225 posts
8 years ago
James Mathias's avatar James Mathias

This won’t work. Can I ask what you are trying to accomplish with this set up so I can try and recommend a possible alternative?

       
JCOGS Design's avatar
JCOGS Design
71 posts
8 years ago
JCOGS Design's avatar JCOGS Design

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.

       
James Mathias's avatar
James Mathias
225 posts
8 years ago
James Mathias's avatar James Mathias

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.

       
JCOGS Design's avatar
JCOGS Design
71 posts
8 years ago
JCOGS Design's avatar JCOGS Design

Hi James,

Helpful to know - but AFAIK Template Partials only exist in EE3… I’m still on EE2 (on the long wait for Assets and Publisher to release EE3 compliant versions). Is it possible to do simulate Template Partials in EE2?

       
James Mathias's avatar
James Mathias
225 posts
8 years ago
James Mathias's avatar James Mathias

Yup! They’re called snippets in version 2!

       
James Mathias's avatar
James Mathias
225 posts
8 years ago
James Mathias's avatar James Mathias

Also I believe Assets was released for version 3 just last week, so 1 down!

       
JCOGS Design's avatar
JCOGS Design
71 posts
8 years ago
JCOGS Design's avatar JCOGS Design

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?

       
JCOGS Design's avatar
JCOGS Design
71 posts
8 years ago
JCOGS Design's avatar JCOGS Design
Also I believe Assets was released for version 3 just last week, so 1 down!

Yes, progress indeed - but AFAIK it is still only in beta… :(

       
James Mathias's avatar
James Mathias
225 posts
8 years ago
James Mathias's avatar James Mathias

Ah yes, I see what you mean. Yeah that wouldn’t be very DRY.

Unfortunately, it doesn’t work with embeds because of the way they are parsed and separated into other templates

       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.