We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 48586
    • 78 Posts
    Hello, I'm trying to figure out a way to accomplish something on my site, and I found some documentation on passing values to my snippet, but it doesn't seem to be working. Could someone please let me know if I am using this feature correctly.

    In my document, my code looks like this:

    <div id="1" style="display:none;">[[!snippetName &filter=`value1`]]</div>
    <div id="2" style="display:none;">[[!snippetName &filter=`value2`]]</div>
    <div id="3" style="display:none;">[[!snippetName &filter=`value3`]]</div>


    My snippet is basically doing a query, and I am populating chunks from the array. That part is working fine (when I display my document without the extra div calls). Here is basically what I am looking to do with my passed values:

    if ($filter = "value1") {
    run this query
    populate this chunk
    } elseif ($filter = "value2") {
    run this query
    populate this chunk
    } elseif ($filter = "value3") {
    run this query
    populate this chunk
    }


    Thank you very much.

    This question has been answered by ottogal. See the first response.

    • discuss.answer
      • 22427
      • 793 Posts
      [[!snippetName &filter=`value1`]]
      You need to set a ? after the snippet name:
      [[!snippetName? &filter=`value1`]]
        • 48586
        • 78 Posts
        Thank you, I think I have officially been staring at my screen for too long, I totally missed that. I really appreciate the help.
          • 22427
          • 793 Posts
          To compare values, you have to use double (or triple) equal signs:
          ($filter == "value1")
          or
          ($filter === "value1")
          The difference is that an expression with === is not true if the types of the operands are different (even if the values itself are considered equal).
          See http://php.net/manual/en/types.comparisons.php#types.comparisions-loose.

          And it would be better to avoid several nested elsif statements; for a situation like that it's better to use switch:
          http://php.net/manual/en/control-structures.switch.php

          Anyway, you might want to give php.net a read...
            • 48586
            • 78 Posts
            I'm sure that I'm over-thinking this, and there has to be an easier way to do it. Basically, I have some checkboxes in a chunk, and I am trying to use some javascript to display certain divs based on what is checked. I am placing my div id's in a document that is returning values from my snippet. I am very new to ModX, only been working with it for a little over a week, so piecing it together has been a little sketchy.

            I just want to make sure I am on a good track here, should my snippet code look more like:

            switch ($filter) {
            
            case "value1":
            
            $query = $modx->newQuery('bundles');
            $query->where(array(
              package_type => package1
            ));
            $query->sortby(priority);
            
            $packages = $modx->getCollection('bundles', $query);
            
            foreach($packages as $package) {
                $fields = $package->toArray();
                $output .= $modx->getChunk('package1_chunk_output', $fields);
            };
            break;
            
            case "value2":
            
            $query = $modx->newQuery('bundles');
            $query->where(array(
              package_type => package2
            ));
            $query->sortby(priority);
            
            $packages = $modx->getCollection('bundles', $query);
            
            foreach($packages as $package) {
                $fields = $package->toArray();
                $output .= $modx->getChunk('package2_chunk_output', $fields);
            };
            break;
            }


            Then have my document code look like:

            <div id="1" style="display:none;">[[!snippetName? &filter=`value1`]]</div>
            <div id="2" style="display:none;">[[!snippetName? &filter=`value2`]]</div>
            <div id="3" style="display:none;">[[!snippetName? &filter=`value3`]]</div>


            Currently, in my document code with all the if's based on filter values, when I check a box it should only be calling div id="1", but it is returning everything like:

            package1_chunk_output
            package2_chunk_output
            package3_chunk_output
            package4_chunk_output

            Do you think updating my snippet to switch cases may help that? Any other suggestions? Thank you.
              • 3749
              • 24,544 Posts
              It looks good to me. Remember that you can add other placeholders to the Tpl chunks besides the ones you get from toArray(). You can add unique classes or set the IDs in your PHP loop if that works better. Any key => value pair you add to the $fields array will replace a placeholder with the key name.
                Did I help you? Buy me a beer
                Get my Book: MODX:The Official Guide
                MODX info for everyone: http://bobsguides.com/modx.html
                My MODX Extras
                Bob's Guides is now hosted at A2 MODX Hosting