We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 44375
    • 92 Posts
    Great thanks Bob, I'll give it a go on development if I ever get this damn upgrade out the door (having .htacess issues).

    Something else, while I'm making random notes for others doing Evo->Revo, is that snippets in Revo have to start and end in PHP, by which I mean say, for example, you wanted to create a plain HTML snippet, you need to start the snippet with ?> and end it with <?php Of course you'd never have an HTML snippet, but you might have a form that has some PHP, then the HTML of the form. The HTML must be suffixed with <?php or the page will throw a server 500 error (and log nothing anywhere, as far as I can find, even in debug mode). This has got me several times for several hours. It seems nuts, but I imagine there are performance reasons. Personally I'd prefer normal PHP standards, ie, you must start with <?php and end with ?>, but I suspect someone cleverer than me made this decision.
      • 3749
      • 24,544 Posts
      I never do that. It usually makes things hard to maintain since your logic ends up being mixed up with your presentation and you have to edit snippet code any time you want to change the output.

      I generally put any non-trivial HTML in a chunk and pull it in with $modx-getChunk(). If necessary, the chunk has placeholder tags that get replaced automatically when you send a second argument to getChunk().

      For trivial HTML output that isn't worth putting in a chunk, you can create an $output variable and use .= to add to it through out the snippet (sometimes using heredoc syntax), then return it at the end.

      BTW, snippets that are pure PHP code (as they should be) don't need any PHP tags at all.

        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
        • 44375
        • 92 Posts
        Thanks Bob. (Out of interest, as I've just rolled out my project after a few late nights.)

        How about if you have PHP that validates a lot of form values and conditionally either re-displays the form, with the user's values pre-populated and validation messages, or redirects to a "Thanks for your data" page? I'm talking about a long form with 30+ input tags, images, files, etc. In this situation, as far as I know, you'd have a lot of parameters going from snippet to chunk (to pre-populate form elements). Having a self-contained PHP process, albeit with a PHP and HTML section, requiring a standard validation library, is pretty pleasant to develop with.

        I suppose the MODx way would be to use its templating, providing values the chunk can populate using [[+forename]]. I'm not quite there yet - is this easy? I still suspect the interface for a one or two-off form would be more burden than it is worth in my particular case, although not sure how many sites have long forms any more. But this is still beyond my MODx knowledge. - I'm still patting myself on the back for focusing on MODx over Wordpress..(the site behind this thread was an upgrade but I've done a few complete rewrites).
          • 3749
          • 24,544 Posts
          Using a Tpl chunk is quite easy once you get used to it and it simplifies the code quite a bit, since the code only deals with the array of fields, not the format of the content that's displayed.

          Many people use FormIt to do forms with a postHook to do any processing (though I tend to roll my own). Here's an example for a simple contact form:

          http://rtfm.modx.com/extras/revo/formit/formit.tutorials-and-examples/formit.examples.simple-contact-page



            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
            • 44375
            • 92 Posts
            Thanks very much Bob, tpls next on my MODx learning curve! FormIt looks nice too-great link thanks for an immediate understanding of what it does.
              • 3749
              • 24,544 Posts
              Believe me, once you get used to Tpl chunks, you'll wonder how you ever got along without them. smiley
                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
                • 44375
                • 92 Posts
                Couple small corrections to mapping across default values. Some getResources defaults are odd - why 5?

                There's also a missing bit on the getResources parameter descriptions - only iterating immediate children is depth zero. The zero is missing from an otherwise handy sentence.

                <?php
                global $modx;
                
                $modx->log(modX::LOG_LEVEL_DEBUG, 'sortDir  ' . $sortDir);
                $modx->log(modX::LOG_LEVEL_DEBUG, 'sortBy   ' . $sortBy);
                $modx->log(modX::LOG_LEVEL_DEBUG, 'startID  ' . $startID);
                $modx->log(modX::LOG_LEVEL_DEBUG, 'filter   ' . $filter);
                $modx->log(modX::LOG_LEVEL_DEBUG, 'tagData  ' . $tagData);
                $modx->log(modX::LOG_LEVEL_DEBUG, 'tags     ' . $tags);
                $modx->log(modX::LOG_LEVEL_DEBUG, 'display  ' . $display);
                $modx->log(modX::LOG_LEVEL_DEBUG, 'parents  ' . $parents);
                $modx->log(modX::LOG_LEVEL_DEBUG, 'total    ' . $total);
                $modx->log(modX::LOG_LEVEL_DEBUG, 'depth    ' . $depth);
                $modx->log(modX::LOG_LEVEL_DEBUG, 'tpl      ' . $tpl);
                $modx->log(modX::LOG_LEVEL_DEBUG, 'hideFolders  ' . $hideFolders);
                $modx->log(modX::LOG_LEVEL_DEBUG, 'tplLast  ' . $tplLast);
                $modx->log(modX::LOG_LEVEL_DEBUG, 'paginate ' . $paginate);
                
                $getResourcesParams = array();
                
                if (isset($startID)) 
                {
                    $parents = $startID;
                }
                if (isset($parents))
                {
                    $getResourcesParams['parents'] = $parents;
                }
                else
                {
                    // Ditto defaults to current document's children
                    $getResourcesParams['parents'] = $modx->resource->get('id');
                }
                
                if (isset($sortBy) || isset($sortDir)) {
                    $sortDir = isset($sortDir) ? strtoupper($sortDir) : 'DESC';
                    $sortBy = isset($sortBy) ? $sortBy : "createdon";
                    $getResourcesParams['sortdir'] = $sortDir;
                    $getResourcesParams['sortby'] = $sortBy;
                }
                
                if (isset($filter))
                {
                    // only works for exactly two filter conditions OR'd, using filter comparisons 1 (unequal) or 2 (equals), eg, filter=`id,328,2|template,8,1` OR filter=`createdby,2,1|template,5,1`
                    // extract the strings as so:    `createdby,2,1|`   =>   $filter_field,$filter_id,$filter_cmp
                    $filter_comma1 = strpos($filter, ",", 0);
                    $filter_comma2 = strpos($filter, ",", $filter_comma1 + 1);
                    $filter_divider = strpos($filter, "|", $filter_comma2 + 1);
                    $filter_field = substr($filter, 0, $filter_comma1);
                    $filter_id = substr($filter, $filter_comma1 + 1, $filter_comma2 - $filter_comma1 - 1);
                    $filter_cmp = substr($filter, $filter_comma2 + 1, $filter_divider - $filter_comma2 - 1);
                
                    // Ditto's filter excludes, whereas getResources's where filter includes, so we reverse the boolean operators
                    // ie, Ditto &filter=`id,328,2|template,8,1` means exclude where id=328 OR template!=8, equivalent to INCLUDE where id!=328 AND template=8, equivalent to &where=`{"id:!=":328, "AND:template:=":8}`
                    // This builds the first half, eg, translates the string   `id,328,2|`   to   `{"id:!=":328, "AND:`
                    $filter_cmp_prm = ($filter_cmp == '2') ? '!=' : '=';
                
                    $where = '{"' . $filter_field . ':' . $filter_cmp_prm . '":' . $filter_id;
                
                    if ($filter_divider)
                    {
                        $filter_comma1 = strpos($filter, ",", $filter_divider);
                        $filter_comma2 = strpos($filter, ",", $filter_comma1 + 1);
                        $filter_field = substr($filter, $filter_divider + 1, $filter_comma1 - $filter_divider - 1);
                        $filter_id = substr($filter, $filter_comma1 + 1, $filter_comma2 - $filter_comma1 - 1);
                        $filter_cmp = substr($filter, $filter_comma2 + 1);
                
                        $filter_cmp_prm = ($filter_cmp == '2') ? '!=' : '=';
                
                        $where = $where . ', "AND:' . $filter_field . ':' . $filter_cmp_prm . '":' . $filter_id . '}';
                    }
                    else
                    {
                        $where = $where . '}';
                    }
                
                    $getResourcesParams['where'] = $where;
                }
                
                if (isset($tagData))
                {
                    $tvFilters = $tagData . '==%' . $tags . '%';
                    $getResourcesParams['tvPrefix'] = '';
                    $getResourcesParams['tvFilters'] = $tvFilters;
                }
                
                if (isset($total))
                {
                    $getResourcesParams['limit'] = $total;
                }
                else
                {
                    $getResourcesParams['limit'] = 0;
                }
                if (isset($depth))
                {
                    if ($depth == 0)
                    {
                        // Ditto depth==0 means unlimited depth
                        $getResourcesParams['depth'] = 200;
                    }
                    else
                    {
                        $getResourcesParams['depth'] = $depth;
                    }
                }
                else
                {
                    $getResourcesParams['depth'] = 0;
                }
                
                if (isset($tpl))
                {
                    $getResourcesParams['tpl'] = $tpl;
                }
                if (isset($tplLast))
                {
                    $getResourcesParams['tplLast'] = $tplLast;
                }
                if (isset($hideFolders))
                {
                    $getResourcesParams['hideContainers'] = $hideFolders;
                }
                
                foreach ($getResourcesParams as $key => $value)
                {
                    $modx->log(modX::LOG_LEVEL_DEBUG, 'getResourceParams [' . $key .'] = '. $value);
                }
                
                $getResourcesParams['includeContent'] = '1';
                $getResourcesParams['includeTVs'] = '1';
                $getResourcesParams['prepareTVs'] = '1';
                $getResourcesParams['processTVs'] = '1'; 
                $getResourcesParams['tvPrefix'] = ''; 
                
                if (isset($paginate)) 
                {
                    $getResourcesParams['elementClass'] = 'modSnippet';
                    $getResourcesParams['element'] = 'getResources';
                    $getResourcesParams['pageVarKey'] = 'page';
                    $getResourcesParams['pageLimit'] = '10';
                    
                    if (isset($display))
                    {
                        $getResourcesParams['limit'] = $display;
                    }
                
                    return $modx->runSnippet('getPage', $getResourcesParams);
                }
                else
                {
                    $modx->log(modX::LOG_LEVEL_DEBUG, $modx->runSnippet('getResources',$getResourcesParams));
                    return $modx->runSnippet('getResources',$getResourcesParams);
                }