We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 24071
    • 14 Posts
    Hi all,

    In the chunks in modx I frequently see this:
    <form>
    ....
    <input value="[+variablename+]">
    ....
    </form>

    I can’t exactly figure out how this [+variablename+] can be used to dynamically display data.

    Right now I use str_replace in my snippet to replace the +variablename+ with something I want. Is this the correct use or can I use this more clever?
      Purple
      === Don’t forget to smile ===
      • 32241
      • 1,495 Posts
      Well you sure can do those thing with str_replace(), but there is an API provided by MODx to do those kind of thing, which sometimes it works really well for me.

      Check setPlaceHolder & getPlaceHolder API

      You also want to check mergePlaceholderContent & MergeChunkContent API for integrating it with the chunk template that you’ve created.

      Here is a little overview for each of them

      function setPlaceholder($name,$value)

      This API will basically store the $value variable into placeholders array with the associated key name from the given $name. The placeholder array, later can be applied to substitute a string template that has a special MODx placholder with the format of "[+key_name+]", without the quote.

      function getPlaceholder($name)

      This is the opposite API of sePlaceHolder, which it will return the value of placeholder array with the given $name as the key name.

      function mergePlaceholderContent($content)

      This function will combined the placeholders array that was set earlier (setPlaceholder) with the given string template, which was passed on to $content. It will replace "[+key_name+]" symbol in the template with the value on the array with the same key name. The rest of the placeholders symbol that are not associated with everything will be remove and substituted with empty string.

      function mergeChunkContent($content)

      This API will fetch a chunk based on their name, when it was stored inside the manager. We need to pass on a string with the format as when we are included chunk to the template or content "{{chunk_name}}", without the quote.

      I think that’s all that you need to sstart rolling right now. Have MODx-ing with your new set of toy.
        Wendy Novianto
        [font=Verdana]PT DJAMOER Technology Media
        [font=Verdana]Xituz Media
        • 24071
        • 14 Posts
        Wendy, thank you for your thorough explanation.
        Didn’t know this was called a place holder. I must have overlooked it in the documentation.

        I’m going to give it a try.
          Purple
          === Don’t forget to smile ===
          • 22303 MODX Staff
          • 10,725 Posts
          Wendy, please be aware that the mergePlaceholderContent() and mergeChunkContent() functions you are calling here are not public API methods, and get called by the parser regardless (you don’t need to call these in code except in very specialized cases). These are internal methods for the parser only, and calls to these in snippets/plugins/modules may not be supported in future releases, though similar public API methods may be exposed at some point to replace such calls.
            • 32241
            • 1,495 Posts
            Quote from: OpenGeek at Jan 13, 2006, 09:50 AM

            Wendy, please be aware that the mergePlaceholderContent() and mergeChunkContent() functions you are calling here are not public API methods, and get called by the parser regardless (you don’t need to call these in code except in very specialized cases). These are internal methods for the parser only, and calls to these in snippets/plugins/modules may not be supported in future releases, though similar public API methods may be exposed at some point to replace such calls.

            Ehm, I just realize it, when I check back to the document parser. Is there any better way to do it? Honeslty, when code something for MODx, I always look for the fastest way to dit.

            If you can provide us with a better solution, than it will be great. If not, is it possible to have this current function be setup as a public supported API?

            Thanks
              Wendy Novianto
              [font=Verdana]PT DJAMOER Technology Media
              [font=Verdana]Xituz Media
              • 22303 MODX Staff
              • 10,725 Posts
              What is the purpose? These functions are called on the content by the parser anyway. There is no need to call them directly, unless you are doing something very complex where you need the content parsed in a very specific sequence. Even this will not be necessary once the recursive parser is in place, as this will allow placeholders and snippets to be nested within other snippet calls. Make sense Wendy?
                • 32241
                • 1,495 Posts
                Quote from: OpenGeek at Jan 13, 2006, 03:12 PM

                What is the purpose? These functions are called on the content by the parser anyway. There is no need to call them directly, unless you are doing something very complex where you need the content parsed in a very specific sequence. Even this will not be necessary once the recursive parser is in place, as this will allow placeholders and snippets to be nested within other snippet calls. Make sense Wendy?

                Ehmm....
                Let me think first, trying not to make a wrong assumption again.

                Ok, I think I got it.

                Now the quesiton is, how about if we need to use something for template looping. I mean, one chunk template will be use over and over again as a template for listing. The best example to look at is newslisting or childocumentmapper snippet. Can this be achieve with recursive parser? I have no idea on how the future parser will work, so if you think this will work in the future release, without having to use the mergechunk and mergeplaceholder API, then it’s awesome.

                Thanks Jason,
                  Wendy Novianto
                  [font=Verdana]PT DJAMOER Technology Media
                  [font=Verdana]Xituz Media
                  • 22303 MODX Staff
                  • 10,725 Posts
                  Quote from: wendy at Jan 13, 2006, 03:58 PM

                  Now the quesiton is, how about if we need to use something for template looping. I mean, one chunk template will be use over and over again as a template for listing. The best example to look at is newslisting or childocumentmapper snippet. Can this be achieve with recursive parser? I have no idea on how the future parser will work, so if you think this will work in the future release, without having to use the mergechunk and mergeplaceholder API, then it’s awesome.

                  The proper way to do substitution on a chunk used as a template within a snippet, which is what I believe you are referring to, is to simply create an array of your replacements and use str_replace(). There is also a convenience API function to assist you with the task:
                  $modx->parseChunk($chunkName, $chunkArr, $prefix="{", $suffix="}")


                  There’s no reason to use placeholders in this instance, as you are only doing this in the scope of the current snippet, but you certainly could use the placeholder array as the second parameter in parseChunk() to do the replacements. Notice that you can use just about any syntax you want to define the chunk variables you will be replacing, and by default it is not the placeholder syntax [+variableKey+], but instead {variableKey}.

                  Here is an example from some code I used on a project last year to produce individual forms for each user from a client company; these client users forms are generated dynamically using the technique I’m describing above:

                  	$userForm = "";
                  	$formInstance = 1;
                  	$clientUsers= $objExtranet->getClientAuthorizedUsers($clientID);
                  	if (is_array($clientUsers)) {
                  		foreach($clientUsers as $clientUser) {
                  			$clientUser['formName']= "adminClientUser" . $clientUser['id'];
                  			$clientUser['formInstance']= "$formInstance";
                  			$clientUser['formPrefix']= "clientuser" . $clientUser['id'] . "-";
                  			$clientUser['clientUserAccessLink']= !$clientUser['webuser']? '<a title="Provide extranet access and notify client" href="[~95~]?id='.$clientUser['id'].'&opt=0">Provide extranet access and notify client</a>': '<a title="Resend extranet access notification to client" href="[~95~]?id='.$clientUser['id'].'&opt=1">Resend extranet access notification to client</a>';
                  			$userForm.= $modx->parseChunk("AdminClientUserForm", $clientUser, "[+", "+]");
                  			$formInstance++;
                  		}
                  	}
                  


                  Notice that I’m sending each result set row from a collection of rows returned by a db query in the call $clientUsers= $objExtranet->getClientAuthorizedUsers($clientID), adding some additional data to the result set array specific to form processing, and then doing the replacements for each instance.

                  I hope that helps clarify the difference between setting placeholders that have a global scope across the document and are automatically merged with the document content when being parsed, and doing template style replacements on individual chunks for specialized applications.
                    • 24071
                    • 14 Posts
                    Well, it got a littlebit more complicated then I did expect.

                    After reading this it seems to me it is common use to use str_replace to subtitute placeholders. In more complex situations you probably will use the mentioned functions.

                    Tnx,
                    purple
                      Purple
                      === Don’t forget to smile ===
                      • 22303 MODX Staff
                      • 10,725 Posts
                      The important point here, is that unless you need to manually replace something that is in danger of having overwritten data in a placeholder, you don’t ever need to worry about it. Just set the placeholder, place the tag (e.g. [+placeholdername+]) in the chunks being included on the page, and you are done. The parser will handle replacing any placeholder tags in the content with the appropriate value.

                      Consider in the example below that I could not generate the same form for 10 clients using placeholders, because each instance would overwrite the placeholder values for the previous instance. So, instead I chose this manual template-style replacement using the $modx->parseChunk method on each instance, instead of using placeholders at all.