We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 30630
    • 5 Posts
    Hi, I would like to have a page where content is constantly added (like a what’s new page) and somehow show the top few lines (say the first seven lines) of that page on the home page. I’m new to modx and I’m not sure how to do this. Any help would be greatly appreciated.
      • 18913
      • 654 Posts
      You could write a snippet that does that, e.g. :
      <?php
      $somedoc = $modx->getDocument(1);  //get the content of resource 1
      preg_match_all("/([^.?!]+[.?!])/", $somedoc['content'], $matches); //match lines per pattern found via google
      $somelines = array_slice($matches[0],0,7);  //take the first 7 matches, starting with the 0th
      $output = '';  //initialize your output
      foreach($somelines as $aline)  //for each line found ...
      {
        $output .= '---> '.$aline.'<br />';  //... output it according to some format
      }
      return $output;  //return the whole shootin' match
      ?>
      


      The pattern in the above code was found via a google search. I tested in on the "Install successful" page of MODx and it worked, though it also returned the html formatting. No doubt that pattern isn’t foolproof. But that should get you going ...
      Matt
        • 30630
        • 5 Posts
        Thank you very much, mconsidine! That’s exactly what I was looking for. It does return the html formatting but just wrapping the snippet call with div tags takes care of that.
          • 3749
          • 24,544 Posts
          You could also use the Ditto snippet to pull the content of another resource or create a TV with an @DOCUMENT binding and use PHX to truncate it.

          OTOH, what you’re doing now is more efficient if it works for you.
            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
            • 30630
            • 5 Posts
            one more question, the snippet doesn’t seem to preserve links. So a link on the original page, appears just like normal text with the snippet. Is there a way to fix that?
              • 3749
              • 24,544 Posts
              Quote from: trilepton at Apr 11, 2011, 12:39 AM

              one more question, the snippet doesn’t seem to preserve links. So a link on the original page, appears just like normal text with the snippet. Is there a way to fix that?
              Are all your links in the form of link tags?

              Evolution: [~12~]

              Revolution: [[~12]]
                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
                • 29955
                • 9 Posts
                Thanks @mconsidine. I like to minimize everyhting, so if a snippet would be faster and more efficient than say Ditto, I’d prefer to use that. Nice one!
                  • 18913
                  • 654 Posts
                  Glad this helps a bit. I wish I could figure out how to amend the pattern to only operate on what doesn’t fall between <a> tags, but I’m afraid that’s beyond me. I’ve found regex tutorials that show how to match strings, numbers, etc, not not one on how to operate on results that don’t match a given criteria. Also, when I tried out a URL matching pattern, it got confused when there was more than one in the block of sample text.

                  Maybe when I graduate from regex kindergarten I’ll learn about such stuff. But for the moment, I seem to be repeating the grade smiley

                  Matt
                    • 30630
                    • 5 Posts
                    Hello again, the above has been working great until now. But when it was time to upgrade Modx to 2.1.3, instead of the page with the snippet, I get the following error
                    Fatal error: Call to undefined method modX::getDocument() in /home/content/84/8028184/html/core/cache/includes/elements/modsnippet/3.include.cache.php on line 7

                    If I remove the snippet tag from the page, the rest of the page loads fine, but I add back the snippet tag, it gives the above error. I've tried clearing the core/cache folder but that didn't solve the problem. Any ideas?