We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 44891
    • 35 Posts
    I have a resource called FrontPage (id=1). In this frontpage I have several chunk calls for layout purposes. All the chunks have the same layout but have to show different content. For example, the chunk (newsTpl) looks like:
    <div class="section-gray" id="news">
    	<div class="container">
    		<div class="title">
    			<h2>
    				HERE COMES THE PAGETITLE
    			</h2>
    		</div>
    
    		<h4 class="description">
    			HERE COMES THE DESCRIPTION
    		</h4>
    
    		<div class="row">
    			<div class="col-md-4 col-sm-6">
    				<div class="blog">
    					<div class="body text-center">
    						<div class="introtext">
    							<p>
    								HERE COMES THE INTROTEXT
    							</p>
    						</div>
    
    						<div class="content">
    							HERE COMES THE CONTENT
    						</div>
    					</div>
    				</div>
    			</div>
    		</div>
    	</div>
    </div>
    


    What I want is to pull in pagetitle, description, introtext and content from a different resource (id=69) into the right places of the chunk.
    For better understanding, imaging a newspaper frontpage where everyday the content changes on that frontpage. Would be handy when i just have to change the resourceId to get quickly new content in the chunks. I hope I explained myself clearly. How to pull this of?

    Using:MODX Revolution 2.6.1-pl

    This question has been answered by BobRay. See the first response.

    • discuss.answer
      • 3749
      • 24,544 Posts
      It's kind of tricky, since there are already placeholders set for those fields.

      I think I would do this:

      Create a System Setting with the key: news_page and put the ID there. Then use these placeholders in the page content:

      Put a snippet tag in the template above the content tag:

      [[!GetNewsPlaceholders]]



      Use these placeholders at the appropriate places in the chunk:

      [[+news_description]]
      
      [[+news_introtext]]
      
      [[+news_content]]


      Then create the snippet (GetNewsPlaceholders) with this code:


      <?php
      $docId = $modx->getOption('news_page');
      
      $doc = $modx->getObject('modResource', (int) $docId);
      
      if (! $doc) {
         $modx->log(modX::LOG_LEVEL_ERROR, 'Could not get resource with ID: ' . $docId);
      
      } else {
         $placeholders = array('description', 'introtext', 'content');
         foreach ($placeholders as $ph) {
             $modx->setPlaceholder('news_' . $ph, $doc->get($ph));
         }
      }
      
      return '';
      


      [ed. note: BobRay last edited this post 6 years, 1 month ago.]
        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
        • 44891
        • 35 Posts
        OK, this is interesting stuff. But could you elaborate a little bit more because itś not clear to me.

        I have figured out 2 options for my problem.
        1. use
        [[getResourceField? &id=`71` &field=`introtext`]]
        and
        [[getResourceField? &id=`71` &field=`description`]]


        This requires to adjust the id parameter everytime. This is feasible.

        2. I pull the data straight from the database table with several snippets.
        [[!getIntroText? &id=`67`]]
        and
        [[!getdescription? &id=`67`]]
        .


        - But now back to your solution.
        "Create a System Setting with the key: news_page and put the ID there."


        What you mean by "put the ID there"? I don't see a field where to put an ID. You mean the ID of a resource?

        - How do the parameters get there values?

        [[+news_description]]
        [[+news_introtext]]
        [[+news_content]]


        BTW shouldn that be double + [[++news_introtext]]

        For clarity, I have 3 chunks like I described in the original posting. For every chunk I want to pull in data from 1 resource. So in chunk 1 goes the data from resource 69, in chunk 2 the data from resource 70 and in chunk 3 the data from resource 71. Rhe chunks are on 1 page called FrontPage with id=1.

        Thanks for your time spending on this.

        P.s. the code is missing a parenthesis { in the else statement.
          • 3749
          • 24,544 Posts
          Yes, I meant the ID of the resource you wanted to pull fields from. I didn't realize you had three pages and three chunks.

          I think your methods would work, but here are two simpler and faster ones:

          Method One

          Put these tags where you want each chunk's content displayed (use the actual names of the chunks):

          [[!GetMyChunk? &chunk=`Chunk1` &docId=`69`]]
          
          [[!GetMyChunk? &chunk=`Chunk2` &docId=`70`]]
          
          [[!GetMyChunk? &chunk=`Chunk3` &docId=`71`]]


          Create a snippet called GetMyChunk with this code:

          <?php
          $chunk = $modx->getOption('chunk', $scriptProperties, '', true);
          $docId = $modx->getOption('docId', $scriptProperties, '', true);
          
          if (empty($chunk) || empty($docId)) {
             return 'Empty Property';
          }
          
          $doc = $modx->getObject('modResource', (int) $docId);
          if (! $doc) {
              return 'Resource ' . $docId . ' not found';
          }
          
          $fields = $doc->toArray();
          return $modx->getChunk($chunk, $fields);
          
          


          Method Two

          It would be possible to make this significantly faster by calling the snippet only once and having it process and set set separate placeholders for all three chunks, but the version above may be fast enough.

          Here's how that would look (untested). These would replace the three tags above (the first one needs to go above the other three). Note that the snippet name has changed and no properties are sent:

          [[GetMyChunks]]
          
          [[+Chunk1_placeholder]]
          
          [[+Chunk2_placeholder]]
          
          [[+Chunk3_placeholder]]


          GetMyChunks snippet (untested):

          <?php
          $output = '';
          
          /* Array of chunk names and associated Doc ID. 
             Name must match the first part of the 
             placeholder exactly */
          
          $chunks = array(
              'Chunk1' => 69,
              'Chunk2' => 70,
              'Chunk1' => 71,
          );
          
          foreach ($chunks as $chunkName => $docId) {
              $fields = array();
          
              /* Get the resource */
              $doc = $modx->getObject('modResource', $docId);
          
              /* Make sure we got it */
              if (! $doc) {
                  $output .= '<br>Resource ' . $docId . ' not found';
              } else {
                  /* Get Resource's fields into an array for getchunk */  
                  $fields = $doc->toArray();
                  
                  /* Get chunk with placeholders replaced by field values*/
                  $chunkContent = $modx->getChunk($chunkName, $fields);
          
                  /* Set the chunk's specific placeholder */
                  $modx->setPlaceholder($chunkName . '_placeholder', $chunkContent);
              }
          }
          /* return nothing (hopefully) or any errors */
          return $output;


          This is much faster because (1) It only calls the snippet once, and (2) unlike the other snippet, it can be called uncached. It's slightly flawed in that you have to hard-code the chunk names and Resource ID's in the snippet. They could be sent as properties in the snippet tag, but it would be more complicated and would slow things down, since they'd all have to parsed.

          Another plus is that it's largely immune to parsing-order issues, since it gets everything at the same time with a simple snippet tag (i.e., no nesting).


          [ed. note: BobRay last edited this post 6 years, 1 month ago.]
            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
            • 44891
            • 35 Posts
            Whawwww, this is really cool. Both methods work like a charm. I really appreciate the time you put in to come up with these solutions. The benefit is indeed that I don't have to call a lot of snippets. Very good work. Thanks Bob. This question can go in the books as solved.
              • 3749
              • 24,544 Posts
              Thanks for the kind words. I'm glad I could help. 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