We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 1169
    • 312 Posts
    I want to get the content of resource id=5 into a template

    I tried
    [[~5]]


    this produced
    index.php?id=5

    This is Revo 2.0pl

    I hope this can be done

    Thanks in advance
      DEVELOPMENT ENV:- Ubuntu 12.04 | MODx Revolution 2.2.8 | LAMP 2i Apache 2.2.22 | Php 5.3.10 | Mysql 5.5.31 MySQL client version: 5.5.31
      • 9207 ☆ A M B ☆
      • 2,475 Posts
      the [[~5]] syntax is meant to generate the URL for that page, not the page itself.

      You can have a look at the Template Variable @RESOURCE binding: http://rtfm.modx.com/display/revolution20/RESOURCE+Binding

      but perhaps the getResource Snippet is easier to use.

      What exactly are you trying to do? Might it be possible to put the content into a chunk, then reference the chunk where-ever you needed that content?
        • 1169
        • 312 Posts
        I am trying to get the content into a div in a template to be the content of resource id = 5

        I tried getResources with the following result

                <div class="col2">
        
                    <!-- Column 2 start -->
        
                    [[getResources? &tpl=`column-right-tpl` &limit=`1` &includeContent=`1` &first=`5`]]
        
                    <! Column 2 end -->
        
                 </div>
        


        with column-right-tpl in chunks
        <p>
        [[+content]] </p>
        

        which produces the following

        [2010-09-03 10:36:11] (ERROR @ /~brainwav/maydaylock/index.php) Error 42000 executing statement: Array ( [0] => 42000 [1] => 1064 [2] => You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ’1,2,3,4,5) AND (`modResource`.`context_key` IN (’web’) OR EXISTS(SELECT 1 FROM `’ at line 1 )

        Thanks for your reply @Everett
        Anyone any ideas huh
          DEVELOPMENT ENV:- Ubuntu 12.04 | MODx Revolution 2.2.8 | LAMP 2i Apache 2.2.22 | Php 5.3.10 | Mysql 5.5.31 MySQL client version: 5.5.31
          • 1778
          • 659 Posts
          hello

          You can create a snippet (call it "getContent" for example) put this code in it :
          $id= 5; // ID of the doc you want to display the content field
          $resource = $modx->getObject('modResource',$id); // get the resource as an object
          $output = $resource->get('content'); // get the content 
          
          return $output; // display output 
          
          


          Then in your template you can do
          <div class="col2">
                      <!-- Column 2 start -->
          
                      [[!getContent]]
          
                      <! Column 2 end -->
          
                   </div>
          


          It should fit your goal
          Cheers
            • 1169
            • 312 Posts
            Thanks for your input @anso

            It worked a treat.

            I did try something similar but with no luck. sad
              DEVELOPMENT ENV:- Ubuntu 12.04 | MODx Revolution 2.2.8 | LAMP 2i Apache 2.2.22 | Php 5.3.10 | Mysql 5.5.31 MySQL client version: 5.5.31
              • 1778
              • 659 Posts
              Glad to help smiley
                • 9207 ☆ A M B ☆
                • 2,475 Posts
                Or perhaps, to further anso’s example... make the Snippet more reusable by giving it an input option:

                // INPUT: $id (int) Page ID of page you want to get the content of. 
                // USAGE: [[!getContent? &id=`5`]]
                $resource = $modx->getObject('modResource',$id); // get the resource as an object
                $output = $resource->get('content'); // get the content 
                
                return $output; // display output 


                Then in your other page
                [[!getContent? &id=`5`]]
                  • 1169
                  • 312 Posts
                  Thanks to both of you @anso & @Everett. I had just tried @Everett’s code before I read it to see if I could make it more generic.
                  Even thought of adding error checking. huh
                  But maybe not necessary for most situations.

                  I was also thinking of calling it getModResourceContent or get_modResourceContent Snippet not sure which convention would be more appropiate.

                  I was just wondering where I went wrong with my getResorces code above. huh

                  Is there a need for a home for these small maybee 1 to 5 line snippets for these general kind of things.
                  Probably the sort of things you guys write in you sleep.
                  I am sure these have been presented many times before.
                  I tried Forum Search and Google to no avail only Template Variable answers.

                  I read a lot about guys being designers not PHP programers.
                  Surley this sort of thing would help them.

                  With guys around like you this surley is "The Greatest Show on Earth"
                  (I am Including Everyone who gives so freely of their most valuable assets Time & Experience)
                    DEVELOPMENT ENV:- Ubuntu 12.04 | MODx Revolution 2.2.8 | LAMP 2i Apache 2.2.22 | Php 5.3.10 | Mysql 5.5.31 MySQL client version: 5.5.31
                    • 9207 ☆ A M B ☆
                    • 2,475 Posts
                    I’ve been thinking a lot about how to help users catch mistakes in code and in templates... I’ve got a few ideas for UI plugins that should improve the "MODx Experience".

                    For now, I usually save a local .PHP file as I develop, then I can do syntax checking (e.g. on the *NIX command line, type "php -l name_of_file.php" and it’ll check for valid syntax). This is hugely helpful for catching missing semi-colons, brackets, etc. but it’s always a good idea to use logging in your scripts so you can see what’s going on.