We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 21088
    • 85 Posts
    I want to take the content from a content page and insert it into another page in my template.

    How can I do that?
      • 21088
      • 85 Posts
      No suggestion? I just want to take the content from one content page and insert it,into my template file.
        • 22629
        • 194 Posts
        Your question is not overly clear. I’m taking this as, for example, your template is rendering the content for page 1, but you want to insert the content from page 2 somewhere in the HTML?

        What I would do is create a snippet that takes a resource ID as a parameter, looks up the resource from the database, and returns the content.

        You will then be able to put
        [[YourSnippetName? &docId=`2`]]
        in your template.

        Use the following code as guidance - it’s possibly not 100% correct and there’s no error or sanity checking so isn’t production-ready:

        $c = $modx->newQuery('modResource');  
        $c->where(array(
          'published' => true,  
          'deleted' => false,
          'id' => $docId
        ));
        $resources = $modx->getCollection('modResource', $c);
        
        return $resources[0]->get('content');


        Alternatively you might be able to use the getResources snippet from the modx Extras directory.
          Andy Shellam | www.networkmail.eu | @Pandy06269 @NetworkMail

          modx Revolution 2.2.6
          Windows 2012 | IIS 8 | php 5.4.11 | MySQL 5.5.29

          Content-Managed Websites Built on MODX
          • 21088
          • 85 Posts
          I got this error:

          Fatal error: Call to a member function get() on a non-object in /var/www/xxxx.dk/public_html/core/cache/elements/modsnippet/44.include.cache.php on line 15

          I create the snipped called addcontent with the php code and added this snippet to my template: [[addcontent? &docId=`24`]]

          Can you make it work laugh ?
            • 3749
            • 24,544 Posts
            Possibly your document is not published?

            At any rate, you could simplify the code to this, which would be faster:

            <?php
            $resource = $modx->getObject('modResource', $docId);
            
            if ($resource) {
                return $resource->getContent());
            } else {
                return 'Resource Not Found';
            }
              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
              • 22629
              • 194 Posts
              Quote from: BobRay at Oct 28, 2010, 03:29 PM

              At any rate, you could simplify the code to this, which would be faster:

              <?php
              $resource = $modx->getObject('modResource', $docId);
              
              if ($resource) {
                  return $resource->getContent());
              } else {
                  return 'Resource Not Found';
              }


              Nice tip, BobRay, thanks.
                Andy Shellam | www.networkmail.eu | @Pandy06269 @NetworkMail

                modx Revolution 2.2.6
                Windows 2012 | IIS 8 | php 5.4.11 | MySQL 5.5.29

                Content-Managed Websites Built on MODX
                • 4172
                • 5,888 Posts
                or just with getResourceField:
                [[getResourceField? &id=`[[*parent]]` &field=`content` ]]
                  -------------------------------

                  you can buy me a beer, if you like MIGX

                  http://webcmsolutions.de/migx.html

                  Thanks!
                  • 21088
                  • 85 Posts
                  I think Im stupid or something.

                  I have created the snippet:

                  <?
                  $resource = $modx->getObject(’modResource’, $docId);

                  if ($resource) {
                  return $resource->getContent());
                  } else {
                  return ’Resource Not Found’;
                  }


                  And in my template i have added the line: [[AddContent? &docId=`34`]]

                  And it says: /public_html/core/cache/elements/modsnippet/44.include.cache.php on line 10
                    • 21088
                    • 85 Posts
                    Okay I got it to work after I installed the getResourcefield

                    And used this line: [[getResourceField? &id=`53` &field=`content` ]]

                    Thank you all for your help!