All Low add-ons are now owned by EEHarbor. Read the blog post.

Support archive

Dynamically created variables in results / parse order

Tim Kelty 24 May 2011 14:26 question, complete

I have several fields names like this: {cf_xx_field_name}, where xx is the channel ID.

So in my results tag, what I want to do is something like:
{cf_{channel_id}_title} and {cf_{channel_id}_body}

But the way the tags are parsed, this doesn't work. Do you have any ideas (other than a massive if statement) to make something like this work?

I realize this isn't necessarily specific to Low Search, as I have the same problem using channel:entries if I'm pulling from multiple channels, but I thought I'd ask here anyhow.

preload:replace works, but then it clearly doesn't change per entry, that will only work if everything is in the same channel.

Replies

  1. Low 24 May 2011 14:55

    Hey Tim,

    This is a tricky setup you've got there. It has to do with the parse order of a single channel entry, more so than the parse order of the entire page.

    Both the {channel_id} and {cf_xx_field_name} fields are parsed by EE at the same stage, but apparently the custom fields are parsed before {channel_id}. There is no other way around this apart from creating your own extension.

    This extension would use the hook channel_entries_tagdata, which is called before all the vars are parsed. When the hook is called, you could replace the {channel_id} variable before EE goes on with its routine.

    Another option could be to just add all custom fields to your template. For example: {cf_1_title}{cf_2_title}{cf_3_title}
    EE will actually parse all these variables, despite some of them not belonging to the entry. These vars will just return empty strings, resulting in just the one you want.

    Does that help?

  2. Tim Kelty 24 May 2011 15:07

    Totally does help, thanks Low.

    The setup is a result of battling EE2's lack of a good Field General / Gypsy type solution as of yet, and trying to make my templates as flexible and reusable as possible.

    The whole field group relevance issue has been brought up plenty before, but what's also frustrating is it doesn't seem like there's a good reason EE needs you to have unique field names. At the very least it seems I should be able to have a unique Field group for each channel with duplicated fields, but the same names so they can be used in the template the same.

    Anyhow, this isn't really support related, so I'll shut up. What I think I might do is make an extension like you suggested, and look for any fields prefixed with preload:, so I would have {cf_{preload:channel_id}_title}.

    Thanks again, we'll see how it goes.