We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 7231
    • 4,205 Posts
    I was wondering it there is a method for parsing/replacing custom placeholders (in a custom snippet). I have been using str_replace() for individual placeholders, but in this case i have a lot of placeholders so to do it ’one at a time’ is not as practical. I was hoping to have an array of available placeholders and for each record returned from a query to replace the placeholders with the corresponding variable.

    I tried replacing [+placeholder+] with ${placeholder} in the tpl, but that did not work...i just got ${placeholder} as the output rather than the variable value.

    $tpl = str_replace(array('[+', '+]'), array('${','}'), $tpl);


    Any tips to get me in the right direction rolleyes
      [font=Verdana]Shane Sponagle | [wiki] Snippet Call Anatomy | MODx Developer Blog | [nettuts] Working With a Content Management Framework: MODx

      Something is happening here, but you don't know what it is.
      Do you, Mr. Jones? - [bob dylan]
    • Why not use the $modx->placeholders array? Loop through your query results and load each one into the array with the $key being the placeholder name parameter and the value being the placeholder value parameter. Then you can take advantage of the $modx->mergePlaceholders function.
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 33372
        • 1,611 Posts
        These methods are actually a bit hard to find in the wiki and documentation right now (due to ongoing improvements). Basically you can just set the placeholders in your snippet and use them in page content, template, chunks, etc. as [+placeholdername+]. All you do to set a placeholder in a snippet or plugin is
        $modx->setPlaceholder('placeholdername',$value);


        You can also merge placeholders into a template chunk in a snippet using the method that Susan mentioned.
          "Things are not what they appear to be; nor are they otherwise." - Buddha

          "Well, gee, Buddha - that wasn't very helpful..." - ZAP

          Useful MODx links: documentation | wiki | forum guidelines | bugs & requests | info you should include with your post | commercial support options
          • 7231
          • 4,205 Posts
          Thanks for the tips...I did not consider this method, will need to check it out. So I would have a placeholder in my tpl:

          $tpl = name: [+name+]


          Then I would iterate through the query results and set the placeholder corresponding to each variable with a foreach() loop, and then use the $modx->mergePlaceholders at the end of each iteration? Is there any documentation on $modx->mergePlaceholders? I will play around with this and see how it goes....but, I am thinking that I may be missing something here (gray matter wise) embarrassed
            [font=Verdana]Shane Sponagle | [wiki] Snippet Call Anatomy | MODx Developer Blog | [nettuts] Working With a Content Management Framework: MODx

            Something is happening here, but you don't know what it is.
            Do you, Mr. Jones? - [bob dylan]
          • No, you would use $modx->setPlaceholder($key, $value) in your loop. Actually, you shouldn’t need to use the mergePlaceholders function at all; MODx will take care of that as part of the document processing.
              Studying MODX in the desert - http://sottwell.com
              Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
              Join the Slack Community - http://modx.org
              • 33372
              • 1,611 Posts
              I never do it this way (since Susan is right in saying that you can just output placeholder tags ("[+name+]") and MODx will replace them as part of the document parsing process, just like chunk names, etc.), but you can replace all placeholder tags in any template code in one fell swoop with:
              $output = $modx->mergePlaceholderContent($tpl);

              You’d set them first using the code above, of course.
                "Things are not what they appear to be; nor are they otherwise." - Buddha

                "Well, gee, Buddha - that wasn't very helpful..." - ZAP

                Useful MODx links: documentation | wiki | forum guidelines | bugs & requests | info you should include with your post | commercial support options
                • 7231
                • 4,205 Posts
                Quote from: sottwell at Jul 07, 2009, 07:04 PM

                No, you would use $modx->setPlaceholder($key, $value) in your loop. Actually, you shouldn’t need to use the mergePlaceholders function at all; MODx will take care of that as part of the document processing.
                I understand this part. But in my code i will be returning possibly 100’s of results...by setting the placeholder and then letting MODx parse them seems like overkill since I would need 100’s of placeholders...or am I missing something?

                I was hoping to parse the placeholders during each iteration so that the output would already be ’parsed’. I notice that some snippets will add a key to the placeholder like $modx->setPlaceholder($key.’-’.$iteration, $value) so that the resulting placeholder is actually [+name-123+]...this would work. But won’t this add extra, unnecessary overhead to the document parsing (and result in 100’s of items in the placeholders array)? Seems like outputting the direct output would be more economic on the resources.

                I have also seen snippets use 3rd party parsers, for example there is a snippet that uses the codeigniter template parser http://codeigniter.com/user_guide/libraries/parser.html for this.

                Am I over complicating this?

                I think this is the code Ditto uses to parse the placeholders:
                	// ---------------------------------------------------
                	// Function: replace
                	// Replcae placeholders with their values
                	// ---------------------------------------------------
                    function replace( $placeholders, $tpl ) {
                		$keys = array();
                		$values = array();
                		foreach ($placeholders as $key=>$value) {
                			$keys[] = '[+'.$key.'+]';
                			$values[] = $value;
                		}
                		return str_replace($keys,$values,$tpl);
                	}


                But I don’t understand it...or how to use it undecided

                Wayfinder also uses arrays and str_replace for this...
                //Process the row
                        $output .= str_replace($usePlaceholders,$phArray,$useChunk);
                		//Return the row
                        return $output . $this->_config['nl'];
                  [font=Verdana]Shane Sponagle | [wiki] Snippet Call Anatomy | MODx Developer Blog | [nettuts] Working With a Content Management Framework: MODx

                  Something is happening here, but you don't know what it is.
                  Do you, Mr. Jones? - [bob dylan]
                  • 33372
                  • 1,611 Posts
                  It’s not so much that you’re overcomplicating it. It’s just that there are multiple ways to do what you want to do. Personally I would either use MODx’s built-in placeholder system or plain old str_replace().

                  So for example you could do something like:

                  $output='<table>';
                  $tpl='<tr><td>[+name+]</td><td>[+email+]</td></tr>';
                  ...
                  while(your conditions){
                  ...get your values
                  $modx->setPlaceholder('name',$name);
                  $modx->setPlaceholder('email',$email);
                  ...more placeholders
                  $output.=$modx->mergePlaceholderContent($tpl);
                  }
                  $output.='</table>';
                  return $output;
                  

                  So each iteration would add a row to your output with the appropriate values for that row.

                  Realistically, this isn’t all that much simpler than a regular str_replace() (which is what Ditto uses). The real value of placeholders in MODx is that you can use them outside of a snippet once they’ve been set. There are also some quirks to MODx placeholders (e.g., initializing them to be sure that they’re empty, no uncached syntax), but in general they do the job.
                    "Things are not what they appear to be; nor are they otherwise." - Buddha

                    "Well, gee, Buddha - that wasn&#39;t very helpful..." - ZAP

                    Useful MODx links: documentation | wiki | forum guidelines | bugs & requests | info you should include with your post | commercial support options
                    • 7231
                    • 4,205 Posts
                    Thanks Zap..I think I might be getting it grin grin

                    I’ll try to apply this later on and see how it goes.
                      [font=Verdana]Shane Sponagle | [wiki] Snippet Call Anatomy | MODx Developer Blog | [nettuts] Working With a Content Management Framework: MODx

                      Something is happening here, but you don&#39;t know what it is.
                      Do you, Mr. Jones? - [bob dylan]
                      • 33372
                      • 1,611 Posts
                      You can also just copy the Ditto function as is and use it in your code. You would set each placeholder by adding it to the $placeholders array, and then at the end you’d just do a $row=replace($placeholders,$tpl); If you’d doing multiple iterations of rows of data, you’d just cycle through your rows and initialize $placeholders at the beginning of each iteration.
                        "Things are not what they appear to be; nor are they otherwise." - Buddha

                        "Well, gee, Buddha - that wasn&#39;t very helpful..." - ZAP

                        Useful MODx links: documentation | wiki | forum guidelines | bugs & requests | info you should include with your post | commercial support options