We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 3914
    • 12 Posts
    I am building a custom add-on for a site. I cache the xml of a Google calendar and in another function I read the contents of the xml and iterate over the results to display them. The problem is that every row shows the content of the first item from the xml file.

    My code:
    $eventArray = $this->getEventsFromXml( $this->getProperty( 'google_xml_cache_file' ), 'entry' );
    
    		$tplOuter = file_get_contents( $this->getProperty( 'eventlist' ) );
    		$chkOuter = $this->modx->newObject( 'modChunk' );
    		$chkOuter->setCacheable( false );
    		$tplRow = file_get_contents( $this->getProperty( 'eventitem' ) );
    		$chkRow = $this->modx->newObject( 'modChunk' );
    		$chkRow->setCacheable( false );
    
    		// loop through events and write template item
    		foreach( $eventArray as $eventItem ) {
    			$rowsOutput .= $chkRow->process( $eventItem, $tplRow );
    		}
    		$aOuterTpl = array();
    		$aOuterTpl['rowsOutput'] = $rowsOutput;
    		$content = $chkOuter->process( $aOuterTpl, $tplOuter );
    		return $content;


    In the foreach loop, when debugging I can see that the $eventItem differs every iteration. Still, I get 25 times the same content in $rowsOutput. In another function where I iterate over items from the database with $modx->getCollection(); this construction works perfectly. What am I doing wrong? [ed. note: bosse1981 last edited this post 14 years, 1 month ago.]
      • 3914
      • 12 Posts
      I also tried to place

      $tplRow = file_get_contents( $this->getProperty( 'eventitem' ) );
      $chkRow = $this->modx->newObject( 'modChunk' );
      $chkRow->setCacheable( false );
      


      inside the foreach-loop, knowing that it is not good for performance, but then I get empty rows except the first.

      Can someone help me with this?
        • 39932
        • 483 Posts
        Absolutely! I took at look at your code and tested it several ways... There is nothing wrong with the code itself. The issue must be in either "getEventsFromXml()" or in your Chunks. To test this, change the following:

        From:

                foreach( $eventArray as $eventItem ) {
                    $rowsOutput .= $chkRow->process( $eventItem, $tplRow );
                }
                $aOuterTpl = array();
                $aOuterTpl['rowsOutput'] = $rowsOutput;
                $content = $chkOuter->process( $aOuterTpl, $tplOuter );
                return $content;
        


        To:


                foreach( $eventArray as $eventItem ) {
                   // $rowsOutput .= $chkRow->process( $eventItem, $tplRow );
                   $content .= '<p>' . $eventItem . '</p>';
                }
                // $aOuterTpl = array();
                // $aOuterTpl['rowsOutput'] = $rowsOutput;
                // $content = $chkOuter->process( $aOuterTpl, $tplOuter );
                return $content;
        


        Since it is not using the chunks, you can see if your data is being duplicated before you get it. If it is not being duplicated, it is the chunks. If it is, it is your getEventsFromXML().
          Website: Extended Dialog Development Blog: on Extended Dialog
          Add-ons: AJAX Revolution, RO.IDEs Editor & Framework (in works) Utilities: Plugin Compatibility List
          Tutorials: Create Cross-Context Resources, Cross-Context AJAX Login, Template-Based Actions, Remove Extensions from URLs

          Failure is just another word for saying you didn't want to try. "It can't be done" means "I don't know how".
          • 3914
          • 12 Posts
          With your testcode I get different content each row, so it has to be something with the chunk. But the chunk is nothing special:

          <li>
          	<a href="[[*alias]]/[[!+id:getIdFromGCalLink]]">
          		[[!+title]]
          	</a>
          </li>
          


          (the getIdFromGCalLink modifier only gets the real event id from Google's crappy formatted xml). It's a strange problem but it has to make sense somehow.
            • 39932
            • 483 Posts
            Confirmation: OK. So you don't get 25 times the rows?

            There doesn't look to be anything wrong with that Chunk, however, I can't view the placeholder. The way you are calling it might be off. I get my Chunks and process them in a much different manner... Have you tried:

                $rowsOutput .= $modx->getChunk($tplRow, $eventItem);
            


            Also, I find that initializing really, really helps (particularly in MODx). Near the top of the snippet, set:

                $rowsOutput = '';
            
            [ed. note: fuzzicallogic last edited this post 14 years, 1 month ago.]
              Website: Extended Dialog Development Blog: on Extended Dialog
              Add-ons: AJAX Revolution, RO.IDEs Editor & Framework (in works) Utilities: Plugin Compatibility List
              Tutorials: Create Cross-Context Resources, Cross-Context AJAX Login, Template-Based Actions, Remove Extensions from URLs

              Failure is just another word for saying you didn't want to try. "It can't be done" means "I don't know how".
              • 3914
              • 12 Posts
              Ok, I have managed to get this working. Thanks Fuzzical Logic for your help.

              I restructured some of the code and have one tiny problem left. My code is now:

              $rowsOutput = '';
              		foreach( $eventFeed as $event )
              		{
              			$calItem = $this->getDataFromCalItem( $event );
              			$rowsOutput .= $this->getChunk( $this->getProperty( 'eventitem' ), $calItem );
              		}
              		$aOuterTpl = array();
              		$aOuterTpl['rowsOutput'] = $rowsOutput;
              		$content = $this->getChunk( $this->getProperty( 'eventlist' ), $aOuterTpl );
              		return $content;


              and my template

              <li>
              	<a href="[[*alias]]/form/[[+eventid]]">
              		[[!+eventtime:date=`%d-%m-%Y`]] - [[+eventtitle]]
              	</a>
              </li>


              The problem is, the 'eventtime' is showed only in the first item. I am 100% sure that the $calItem['eventtime'] in the code is set with a timestamp.

              You can see the problem in action at http://baanadelaar.bossenbroek.glx.nl/nl/evenementen
                • 18373 ☆ A M B ☆
                • 3,141 Posts
                Don't call [ [+eventtime]] uncached.. the chunk's process() method sets the placeholders for just that specific "scope", so by the time the uncached tag gets processed the value is gone.
                  Mark Hamstra • Developer spending his days working on Premium Extras and a MODX Site Dashboard with the ability to remotely upgrade MODX and extras to make the MODX world a little better.

                  Tweet me @mark_hamstra, check my infrequent blog at markhamstra.com, my slightly more frequent ramblings at MODX.today or see code at Github.
                  • 3914
                  • 12 Posts
                  Wow, I was amazed by your fast repsonse! But you probably onyl read my first post and not the last? I changed some code...
                    • 18373 ☆ A M B ☆
                    • 3,141 Posts
                    You're still calling eventtime uncached in your last post wink
                      Mark Hamstra • Developer spending his days working on Premium Extras and a MODX Site Dashboard with the ability to remotely upgrade MODX and extras to make the MODX world a little better.

                      Tweet me @mark_hamstra, check my infrequent blog at markhamstra.com, my slightly more frequent ramblings at MODX.today or see code at Github.
                      • 3914
                      • 12 Posts
                      Pff, I read 'uncached' but thought 'cached' smiley

                      Tested, and it works fine now! Thanks for the fast response. If there was an award for quickest response, you should have it now.