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 snippet gets a specific field (any of these http://modxcms.com/the-document-object.html) for an individual document.

    Example call that will return the content of the first document (id 1):

    [[getdoc? &id=1 &field=`content`]]


    Snippet code:

    $id = isset($id)? $id: $modx->documentObject['id'];
    	
    $field = isset($field)? $field : "pagetitle";
    
    $result = $modx->getDocument($id,$field);
    
    return $result[$field];
    
    
      • 33337
      • 3,975 Posts
      Very nice little snippet !

      Thanks for sharing.

      regards,

      zi
        Zaigham R - MODX Professional | Skype | Email | Twitter

        Digging the interwebs for #MODX gems and bringing it to you. modx.link
        • 2762
        • 1,198 Posts
        great useful snippet! thanks!
          Free MODx Graphic resources and Templates www.tattoocms.it
          -----------------------------------------------------

          MODx IT  www.modx.it
          -----------------------------------------------------

          bubuna.com - Web & Multimedia Design
          • 25663 MODX Staff
          • 12,272 Posts
          Does it get the raw code or the parsed output?
            Ryan Thrash, MODX Co-Founder
            Follow me on Twitter at @rthrash or catch my occasional unofficial thoughts at thrash.me
            • 18397
            • 3,250 Posts
            Parsed output.

            Sorry folks I copied and pasted the wrong version!

            I updated the first post with the correct version!
              • 6726
              • 7,075 Posts
              Thanks Mark for this much needed snippet, I can think of a few uses for it smiley
                .: COO - Commerce Guys - Community Driven Innovation :.


                MODx est l'outil id
                • 25663 MODX Staff
                • 12,272 Posts
                Both versions would be useful actually (parsed and raw unparsed). smiley
                  Ryan Thrash, MODX Co-Founder
                  Follow me on Twitter at @rthrash or catch my occasional unofficial thoughts at thrash.me
                  • 17665
                  • 54 Posts
                  Slight modification to look for a template variable:
                  // Determine the target document, defaulting to self if nothing specified
                  $id = (!isset($id)) ? $modx->documentObject['id'] : "$id";
                  // Document field to get
                  $field = (!isset($field)) ? 'pagetitle' : "$field";
                  
                  $array = $modx->getDocument($id);
                  $output = $array[$field] ;
                  if (!$output) {
                      // try to get a template variable
                      $TVarray = $modx->getTemplateVar($field, "*", $id); 
                      $output = $TVarray["value"];
                      if (!$output) {
                           $output = $array['pagetitle'];
                      }
                  }
                  return $output;
                  

                    • 7737
                    • 10 Posts
                    Great snippet, although for some documents I request I’m getting no output when I request the content, although the "source retrieved from cache" gives me the following:
                    <?php die('Unauthorized access.');
                    .

                    Any help would be greatly appreciated. Thanks.
                      • 17665
                      • 54 Posts
                      Hmm... does it work fine without the modification I made? I just put in a quick hack, I didn’t do any security testing. It may be that the template variable you’re requesting has specific document permissions on it which would cause the getTemplateVar call to fail. I’d try taking a look at the "some documents" to find a pattern and see if you are requesting info from pages that have security on them.

                      Hope that helps.