We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 23322
    • 7 Posts
    I just want to retrieve the content of another page (by id) into the main page by putting into the main page’s template the page id of the other page.
    Something like this:
    [*content&id=`12`*]

    TIA! smiley
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      You can use a TV with the @DOCUMENT binding. I don’t believe that any MODx tags in the document’s content will be processed, however.

      http://modxcms.com/document-binding.html
        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
        • 23322
        • 7 Posts
        Yeah, I looked into that too, but I don’t know how to set up the template variable’s input to accept a parameter from the template.
        So, I put @DOCUMENT in "Input option values", with a type like ’textarea’, then call it in the template by doing this:
        [*ContentBlock?id=`12`*]
        Why doesn’t this work ? undecided
          • 28042 ☆ A M B ☆
          • 24,524 Posts
          Ah, sorry, I didn’t notice that you wanted the ID to be variable.
            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
            • 23322
            • 7 Posts
            It’s okay. Would it work if I set the TV input field like this? id==@DOCUMENT
            ...am I on the right track? :-/
              • 28042 ☆ A M B ☆
              • 24,524 Posts
              I don’t think so.

              How about the GetField snippet? A snippet call I know can accept variables for its parameter values.

              http://modxcms.com/getField-667.html
                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
                • 23322
                • 7 Posts
                Egh, I looked at the documentation, but that method seems to be too involved for what I want to do.
                I just want to call content of a page into another page so that I can have different blocks of content on the frontpage; not all sites are linear, one page == one area of content.
                I’ve heard stuff about Chunks, but they’re not WYSIWYG formatted, and in a non-obvious portion of the site (not in the hierarchy)... Would be harder for my client to change.
                It really shouldn’t be this hard... sad
                  • 30223
                  • 1,010 Posts
                  Try this:


                  Create a TV with these properties:
                  Variable name: includeDocs
                  Input Type: "DropDown List Menu" or a "Listbox"
                  Input Option Values:
                  @SELECT pagetitle,id FROM {prefix_}site_content WHERE parent={page_id} ORDER BY menuindex
                  

                  where {prefix} is your modx table prefix and {page_id} is the parent container of the documents you want to be able to include. youll need to replace this yourself.(If you don’t have a parent container adjust the SQL accordingly)
                  Default Value: @INHERIT
                  Assign it to the template of your choice.

                  Now create a new snippet (name it "includeDocument" for instance) and paste the following code:

                  <?php
                  
                  $output = '';
                  /** A simple template to wrap around the included content
                  * Adjust as necessary... Classes are arbitrary - up to you*/
                  
                  $includeTemplate = <<<EOD
                  		<div class="includedDocument">
                  			<div class="xHead"><h3>[+includedTitle+]</h3></div>
                  			<div class="includedBody">
                  				[+includedBody+]
                  			</div>
                  		</div>
                  		
                  EOD;
                  
                  $docIds = $modx->getTemplateVarOutput(array('includeDocs'),$modx->documentIdentifier);
                  
                  if( count($docIds)>0 && !empty($docIds['includeDocs']) ){
                  	$includeDocs = $modx->getDocuments(explode(',',$docIds['includeDocs']), 0, 0, "id,longtitle,content");
                  
                  	foreach ($includeDocs as $doc){
                  		$item = str_replace('[+includedTitle+]',$doc['longtitle'],$blockTemplate);
                  		$item = str_replace('[+includedBody+]',$doc['content'],$item);
                  		$output .= $item;
                  	}
                  }
                  return $output;
                  
                  ?>
                  


                  Last thing to do is to place a snippet call in your document template [[includeDocument]]

                  This will give you a list of documents to choose from when you edit a page in the manager. And the snippet will fetch the document content based on the choice you made in the TV.

                  Hope this is more or less what you are trying to achieve...
                    • 23322
                    • 7 Posts
                    Hi, TobyL! Thanks for your time!

                    That’s ... almost what I need. However, your code inspired me to write my own, so here’s my version:

                    <?php
                    $output = '';
                    /*
                    This is a simple snippet for replacing placeholders with docIDs in them with the content of the docID's corresponding page.
                    Example placeholder:
                    [+12+]
                    */
                    
                    $includeTemplate = <<<EOD
                    		<div class="includedDocument">
                    			<div class="includedBody">
                    				[+includedBody+]
                    			</div>
                    		</div>
                    EOD;
                    
                    // Set to your docIDs!
                    $docIDs = array(12,16,17);
                    
                    for($i=0; $i < count($docIDs); $i++) {
                    	$doc = $modx->getPageInfo($docIDs[$i],1,'content');
                    	
                    	$item = str_replace('[+includedTitle+]',$doc['longtitle'],$includeTemplate);
                    	$item = str_replace('[+includedBody+]',$doc['content'],$item);
                    	$output .= $item;
                    	
                    	$modx->setPlaceholder($docIDs[$i], $output);
                    	$output = '';
                    }
                    ?>


                    Obviously, my pageIDs are hard-coded. It wouldn’t be too much effort to make a comma-delimited text field TV for that, however, me being a lazy programmer, I’m just going to leave it there for now. Not bad for my first MODx program! smiley

                    Thanks for all the help getting here, guys!
                      • 25883
                      • 128 Posts
                      @PsychoFalcon: Cool idea and I’m thinking about using it as a workaround to a problem I discovered.
                      @TobyL: I really think your flexible TV is a great concept too, I can see where that would be very useful if a client needed to change content seasonally (a "the ski lodge is closed for the season" page, or a restaurant’s summer menu).


                      I’m having a problem with @DOCUMENT so I found this thread and I think you have a solution. Here is the problem I was experiencing: http://modxcms.com/forums/index.php/topic,28663.msg180534.html#msg180534


                      But here are a few questions about your snippet, I’m pretty new to snippets so I appreciate your help:


                      1.) You named it "includeDocument" or something similar? The name doesn’t matter?

                      2.) From what I can tell [+12+] will call the document content, if and only if "12" is also hardcoded in the snippet under $docIDs = array. Am I correct about that?

                      3.) How would I change the code so that [+12+] will not do this but instead I would prefer to use something like [+CallDoc12+] just so I don’t get confused and so this doesn’t happen to interfere with Ditto or any other snippets or Modx functions?

                      4.) When entering and saving this as a new snippet do any boxes need to be ticked on the snippet page?


                      Thanks!

                      John