I’m curious about which of the following solutions would be considered most efficient programmatically. Using the “If” Extra or using PHP in a Snippet.
Using MIGX, I have the ability for the user to have multiple sections, each section having it’s own layout. I have separate HTML per layout, and thought it would be nice to store each layout in it’s own Chunk. I figured I could either run the getImageList from the template and run an if there; shown below, or run some PHP via a Snippet; shown below. But, which is best?
If you’d had 2 different chunk I’d recommend output filters like [[+section_layout:is=`A`:then=`[[$layout_a]]`:else=`[[$layout_fallback]]`]]
However not that in that case (and also in your first “If” example) MODX will first render all inner tags (your chunks) which is super inefficient!
The above example is better written as [[[[+section_layout:is=`A`:then=`$layout_a`:else=`$layout_fallback`]]]]
(note that the square brackets moved to the outer tag)
All in all, with multiple options, a simple snippet will be better. You could event hand over the TV as a snippet property into the snippet: [[!section_layout? &tv=`[[*sections]]`]]
and in your snippet you would use it like this: $str = $modx->getOption('section', $scriptProperties, '');
Another thing though, your example has all snippets (both If and the section_layout) as uncached, by prefixing it with the exclamation mark (i.e. [[!If? ... instead of [[If? ...), which is unnecessary also.
Uncaching tags is only needed if the return value of the tag may be different between requests. Anything that only looks at resource values can safely be cached (as saving the resource would clear the cache), which will benefit your performance a lot.
Alternatively you could just change the value field of your selector to the name of the chunk and have it be [[$[[+section_layout]]]], which would be even more efficient.
@bruno17, you get the prize for this one sir! This is AWESOME!
On my template I have my MIGXTemplate Variable (sections) and I’m selecting the layout Chunk (custom HTML / section) based on the value of one of my MIGX field’s (section_layout) value!
Chunks:
Three separate chunks, each with unique HTML layouts, using the same MIGX variables, with matching names to the MIGX TV / Field (section_layout).
This topic was automatically closed 2 days after discussion ended and a solution was marked. New replies are no longer allowed. You can open a new topic by clicking the link icon below the original post or solution and selecting “+ New Topic”.