We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 34017
    • 898 Posts
    I have a snippet that grabs a number of documents to display. What API call do I need to get the generated [*content*] field for that? As in, I want the TV values calculated and snippets ran (if there are any).
      Chuck the Trukk
      ProWebscape.com :: Nashville-WebDesign.com
      - - - - - - - -
      What are TV's? Here's some info below.
      http://modxcms.com/forums/index.php/topic,21081.msg159009.html#msg1590091
      http://modxcms.com/forums/index.php/topic,14957.msg97008.html#msg97008
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      It might be easier to just use Ditto. It’s hard to imagine any scenario where it won’t work. You can even use it in your snippet, with the $modx->runSnippet(’Ditto’, array(’param1’ => $value1, etc, etc)) function if you need to feed it variable parameter values.
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 3749
        • 24,544 Posts
        Quote from: ProWebscape at Feb 11, 2008, 04:07 AM

        I have a snippet that grabs a number of documents to display. What API call do I need to get the generated [*content*] field for that? As in, I want the TV values calculated and snippets ran (if there are any).

        Susan’s suggestion is a good one, but if you want to process a document’s content directly, this code I scavenged from the EZfaq snippet might help:

        $docID = $yourDocIDHere;
        
        $doc = $modx->getDocument((string)$docID, '*', 1); // Search published first.
        
        if (empty($doc)) {
            $doc = $modx->getDocument((string)$docID, '*', 0); // Un-published next?
        }
        
        if (empty($doc)) { // requested a non-existing document
           return $faq_lang['ezfaq-doc-not-found'];
        }
        
        $docString = $doc['content'];
        
        $docArray = explode($delimiter,$docString)
        ;
        foreach($docArray as $value) {
             // do your thing here
        }


        You’ll have to set your own $delimiter. EZfaq uses "Q:" and "A:" for that variable. Or you could just operate on $docString as a giant string with the whole content in it.

        getDocument() should really be in the API listing, IMHO, along with the $doc[’element’] options.



        Bob
          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
          • 22303 MODX Staff
          • 10,725 Posts
          Keep in mind that most TV’s and snippets are not going to be valid when not being processed on the page being viewed. For instance, a lot of snippets use $modx->documentIdentifier to determine which page is executing, but if you are manually processing this page outside of the core rendering process, it will not be set as expected; same with $modx->documentObject.

          There will be ways to do this "properly" in the upcoming API.
            • 34017
            • 898 Posts
            I really dont want to use Ditto for this, because each content has a variable layout. Could I use any of these functions directly:
            parseDocumentSource($source);
            
            //or any of the below
                        // combine template and document variables
                        $source= $this->mergeDocumentContent($source);
                        // replace HTMLSnippets in document
                        $source= $this->mergeChunkContent($source);
                        // find and merge snippets
                        $source= $this->evalSnippets($source);
                        // find and replace Placeholders (must be parsed last) - Added by Raymond
                        $source= $this->mergePlaceholderContent($source);
            


              Chuck the Trukk
              ProWebscape.com :: Nashville-WebDesign.com
              - - - - - - - -
              What are TV's? Here's some info below.
              http://modxcms.com/forums/index.php/topic,21081.msg159009.html#msg1590091
              http://modxcms.com/forums/index.php/topic,14957.msg97008.html#msg97008
              • 22303 MODX Staff
              • 10,725 Posts
              You can, but it’s not recommended because:

              a) these functions are deprecated in the new core
              b) they are meant to be private functions anyway, not public API
              c) it still doesn’t solve the documentObject/documentIdentifier problem

              Ditto is being recommended here, because it best deals with all 3 of these problems. And even though it does use some of the deprecated/private API functions, it will not in the new core, without sacrificing any functionality; your upgrade path will be protected.
                • 34017
                • 898 Posts
                Thanks Jason,

                (Im a hard-headed son-of-a... sometimes)

                So I am going along and have made a modified version of mergeDocumentContent. It accepts a doc_id and converts all of its TVs. Right now it echos because I’m still testing. I thought I could give updates as I’m working on this.


                There are 2 reasons I dont want to use Ditto for this:
                1. Each document has different TVs
                2. I dont want to create a TV for each ’template’.
                3. I’m a masochist undecided

                Edit: Here’s the call I have so far:
                $doc_ids = '1,2,3'; //example doc_ids. real input comes from TV values
                
                function mergeDocumentContentX($doc_id) {
                		global $modx;
                		
                		$my_doc = $modx->getTemplateVarOutput('*',$doc_id);
                		$template = $my_doc['content'];
                		
                		//return $template;
                		$replace= array ();
                		preg_match_all('~\[\*(.*?)\*\]~', $template, $matches);
                		$variableCount= count($matches[1]);
                		$basepath= $modx->config["base_path"] . "manager/includes";
                		for ($i= 0; $i < $variableCount; $i++) {
                				$key= $matches[1][$i];
                				$key= substr($key, 0, 1) == '#' ? substr($key, 1) : $key; // remove # for QuickEdit format
                				$value = $my_doc[$key];
                				if (is_array($value)) {
                						include_once $basepath . "/tmplvars.format.inc.php";
                						include_once $basepath . "/tmplvars.commands.inc.php";
                						$w= "100%";
                						$h= "300";
                						$value= $modx->getTVDisplayFormat($value[0], $value[1], $value[2], $value[3], $value[4]);
                				}
                				$replace[$i]= $value;
                		}
                		$template= str_replace($matches[0], $replace, $template);
                
                		return $template;
                }
                
                foreach ($doc_ids as $sb_documents)
                {
                	$output .= mergeDocumentContentX($sb_documents);
                }
                
                echo $output;
                
                  Chuck the Trukk
                  ProWebscape.com :: Nashville-WebDesign.com
                  - - - - - - - -
                  What are TV's? Here's some info below.
                  http://modxcms.com/forums/index.php/topic,21081.msg159009.html#msg1590091
                  http://modxcms.com/forums/index.php/topic,14957.msg97008.html#msg97008
                  • 28042 ☆ A M B ☆
                  • 24,524 Posts
                  And it also will help you understand better how document processing is done, although that’s of questionable use right now since the new core will do it totally differently. But in my opinion no knowledge or understanding is wasted, so I’m following your progress with interest.
                    Studying MODX in the desert - http://sottwell.com
                    Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                    Join the Slack Community - http://modx.org
                    • 34017
                    • 898 Posts
                    Well it looks like the above function works as expected. Snippets are still run as well as chunks. And all TVs are replaced with their values. One caveat, if a snippet used documentIdentifier, then it wont work correctly since the documentidentifer will return the current page instead of the one you are pulling via the snippet.

                    Why use this?
                    - If you have a list of pages you wish to include on a page.
                    - Each page has different TV’s (which would make a Ditto Template more difficult tho you could use phx)
                    - You dont want to use Ditto

                    This is a quick way to include documents in a page (could be main content or sidebar items).

                    My Snippet Call:
                    [[parseDocsX? $docids=`1,2,3` ]] OR
                    [[parseDocsX? $docids=`[*myTemplateVariable*]` ]] (where myTemplateVariable is a TV with delimited list widget).

                    I got this idea from Create_TV-Based_Chunks (http://wiki.modxcms.com/index.php/Create_TV-Based_Chunks), but used Documents instead.
                      Chuck the Trukk
                      ProWebscape.com :: Nashville-WebDesign.com
                      - - - - - - - -
                      What are TV's? Here's some info below.
                      http://modxcms.com/forums/index.php/topic,21081.msg159009.html#msg1590091
                      http://modxcms.com/forums/index.php/topic,14957.msg97008.html#msg97008
                      • 34017
                      • 898 Posts
                      I’ve gotten the snippet to parse everything and all looks good. But it’s not setting the placeholders. I know Im missing something simple, could anyone look at this and give me an idea?

                      My snippet call: [!sidebarParser? &chunks=`[*getSidebar*]` !] (sidebar value is 1,2,3)

                      In my page the placeholder would be:
                      [+sidebar.left+] or [+rnStrName+]


                      <?php
                      //File assets/snippets/testing/sidebarParser.inc.php
                      global $modx;
                      
                      //You can use these values as example
                      $ar_sidebar = '1,2,3';
                      
                      //$ar_sidebar = split(',',$chunks);
                      //$ar_sidebar = array_unique($ar_sidebar);
                      
                      $ar_sb_exclude = split(',',$excludeDocs);
                      $ar_sb_exclude = array_unique($ar_sb_exclude);
                      
                      setPlaceholders($ar_sidebar);
                      
                      
                      function setPlaceholders($doc_ids) {
                      		global $modx;
                      
                      		$placeholders_array = getContentX($doc_ids);
                      
                      		foreach ($placeholders_array as $key => $value) {
                      			//echo "<br/>Value: $value<br/><br/>";
                      			$placeholder_name = 'sidebar.'.$key;
                      
                      			$modx->setPlaceholder($placeholder_name,$value);
                      			echo "<br/>Key: $key<br/>";
                      			echo "Placeholder Name: $placeholder_name<br/><br/>";
                      		}
                      
                      }
                      
                      /*	run mergeDocumentX, merge [content]	with styling, and position. Create array for setPlaceholders		*/
                      function getContentX($array)
                      {
                      	foreach ($array as $doc_id)
                      	{
                      		$my_wrapper = "\n<div class='{STYLING}'>\n\t<div><div><div>\n\t\t{CONTENT}\n\t</div></div></div>\n</div>\n";
                      		$output     = mergeDocumentContentX($doc_id);
                      		$template  	= $output[template];
                      
                      		//These are two TVs, I've setup
                      		//You can use
                      		$sidebar_placement = 'left';
                      		$sidebar_styling   = 'left_side';
                      		//$sidebar_placement = $output[my_doc][sidebar_placement];
                      		//$sidebar_styling   = $output[my_doc][sidebar_styling];
                      
                      		$my_wrapper = str_replace('{STYLING}', $sidebar_styling, $my_wrapper);
                      		$template   = str_replace('{CONTENT}', $template, $my_wrapper);
                      
                      		$my_placements[$sidebar_placement] .= $template;
                      
                      		//print_d($template);
                      
                      	}
                      
                      	return $my_placements;
                      }
                      
                      /*	Parse all TVs in the [content] of a document. $doc_id is the id of the document	*/
                      function mergeDocumentContentX($doc_id) {
                      		global $modx;
                      
                      		$my_doc = $modx->getTemplateVarOutput('*',$doc_id);
                      		$template = $my_doc['content'];
                      
                      		$replace= array ();
                      		preg_match_all('~\[\*(.*?)\*\]~', $template, $matches);
                      		$variableCount= count($matches[1]);
                      		$basepath= $modx->config["base_path"] . "manager/includes";
                      		for ($i= 0; $i < $variableCount; $i++) {
                      				$key= $matches[1][$i];
                      				$key= substr($key, 0, 1) == '#' ? substr($key, 1) : $key; // remove # for QuickEdit format
                      				$value = $my_doc[$key];
                      				if (is_array($value)) {
                      						include_once $basepath . "/tmplvars.format.inc.php";
                      						include_once $basepath . "/tmplvars.commands.inc.php";
                      						$w= "100%";
                      						$h= "300";
                      						$value= $modx->getTVDisplayFormat($value[0], $value[1], $value[2], $value[3], $value[4]);
                      				}
                      				$replace[$i]= $value;
                      		}
                      		$template= str_replace($matches[0], $replace, $template);
                      		//return $template;
                      
                      		$return_vals = array("my_doc" => $my_doc, "template" => $template);
                      		return $return_vals;
                      }
                      
                      
                      function print_d($array){
                      	echo "<pre>PRE<br/>\n";
                      	print_r($array);
                      	echo "</pre>\n";
                      }
                      
                      ?>
                      



                      <?php
                      //Snippet
                      //tried this but its not setting either
                      $modx->setPlaceholder('rnStrName', 'T E S T I N G');
                      
                      $snipPath = $modx->config['base_path'] . "assets/snippets/testing/";
                      include_once $snipPath."sidebarParser.inc.php";
                      
                      ?>
                      



                      Edited: Commented out TV’s with static replacements to make debugging easier. Any help would be sooooo appreciated- rackin’ my brain!
                        Chuck the Trukk
                        ProWebscape.com :: Nashville-WebDesign.com
                        - - - - - - - -
                        What are TV's? Here's some info below.
                        http://modxcms.com/forums/index.php/topic,21081.msg159009.html#msg1590091
                        http://modxcms.com/forums/index.php/topic,14957.msg97008.html#msg97008