We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 9207 ☆ A M B ☆
    • 2,475 Posts
    If I have a plugin firing on OnBeforeDocFormSave for example, then I have full access to the $resource object and all of its properties (e.g. $resource->pagetitle or $resource->getTVValue('my_tv') )...

    But how can I trigger the MODX parser to parse out my own formatting string? Say I've got something like this:

    $x = 'Hello [[*pagetitle]], this is my [[*email]]';


    And let's say that in my plugin I want to render $x for each page and store it somewhere. Doing some simple str_replace would not work cleanly because the $resource object does not populate attributes corresponding to the names of the TVs. E.g. print_r($resource) returns attributes for tv1, tv2 etc. when their human readable names might be "city", "email", etc.

    For Snippets, I could set a property in the default property set and check the box that parsed the items, but that does not appear to be working in the plugin: printing $scriptProperties will blank out all the placeholders if the preprocess is checked, or if it is not, I end up with the raw unparsed placeholders.

    Any ideas of how I can parse out an arbitrary string with values from the resource? [ed. note: Everettg_99 last edited this post 13 years, 11 months ago.]
      • 9207 ☆ A M B ☆
      • 2,475 Posts
      Answered most of my own question. See this link: https://forums.modx.com/thread/31704/modx-method-to-parse-a-string#dis-post-441749

      $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);
      


      The question becomes whether or not it's possible to use other placeholder types, e.g. for [[*pagetitle]]... the above does work for including other Chunks or Snippets... my guess is that initializing a different object (e.g. modResource) would unlock some of the other placeholders, but I'm happy if I can use the "+" placeholders.