What's most efficient? Chunk with "If" or Snippet?

Summary

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?

Environment

  • MODX: 2.7.1-pl
  • MIGX: 2.12.0-pl
  • If: 1.1.1-pl
  • PHP: 7.3
  • MySQL: 5.1 (latin1, latin1_swedish_ci)

Template

[[!getImageList?
&tvname=`sections`
&tpl=`sections_list`]]

**OR**

[[!section_layout]]

“If” Extra [sections_list]

This route appears to do a lot of the work for you.

[[!If?
   &subject=`[[+section_layout]]`
   &operator=`=`
   &operand=`Layout A`
   &then=`[[$layout_a]]`
]]
[[!If?
   &subject=`[[+section_layout]]`
   &operator=`=`
   &operand=`Layout B`
   &then=`[[$layout_b]]`
]]
[[!If?
   &subject=`[[+section_layout]]`
   &operator=`=`
   &operand=`Layout C`
   &then=`[[$layout_c]]`
]]
[[!If?
   &subject=`[[+section_layout]]`
   &operator=`=`
   &operand=`Layout D`
   &then=`[[$layout_d]]`
]]

Snippet [section_layout]

One issue with this route is that I have yet to figure out how to pass the rest of the JSON along to the Chunks. :sweat_smile:

<?php
$str = $modx->resource->getTVValue('sections');
$arr = $modx->fromJSON($str);

foreach($arr as $data)
{
    $layout = $data["section_layout"];
    switch ($layout) {
        case "Layout A":
            echo("<h2>" . $layout . "</h2>");
            echo $modx->getChunk('layout_a');
            break;
        case "Layout B":
            echo("<h2>" . $layout . "</h2>");
            echo $modx->getChunk('layout_b');
            break;
        case "Layout C":
            echo("<h2>" . $layout . "</h2>");
            echo $modx->getChunk('layout_c');
            break;
        case "Layout D":
            echo("<h2>" . $layout . "</h2>");
            echo $modx->getChunk('layout_d');
            break;
        default:
            break;
    }
}
?>

Poll:

Do you have an opinions or a more efficient route to take?

  • Chunk with “If”
  • PHP Snippet
  • Other

0 voters

Thank you for your assistance!

A snippet will be more efficient.
I haven’t used the If extra before but assume it would be for people unfamiliar with PHP.

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, '');

Yes, snippet, 100%.

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.

More about how to use caching effectively: https://www.markhamstra.com/modx/2011/10/caching-guidelines-for-modx-revolution/

If you have a field with the chunkname, you don’t need any conditional to load different chunks, you could use
&tpl=`@FIELD:yourchunknamefield`

2 Likes

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! :sunglasses:

On my template I have my MIGX Template 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!

MIGX TV / Field:
Name: section_layout
Type: Listbox
Value: layout_a || layout_b || layout_c

Chunks:
Three separate chunks, each with unique HTML layouts, using the same MIGX variables, with matching names to the MIGX TV / Field (section_layout).

Main Template:

[[!getImageList?
&tvname=`sections`
&tpl=`@FIELD:section_layout`
]]

I’m looking for the “buy me a beer” link now. :beers:

1 Like

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”.