We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 18397
    • 3,250 Posts
    This is a mockup of how I would like the Ditto 2.0 snippet source code to look like. Please give feedback as to function names and architecture.

    <?php
    
    // Ditto 2.0
    
    // PARAMETERS WILL GO HERE
    
    $ditto = new ditto($format,$language,$debug);
    	// create a new Ditto instance in the specified format and language with the requested debug level
    
    $ditto->loadExtenders($extenders);
    	// load the requested extenders into the class
    
    $ditto->parseTemplates($templates);
    	// parse the templates for TV's and store them for later use
    	
    $filter = $ditto->parseFilters($textFilter,$dateFilter,$tagFilter);
    
    $documentIDs = $ditto->getDocumentIDs($IDs, $IDType, $ditto->TVs, $sortBy, $sortDir, $depth, $showPublishedOnly, $seeThruUnpub, $hideFolders, $showInMenuOnly, $where, $keywords, $limit, $filter);
    	// retrieves a list of document IDs that meet the criteria and populates the $resources array with them
    	
    $sortOrder = $ditto->setSortOrder($documentIDs);
    	// saves the order of the documents for use later
    
    if($paginate == 1) {
    	$documentIDs = $ditto->paginate($documentIDs, $start, $stop, $perPage);
    		// paginates the documents array so that it only contains a single page worth of documents
    	$stop = $ditto->stop;
    }
    
    $documents = $ditto->getDocuments($documentIDs);
    	// executes the SQL query but DOES NOT retrieve the row
    
    $output = $ditto->header;
    	// initialize the output variable and send the header
    	
    for ($x=0; $x<$stop; $x++) {
    	$resource = $ditto->getDocument($document);
    		// retrieves the row into an associative array
    	$template = $ditto->determineTPL($templates,$x,$start,$stop,$modx->documentIdentifier);
    		// choose the template to use and set the code of that template to the template variable
    	$output .= $ditto->render($resource, $template, $targetIDs, $removeChunk);
    		// render the output using the correct template, in the correct format and language, with the right target IDs and the correct chunk removed
    }
    
    $output .= $ditto->footer;
    	// send the footer
    	
    return $output;
    
    ?>
    
      • 23491 ☆ A M B ☆
      • 1,056 Posts
      Nice Mark!

      Only suggestion is possibly you could preface certain var names with $ditto or even just $d as per the creating snippets (wiki)

      e.g.

      $sortOrder to $dittoSortOrder
      $filter to $dittoFilter
      etc...
      
        Mike Reid - www.pixelchutes.com
        MODx Ambassador / Contributor
        [Module] MultiMedia Manager / [Module] SiteSearch / [Snippet] DocPassword / [Plugin] EditArea / We support FoxyCart
        ________________________________
        Where every pixel matters.
        • 18397
        • 3,250 Posts
        That is a good practice in certain cases but not most current MODx snippets.

        Can you image a snippet call like so [!Ditto?dittoParent=`foo`&dittoSortBy=`bar` .... !]? It would be impractical. Plus, the current method of using eval means all of the variables are localized and will be unset after the run.

        When 0.9.7 is released this may need to be re-evaluated depending on how the new caching system handles parameters. But until then....


        (I realize you are referring to the internal variables but it is not relevant because the parser creates the variables as they are passed to it so if you pass foo you get a var foo.)
          • 23491 ☆ A M B ☆
          • 1,056 Posts
          Quote from: Mark at Jan 06, 2007, 08:13 PM

          (I realize you are referring to the internal variables but it is not relevant because the parser creates the variables as they are passed to it so if you pass foo you get a var foo.)

          That is good to know! So if I run [!SnippetA!] (which creates var $foo, (but does not unset ...bad snippet!), and then I run [!SnippetB!] which also creates var $foo, would $foo/$foo’s value from [!SnippetA!] be present/accessible in the [!SnippetB!] name space before the second $foo is created? (if that makes sense)
            Mike Reid - www.pixelchutes.com
            MODx Ambassador / Contributor
            [Module] MultiMedia Manager / [Module] SiteSearch / [Snippet] DocPassword / [Plugin] EditArea / We support FoxyCart
            ________________________________
            Where every pixel matters.
            • 18397
            • 3,250 Posts
            MODx runs current snippets in eval() which keeps all variables created localized and thus when the eval finishes the variables are destroyed.
              • 10487 MODX Staff
              • 1,535 Posts
              Not quite true, from the eval() PHP help page:
              Also remember that variables given values under eval() will retain these values in the main script afterwards.
              There was a bug in the early 0.9.5 beta builds where there was an issue with the $_lang variable between QuickEdit and Ditto, where Ditto was overwriting the QuickEdit language strings and subsequently QuickEdit was being displayed blank in the frontend. (This was solved by prefixing the QuickEdit language variable, ie. $QE_lang)

              So, in some instances, it is possible for variables to conflict between resources (although, I admit, I haven’t tested this any further)
                Garry Nutting
                Senior Developer
                MODX, LLC

                Email: [email protected]
                Twitter: @garryn
                Web: modx.com
                • 23491 ☆ A M B ☆
                • 1,056 Posts
                I’m sure as I continue more advanced, web based applications + mash ups, etc that I will find out the "hard way" and will be sure to post any peculiarities.
                  Mike Reid - www.pixelchutes.com
                  MODx Ambassador / Contributor
                  [Module] MultiMedia Manager / [Module] SiteSearch / [Snippet] DocPassword / [Plugin] EditArea / We support FoxyCart
                  ________________________________
                  Where every pixel matters.
                  • 18397
                  • 3,250 Posts
                  I stand corrected. It appears the PHP book I have is incorrect. The PHP site does say exactly that.
                    • 27376
                    • 576 Posts
                    Also remember that variables given values under eval() will retain these values in the main script afterwards.
                    I think what the documentation means by "main script" is "current scope" meaning, since evalSnippet is a function, and all variables defined inside functions have local scope (except those defined using the ’global’ keyword), they will be destroyed at the end of the function call.

                    I’m fairly certain this is true for PHP version > 4 but can’t say anything for lower versions.