We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 27733
    • 99 Posts
    I’d like to parse some placeholders in a supplied string (as opposed to a chunk). Is there a simple approach to this, similar to that used when parsing a chunk { $modx->getObject(’modChunk’,array([opts]))->process(array([opts])) } ? Thanks smiley
      ------
      Server: *nix, ModX Evo 1.0.4, Apache 2.x, PHP 5.x, Mysql 5.x.
      • 3749
      • 24,544 Posts
      I’m not sure exactly what you’re trying to do, but this may help.

      First, depending on what’s in the array, this would be simpler for chunks:

      $modx->getChunk('ChunkName',$options);


      For the string, it depends on what you’re processing and why. If you want processed output in the front end, you can just leave the string alone and the tags will be processes when it’s rendered.

      You could also use a snippet and parse it yourself with getchunk(), str_replace, and/or runSnippet().

      If it’s too complicated to handle yourself, you could create a chunk from the content and use your original code, something like this:

      $chunk = $modx->newObject('modChunk');
      $chunk->setCacheable(false);
      $chunk->setContent($string);
      $chunk->process($options);
        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
        • 27733
        • 99 Posts
        Thanks for your reply, Bob. Generally, there are many times that I don’t like to use chunks because of the difficulty in editing them (even with codemirror) ... particularly when they are medium to long bits of code. Thus, I tend toward housing these bits in files and including them in my app/snippet code. In my more immediate case, I have a bit of javascript (included from a file) where I’d like to parse a few placeholders before using the regClientHTML method to insert it into the document. I can write a function to do this, but wanted to know if there was an already-existing method that I could use. I see how the method you offered below works (creating a new chunk...) ... should I assume that it’s a more resource-intensive solution than writing a small function to parse the placeholders?
          ------
          Server: *nix, ModX Evo 1.0.4, Apache 2.x, PHP 5.x, Mysql 5.x.
          • 3749
          • 24,544 Posts
          Quote from: smg6511 at Jul 27, 2011, 09:58 AM

          I can write a function to do this, but wanted to know if there was an already-existing method that I could use. I see how the method you offered below works (creating a new chunk...) ... should I assume that it’s a more resource-intensive solution than writing a small function to parse the placeholders?

          Rolling your own would almost certainly be more efficient, but the difference might not be noticeable, depending on how often it happens.

          There may be a way to use the parser for this, but I don’t know offhand what the method would be.
            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
            • 22303 MODX Staff
            • 10,725 Posts
            Quote from: BobRay at Jul 28, 2011, 05:22 AM

            Rolling your own would almost certainly be more efficient, but the difference might not be noticeable, depending on how often it happens.

            There may be a way to use the parser for this, but I don’t know offhand what the method would be.
            I think it would be less efficient to roll your own. modChunk is always loaded as a class, and once a Resource is cached, the Chunk source are also cached along with it (in 2.1+), making it very fast to parse. I would use a blank modChunk, and call process($properties, $content) if you want to process your content from external files. There is no real overhead associated with using an instance of modChunk. There is with loading your own code to parse things in a non-standard way. You can also look at the modChunk->process() method if you want to see how things are processed.
              • 3749
              • 24,544 Posts
              Well, I did say "almost." tongue

              OpenGeek knows way more about it than I do, so I stand corrected.
                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
                • 27733
                • 99 Posts
                Thank you both for weighing in! I did go ahead and implement Jason’s suggestion and it works splendidly.
                Cheers, Jim.
                  ------
                  Server: *nix, ModX Evo 1.0.4, Apache 2.x, PHP 5.x, Mysql 5.x.
                  • 9207 ☆ A M B ☆
                  • 2,475 Posts
                  It should be clarified that the placeholders corresponding to the $props must use the "+" notation. Here's a bit of code inspired by the getResources snippet:


                  $tpl = 'My formatting string contains placeholders for [[+cat]], [[+dog]], and [[+chimp]]';
                  $props = array('cat'=>'Meow', 'dog'=>'Woof', 'chimp'=>'AAAAAA');
                  
                  $uniqid = uniqid();
                  $chunk = $modx->newObject('modChunk', array('name' => "{tmp}-{$uniqid}"));
                  $chunk->setCacheable(false);
                  $output = $chunk->process($props, $tpl);
                  
                    • 38290
                    • 712 Posts
                    This is a cool example of turning MODX around on it self to do something useful. Thanks for sharing.

                    Quote from: Everettg_99 at Oct 29, 2012, 04:52 PM
                    It should be clarified that the placeholders corresponding to the $props must use the "+" notation. Here's a bit of code inspired by the getResources snippet:


                    $tpl = 'My formatting string contains placeholders for [[+cat]], [[+dog]], and [[+chimp]]';
                    $props = array('cat'=>'Meow', 'dog'=>'Woof', 'chimp'=>'AAAAAA');
                    
                    $uniqid = uniqid();
                    $chunk = $modx->newObject('modChunk', array('name' => "{tmp}-{$uniqid}"));
                    $chunk->setCacheable(false);
                    $output = $chunk->process($props, $tpl);
                    
                      jpdevries
                      • 40762
                      • 54 Posts
                      Is there a work around so I can run snippet not only placeholders with this?

                      Thanks
                        Marc-Andre Audet