We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 9102 ☆ A M B ☆
    • 318 Posts
    OK, I figured out what the problem is -- my snippet is too simple, it doesn't pass the values of the current resource into the chunk.

    I have a fix that works without TVs -- with TVs it gets a bit more complicated. I'm working on that, I should have it for you in a few minutes.

    I'm glad this came up, actually, as this new snippet will be much more useful to me as well.
      • 9102 ☆ A M B ☆
      • 318 Posts
      Got it! I've modified the valueToChunk snippet to work inside a getResources template. It requires some additional parameters. I took some of the code from the getResources snippet itself -- I hope opengeek doesn't mind too much.

      Here is the snippet:

      <?php
      $chunks = array();
      $properties = array();
      parse_str($chunkNames,$chunks);
      
      if (!empty($thisId)){
          $includeTVs = !empty($includeTVs) ? true : false;
          $includeTVList = !empty($includeTVList) ? explode(',', $includeTVList) : array();
          $processTVs = !empty($processTVs) ? true : false;
          $processTVList = !empty($processTVList) ? explode(',', $processTVList) : array();
          $includeContent = !empty($includeContent) ? true : false;
          $fields = array_keys($modx->getFields('modResource'));
          if (empty($includeContent)) {
              $fields = array_diff($fields, array('content'));
          }
          $resource = $modx->getObject('modResource',$thisId);
          $properties = $includeContent ? $resource->toArray() : $resource->get($fields);
          if($includeTVs){
              $tvPrefix = isset($tvPrefix) ? $tvPrefix : 'tv.';
              $templateVars = array();
              if (!empty($includeTVs) && !empty($includeTVList)) {
                  $templateVars = $modx->getCollection('modTemplateVar', array('name:IN' => $includeTVList));
              }else{
                  $templateVars = $resource->getMany('TemplateVars');
              }
              $tvs = array();
              foreach ($templateVars as $tvId => $templateVar) {
                  if (!empty($includeTVList) && !in_array($templateVar->get('name'), $includeTVList)) continue;
                  if ($processTVs && (empty($processTVList) || in_array($templateVar->get('name'), $processTVList))) {
                      $tvs[$tvPrefix . $templateVar->get('name')] = $templateVar->renderOutput($resource->get('id'));
                  } else {
                      $tvs[$tvPrefix . $templateVar->get('name')] = $templateVar->getValue($resource->get('id'));
                  }
              }
              $properties = array_merge($properties,$tvs);
          }
      }
      if ($chunks[$value]) {
          return $modx->getChunk($chunks[$value], $properties);
      }else{
          return'';
      }
      


      The call now needs to include more parameters so that it passes field values from the current resource and takes into account things like whether you're including template variables.

      [[!valueToChunk? &value=`[[+parent]]` &thisId=`[[+id]]` &includeContent=`[[+includeContent]]` &includeTVs=`[[+includeTVs]]` &includeTVList=`[[+includeTVList]]` &processTVs=`[[+processTVs]]` &processTVList=`[[+processTVList]]` &tvPrefix=`[[+tvPrefix]]` &chunkNames=`2=blogPost2&3=blogPost3&4=blogPost4&5=blogPost5&6=blogPost6&7=blogPost7&10=blogPost10`]]
      
      


      I think maybe I'll polish this up a bit and publish it. Maybe it would be useful to others. I'm not sure what to call it though -- valueToChunk doesn't seem like it describes what the snippet does now. Hmmm... [ed. note: esnyder last edited this post 14 years, 10 months ago.]
        • 1761
        • 20 Posts
        IT WORKS!!!!

        I am really, really happy!!! Thank you so much!

        It does blow my mind that you can write this so quickly!

        valueToChunk is probably not the best name for it. Maybe getMyTemplate ?

        Is it possible that you let me know when you've polished and published this?
          • 9102 ☆ A M B ☆
          • 318 Posts
          Nice! It works for me too, I now have different templates for webinars on different topics, and I should be able to fix my newsletter template so that it doesn't use 5 different getResources calls.

          Actually, it occurred to me that it would be much more efficient and clean to do this inside of getResources itself. That way there's no need to get the resource fields a second time, or call another snippet which will slow things down a bit.

          I think I'll add the feature to getResources and put in a pull request to opengeek. If he includes it in getResources that would be very cool. If not, then I'll put it together as a separate package.
            • 9102 ☆ A M B ☆
            • 318 Posts
            AnnysLM, you may also want to read this thread: https://forums.modx.com/thread/?thread=72575&page=1

            Bruno17 posted a snippet that (with a bit of modification) does something similar and should perform quite a bit better because instead of getting all of the resource values again, it just uses the existing placeholders. The technique would also work with my original simple little valueToChunk snippet -- the trick is to call the snippet cached, and include a parameter for the id of the current resource.

            I put that idea together with some code from the If snippet, changed the name as you suggested, changed the names of some parameters and changed the array of value-chunk pairs to JSON and came up with this:

            <?php
            /**
             * getMyChunk
             *
             * Copyright 2009-2011 by Jason Coward <[email protected]>, Shaun McCormick
             * <[email protected]>, and Eli Snyder <[email protected]>
             *
             * getMyChunk is free software; you can redistribute it and/or modify it under the terms
             * of the GNU General Public License as published by the Free Software
             * Foundation; either version 2 of the License, or (at your option) any later
             * version.
             *
             * If is distributed in the hope that it will be useful, but WITHOUT ANY
             * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
             * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
             *
             * You should have received a copy of the GNU General Public License along with
             * If; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
             * Suite 330, Boston, MA 02111-1307 USA
             *
             * @package getMyChunk
             */
            /**
             * Snippet to conditionally select a chunk from a JSON array of operand-chunk pairs.
             *
             * @package getMyChunk
             */
            if (!empty($tplCondition) && !empty($conditionalTpls)) {
                	  $conTpls = $modx->fromJSON($conditionalTpls);
                	  if (!empty($scriptProperties['tplCondition'])){
                          $subject = $modx->getPlaceholder($scriptProperties['tplCondition']);
                      }
                	  $tplOperator = !empty($tplOperator) ? $tplOperator : '=';
                	  $tplOperator = strtolower($tplOperator);
                	  $tplCon = '';
                    foreach ($conTpls as $operand => $conditionalTpl) {
                        switch ($tplOperator) {
                            case '!=':
                            case 'neq':
                            case 'not':
                            case 'isnot':
                            case 'isnt':
                            case 'unequal':
                            case 'notequal':
                                $tplCon = (($subject != $operand) ? $conditionalTpl : $tplCon);
                                break;
                            case '<':
                            case 'lt':
                            case 'less':
                            case 'lessthan':
                                $tplCon = (($subject < $operand) ? $conditionalTpl : $tplCon);
                                break;
                            case '>':
                            case 'gt':
                            case 'greater':
                            case 'greaterthan':
                                $tplCon = (($subject > $operand) ? $conditionalTpl : $tplCon);
                                break;
                            case '<=':
                            case 'lte':
                            case 'lessthanequals':
                            case 'lessthanorequalto':
                                $tplCon = (($subject <= $operand) ? $conditionalTpl : $tplCon);
                                break;
                            case '>=':
                            case 'gte':
                            case 'greaterthanequals':
                            case 'greaterthanequalto':
                                $tplCon = (($subject >= $operand) ? $conditionalTpl : $tplCon);
                                break;
                            case 'isempty':
                            case 'empty':
                                $tplCon = empty($subject) ? $conditionalTpl : $tplCon;
                                break;
                            case '!empty':
                            case 'notempty':
                            case 'isnotempty':
                                $tplCon = !empty($subject) && $subject != '' ? $conditionalTpl : $tplCon;
                                break;
                            case 'isnull':
                            case 'null':
                                $tplCon = $subject == null || strtolower($subject) == 'null' ? $conditionalTpl : $tplCon;
                                break;
                            case 'inarray':
                            case 'in_array':
                            case 'ia':
                                $operand = explode(',',$operand);
                                $tplCon = in_array($subject,$operand) ? $conditionalTpl : $tplCon;
                                break;
                            case '==':
                            case '=':
                            case 'eq':
                            case 'is':
                            case 'equal':
                            case 'equals':
                            case 'equalto':
                            default:
                                $tplCon = (($subject == $operand) ? $conditionalTpl : $tplCon);
                                break;
                        }
                    }
                	  if(!empty($tplCon)) {
                        return $modx->getChunk($tplCon);
                    }
                }
            return '';
            


            You have to call it cached inside the getResources template and include a dummy parameter for the resource id. So in your case you would call it like this:

            [[getMyChunk? &tplCondition=`parent` &tplOperator=`is` &conditionalTpls=`{"2":"blogPost2", "3":"blogPost3","4":"blogPost4","5":"blogPost5","6":"blogPost6","7":"blogPost7","10":"blogPost10"}` &resid=`[[+id]]`]]
            
            [ed. note: esnyder last edited this post 14 years, 10 months ago.]
              • 1761
              • 20 Posts
              Hi Freejung,

              I changed the snippet and the rest in my website and this also works. I have to be honest and say that I really don't know what the difference is... You do explain some of it but for a big part it is gibberish for me. The tread you suggested has the same langueage ;-) I'll have to read it a few more times and maybe it will make some more sense to me. There's still a lot of things I have to learn!
              I always thought that my English was reasonably well but I'm finding my self having some difficulty getting the explanations.
              Thank you very much for this update!


                • 9102 ☆ A M B ☆
                • 318 Posts
                Basically, the difference is that I think this latest version will run faster.