0

is there a way - possibly with php - to 'build' a field tag from a 'variable'?

i currently have this:

{exp:stash:get_list name="rf_columns" prefix="rf"}{!--
    --}{if rf:col_value == 'f_admin_notes'}{f_admin_notes}{/if}{!--
    --}{if rf:col_value == 'f_hname1'}{f_hname1}{/if}{!--
    --}{if rf:col_value == 'f_fname1'}{f_fname1}{/if}{!--
    --}{if rf:col_value == 'f_mname1'}{f_mname1}{/if}{!--
    --}{if rf:col_value == 'f_lname1'}{f_lname1}{/if}{!--
    --}{if rf:col_value == 'f_notes'}{f_notes}{/if}{!--
    --}{if rf:col_value == 'f_organisation'}{f_organisation}{/if}{!--
    --}{if rf:col_value == 'entry_id'}{entry_id}{/if}{!--
    --}{if rf:col_value == 'count'}{count}{/if}{!--
    --}...
{/exp:stash:get_list}

obviously this is quite cumbersome, I'm hoping to have something like:

{<?php echo 'rf:col_value'; ?>}

i've tried this, but i don't think im doing it right

just to explain, i have a grid field where the user can select the data they would like displayed, i then use that to output the data of the corresponding field

my channel ("reports") grid has a dropdown to select a field name as a value and not the field related, so it's value is just the text "f_field_name", i'm then using that value inside the entries tag for "contacts" to output the actual field value:

{if rf:col_value == 'f_field_name'}{f_field_name}{/if}

instead i'm hoping to be able to 'build' the field tag, e.g. {{rf:col_value}} (output the value "f_field_name" then parse the outer curly braces to output the actual value of "{f_field_name}")

1
  • Just for future reference it is always helpful to include information about what version of EE you are using (and possibly what version of php in some questions). Apr 14, 2022 at 15:24

2 Answers 2

1

I ran a quick test with a dropdown field, where the value of the drop down was {body} (a field in another channel I have). I believe the following should work to do what your looking for.

{exp:channel:entries channel="test" dynamic="no" limit="1"}
    <h1>{title}</h1>
    <hr>
    {embed="test/body_field_test" get_field='{field}'}
{/exp:channel:entries}

And then for the embed.

{exp:channel:entries channel="blog" dynamic="no" limit="1"}
    {!-- This renders the selected field from a dropdown in the entry... in my test the body field --}
    {embed:get_field}
{/exp:channel:entries}
1

There is a way to do this that would appear to be quite a bit less complicated, and possibly helpfully, does not require the use of either stash, inline php or embeds...

What you need to do is exploit one of the many semi-hidden advantages of EE's Layout Variables system.

EE's template layout system lets you break down the construction of an output template into a series of steps - with each subsequent template in the sequence being able to access the content generated in the previous steps. By using some of these component templates in more than one 'layout stack' you can build templates with common architecture but different detailed content (so for example you can use one stack to embed a form developed on one sub-template within a general background page layout, and in another stack embed a video box in the same space instead: then any changes to the background layout will reflected in the output produced by both template stacks). As part of this system on each sub template you can define content fragments and save them as variables, and then access them from templates later in the stack.

A quirk of the template layout system is that EE evaluates each sub-template before it passes processing on to the next in the sequence: what gets passed on is the 'output' of the sub-template, not the tags that make it up. For the next template, EE evaluates the content from scratch - simply reading the tags presented and evaluating them (in passing - this facility makes it possible to dispense with many embeds and many uses of Stash).

For your issue, all you need to do is construct your 'dynamic' field tag to a layout variable within one template in the stack and then add this layout variable to the template code in the appropriate place in a later sub-template in the stack. Possibly the only cleverness is to ensure that EE doesn't 'evaluate' the field when you build it - to ensure this requires using a url_encode side-step to hide the curly brackets - the url encoded value for { is %7B and for } is %7D.

So how would it work... for the sake of the example let's assume you have a field {chosen_option} that you are using to determine which field you want to capture. In the earlier of the layout templates you would need code like this...

{layout:set name="computed_field"}%7B{chosen_option}%7D{/layout:set}

If your variable contained the text "title" this would save a layout variable with the content %7Btitle%7D.

Now on a later template we want to retrieve this value - this is quite simple but uses another neat EE feature (Variable Modifiers) to reverse our encoding dodge for the curly brackets... all you need to do is insert something like this within your template at the place where you want to retrieve the variable field content

{layout:computed_field:url_decode}

This translates into the sub-template (in our example case) as {title} and indeed if this is included within a Channel Entries loop (or whatever) it will expand to the value saved as the title for the entry...

Hope all that makes sense 🐾

1
  • thank you for this, i got it working with Toms suggestion of including the curly brackets in the value of the drop downs menu. I did try layouts and tried to trick with encoded brackets, but haven't tried :url_decode, so when i get a chance I will give this a try and report back here, thanks
    – Keith
    Apr 15, 2022 at 11:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.