We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 6726
    • 7,075 Posts
    The newslisting is a great snippet, which I am just starting to play with for a website I am buiding. This will allow for nice and feature-rich blogging with MODx laugh !

    My question is not about using NewsListing for blogging purpose, but for this travel agency website I am building. What I want to do is list items and point toward the detailed description. Nothing really hard to do, but I thought I’d use NewsListing to do that and tweak it a litlle. And it works, but I lack the information stored in my TVs (The characteristics of my items are TVs).

    I know the variables you can use in NewsListing are [+title+], [+summary]...etc. I’d like to use [*whatever_tv*] I need to add to the variables in NewsListing.

    The thing I’d like to be able to do is (basically) this :

    $tpl = isset($tpl) ? $modx->getChunk($tpl):'
        <div class="summaryPost">
            <h3>[+title+]</h3>
            <div>[*picture*]</div>
            <div>[+summary+]</div>
            <div>[*location*] - [*price*]</div>
            <p>    [+link+]</p>
            <div style="text-align:right;">by <strong>[+author+]</strong> on [+date+]</div>
        </div>
    


    It does not output anything...thus I wondered if it would be hard to add other variables in NewsListing ?

    Please remember that I am not php savvy wink

    This leads me to another question : so far I did not stumble into conditionnal tags, did I miss something and if there are none will this be added in the future ?
      .: COO - Commerce Guys - Community Driven Innovation :.


      MODx est l&#39;outil id
      • 18397
      • 3,250 Posts
      David,

      The [+tags+] are parsed manually and not automatically. Each additional variable to be parsed must be added in these lines:

      		$fields = array('[+title+]','[+summary+]','[+link+]','[+author+]','[+date+]','[+id+]');
      		$values = array($resource[$x]['pagetitle'],$summary,$link,$username,strftime($date, $resource[$x][$datetype]),$resource[$x]['id']);
      


      These two arrays are used to replace the field with the correct values at this line:
      		$output .= str_replace($fields,$values,$tpl); 
      


      Now, for TV’s, [*tvname*] in the tpl will be replaced by the tv values from the document that NewsListing is called on. Hopefully in the future, (hint hint Raymond), someone could manage to get it so that we can use the same automatic replacement functions as in the parser.

      But until then.....
        • 6726
        • 7,075 Posts
        Quote from: Mark at Nov 22, 2005, 04:34 PM

        David,

        The [+tags+] are parsed manually and not automatically. Each additional variable to be parsed must be added in these lines:

        		$fields = array('[+title+]','[+summary+]','[+link+]','[+author+]','[+date+]','[+id+]');
        		$values = array($resource[$x]['pagetitle'],$summary,$link,$username,strftime($date, $resource[$x][$datetype]),$resource[$x]['id']);
        


        These two arrays are used to replace the field with the correct values at this line:
        		$output .= str_replace($fields,$values,$tpl); 
        



        I suspected this much, but wouldn’t have toyed with it without confirmation...

        Quote from: Mark

        Now, for TV’s, [*tvname*] in the tpl will be replaced by the tv values from the document that NewsListing is called on.

        Dumb me, that’s why the TVs I added do not output anything on the listing page when I inserted them in the NewsListing snippet... I am not on a document page there. I have to find another way to list all children pages in a way that allow me to use TVs...

        Quote from: Mark

        Hopefully in the future, (hint hint Raymond), someone could manage to get it so that we can use the same automatic replacement functions as in the parser.
        But until then.....

        Now that would be cool laugh
          .: COO - Commerce Guys - Community Driven Innovation :.


          MODx est l&#39;outil id
        • Why not call a snippet to take the replacement vars being handled manually and simply loop through them all (e.g. in an array) and create placeholders for them. Then they will get replaced automatically by the parser. This other solution of manually str_replace’ing the text was before the introduction of placeholders. Here is a handy function I’ll probably add to the API in an upcoming release:

          	function generatePlaceholders($placeholders, $prefix="") {
          		global $modx;
          		if (is_array($placeholders)) {
          			foreach($placeholders as $name=> $value) {
          				$modx->setPlaceholder("$prefix$name", $value);
          			}
          			return true;
          		}
          		return false;
          	}


          Here I take an associative array called $placeholders and iterate through it, assigning the value to a placeholder with the key name, prepended with an optional $prefix that could isolate the values into a namespace.

          Alternatively, there is also an existing API function called parseChunk() that can be used to manually do the string replaces given an associative array, and a prefix and suffix for the template tags used to surround the values to be replaced. Here’s the code for it, and as you can see, it uses single braces { } to surround the replacement vars by default.

          	function parseChunk($chunkName, $chunkArr, $prefix="{", $suffix="}") {
          		if(!is_array($chunkArr)) {
          			return false;
          		}
          		$chunk = $this->getChunk($chunkName);
          		foreach($chunkArr as $key => $value) {
          			$chunk = str_replace($prefix.$key.$suffix, $value, $chunk);
          		}
          		return $chunk;
          	}
            • 6726
            • 7,075 Posts
            Thanks Jason, I am a little lost there this might help me if I manage to understand what you say.

            You apparently mean the solution would be to call from NewsListing snippet a snippet whose purpose would be to handle the vars replacement. This is the second bit of code you posted. Does it mean do something like add $modx->runSnippet(’Replacement_Snippet’); in the NewsListing snippet ? If so, where do I place this in the NewsListing code ?

            Now the first bit of code is something you mean to add in the API for the next release, am I correct ? But here comes the dumb no-php-brain question : where do I insert this additionnal code ? document.parser.class.inc.php ?

            I really need to be able to use TVs or custom variables in NewsListing, and I didn’t manage to add what I need manually (probably didn’t yet understand what Mark suggested me to do)...

            I’d appreciate the help smiley
            Thanks in advance !
              .: COO - Commerce Guys - Community Driven Innovation :.


              MODx est l&#39;outil id
            • Quote from: davidm at Nov 23, 2005, 03:37 PM

              You apparently mean the solution would be to call from NewsListing snippet a snippet whose purpose would be to handle the vars replacement. This is the second bit of code you posted. Does it mean do something like add $modx->runSnippet(’Replacement_Snippet’); in the NewsListing snippet ? If so, where do I place this in the NewsListing code ?

              Actually no, I mean there is a function accessible to your snippet using $modx->parseChunk that can take an array of values and do all the str_replacements for you, but only in chunk content. This was mentioned separately to as an alternative to using TV’s, and as a code example of the str_replace advice Mark had provided.

              Quote from: davidm at Nov 23, 2005, 03:37 PM

              Now the first bit of code is something you mean to add in the API for the next release, am I correct ? But here comes the dumb no-php-brain question : where do I insert this additionnal code ? document.parser.class.inc.php ?

              I actually just meant for you to add the generatePlaceholders function into your customized NewsListing snippet, making use of the function directly, until such time as I do add it or a similar function to the MODx API. Was just something I used to create and populate form values in a recent project, loading data from SQL queries into the typical associative array, then calling the generatePlaceholders function with the name of the table + an underscore for the prefix (e.g. user_) along with the array of column names and values. In this case, you could assign all the values to an array you create, and then do the same thing. In this way, I could use the placeholder syntax anywhere in my content (TV, chunk, content) to get the values output (e.g. [+user_first_name+] would output the value of an array element with the key ’first_name’).

              If you still aren’t clear on what I’m trying to explain, just let me know and I’ll throw some examples together when I get a spare moment today.
                • 6726
                • 7,075 Posts
                Thanks a lot for taking the time, I think I get closer to understanding what you mean. I’ll try to dig in that direction.

                In any case, you don’t have the time to teach me basics, it’s only normal. I had been meaning to learn PHP for a long time, MODx is a nice incentive to do so smiley

                If you have time for an example, no doubt it will help me, but it’s no priority you guys have a lot on your hands already smiley
                  .: COO - Commerce Guys - Community Driven Innovation :.


                  MODx est l&#39;outil id
                  • 32241
                  • 1,495 Posts
                  I would like to know more about placeholder as well. I saw this on API listing, but I haven’t have time to really see the code and understanding what the code does. If there is a simple example for me to start understanding this better, it will be great.

                  Thanks for all the willingness to help guys.


                  Sincerely,
                  Wendy Novianto
                    Wendy Novianto
                    [font=Verdana]PT DJAMOER Technology Media
                    [font=Verdana]Xituz Media
                  • Placeholders are replaced with variable data in the snippet code, and are used frequently in forms generated by snippets. They can also be created in a snippet so the corresponding placeholder can be used in a document. For example, the UserComments snippet can generate a placeholder instead of simply echoing the list of comments where the snippet is called. This gives much greater flexibility to the placement of the various parts of a form or any snippet output.

                    The replacement is done with a simple str_replace, as in the case of this bit of code from the weblogin.inc.php file, in the WebLogin snippet:

                    		
                    $tpl = str_replace("[+action+]",$modx->makeURL($modx->documentIdentifier,"",$ref),$tpl);
                    $tpl = str_replace("[+rememberme+]",(isset($cookieSet) ? 1 : 0),$tpl);	
                    $tpl = str_replace("[+username+]",$uid,$tpl);	
                    $tpl = str_replace("[+checkbox+]",(isset($cookieSet) ? "checked" : ""),$tpl);
                    $tpl = str_replace("[+logintext+]",$loginText,$tpl);	
                    echo $tpl;
                    


                    If a placeholder is part of the document, for example if the NewsListing snippet is set to generate a placeholder instead of simply echoing its results, the document parser itself handles the placeholder. The function in the parser that processes placeholders:

                    	
                    function mergePlaceholderContent($content) {
                      preg_match_all('~\[\+(.*?)\+\]~', $content, $matches);
                      $cnt = count($matches[1]);
                      for($i=0; $i<$cnt; $i++) {
                        $v = $this->placeholders[$matches[1][$i]];
                        if(!isset($v)) unset($matches[0][$i]); // here we'll leave empty placeholders for last.
                        else $replace[$i] = $v;
                      }
                      $content = str_replace($matches[0], $replace, $content);
                      return $content;
                    }
                    
                      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