We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 29525
    • 388 Posts
    TobyL

    You snippet above is EXACTLY what I need for a project right now! But I can’t get it to display the contents of the documents I’ve selected.

    My TV contains this:
    @SELECT pagetitle,id FROM terrybar_site_content WHERE parent=49 ORDER BY menuindex


    I’ve assigned the TV to the template I’m using and I"ve put the snippet call in the document where I’d like the specified content to appear.

    When editing the document with this template, the TV provides the correct list of documents in parent folder 49 as expected. I select the document I want and save.

    However, there is no content output to my page.

    Can you help me sort this out?

    Terry
      www.terrybarthdesign.com
      • 30223
      • 1,010 Posts
      So sorry,... I’m closed for Christmas. In fact I’m heading out the door right now to a place without cables and without wireless. I’ll revisit when I’m back in 2 or 3 days.

      Merry Christmas smiley
        • 29525
        • 388 Posts
        Thanks TobyL.

        Have a great holiday.

        Terry
          www.terrybarthdesign.com
          • 30223
          • 1,010 Posts
          It seems I made a typo in the original snippet I posted above... I declared $includeTemplate at the top but used $blockTemplate later on in the code...

          Try this instead:
          <?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'],$includeTemplate);
                $item = str_replace('[+includedBody+]',$doc['content'],$item);
                $output .= $item;
             }
          }
          return $output;
          
          ?>
          
          
            • 29525
            • 388 Posts
            TobyL

            Thank You for taking a look. I used the new snippet code. No change.

            I’ve been testing this on my wamp server. Since I still couldn’t get it work with the new snippet, I thought perhaps I missed a step. So started from scratch, this time on my server site. No change.

            I feel silly about this, it seems so straight forward. . . Can I pm you access to my site for a closer look?

            Terry

              www.terrybarthdesign.com
              • 30223
              • 1,010 Posts
              Yep,.. PM me and I’ll have a quick look..
                • 30223
                • 1,010 Posts
                All fixed.

                In a nutt shell:
                1. typo in my code posted above where I accidentally introduced a space in $modx->[space]getTemplateVarOutput (corrected original posts)
                2. The TV should have had a "Delimited list" widget assigned with a comma as separator.
                3. Your TV was called includesDocs while the snippet tries to get output from includeDocs
                4. My short term memory is failing... don’t remember what the fourth issue was.

                I’ve left some debug code in the snippet (commented out). Feel free to remove.
                  • 29525
                  • 388 Posts
                  TobyL

                  Fantastic!! Thank you! Works like a charm.

                  If I could indulge one more time. I don’t quite understand one thing.

                  It appears that the document selection is done on the container doc, which then displays the selected content on the parent to that container document?

                  Is this correct?

                  Web Page (selected docs appears here)
                  Container Doc (make selection here)
                  Doc 1
                  Doc 2
                  Doc 3

                  I had assumed that the selection would be made on the actual document where I want the output to appear using the TV on that document. It took me a while to figure this out. After you fixed up my page, I was trying to make some selections on the web page, but it didn’t work until I realized I was selecting the docs on the wrong page.

                  My plan was to use this to select sidebar content from a central container for a variety of pages through a site. The idea was to have a set of content documents in one folder (created by the non technical owner of the site as they need them), then give them the ability to select that content on various pages in the sidebar as appropriate using the TV for each page. Some content would be used on multiple pages.

                  If the selected documents are output only to the parent of the container, then the content would not be available to other pages on the site.

                  Is that correct?
                    www.terrybarthdesign.com
                    • 29525
                    • 388 Posts
                    Ok, I’ve sorted this out.

                    This snippet is working perfectly and it does work in the way I had expected.

                    It turns out the alias on the parent document was pointing to the container document. Consequently I wasn’t really seeing the parent document but instead I was seeing selections made on the container doc.

                    So I’m good to go! My clients are going to love this!

                    Thank you.
                      www.terrybarthdesign.com
                      • 40860
                      • 1 Posts
                      I was glad to find this - got it working after a bit of trial and error, so thanks very much for posting this great solution to a problem.

                      I thought I might mention to any people discovering this snippet for the first time that if you copy and paste the final example of the snippet, you might want to change the publish parameter in the getDocuments call from 0 to 1, in order to save yourself the trouble of smacking yourself in the head when you realise that is why the snippet wasn't picking up your published documents.