We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 8109
    • 128 Posts
    Hello everyone,

    I’ve got a getResources call whose template uses two TVs. I’ve run into strange behavior when one of the TVs is called from a chunk inside the template.

    I’m wondering if there’s some problem I’ve uncovered. My non-working code looks like this:

    <div class="catalog-item [[+idx:mod:isequalto=`1`:then=`odd`:else=``]]">
    
    [[$bookStatusGR]]
    
    <a href="[[~[[+id]]]]"><h2 class="hyphenate">[[+longtitle]]</h2></a>
    
    <p>[[Gallery? &album=`[[+tv.bookGalleryID]]` &thumbTpl=`bookCatalogThumbnail` &thumbWidth=`110`  &thumbZoomCrop=`0` &itemCls=`book-thumb` &limit=`1`]]
    
    [[+introtext:shorten=`390`:default=`no introtext`]] 
    
    [[+introtext:length:if:lessthan=`390`:then=`<a href="[[+id]]"> more … </a>`:else=``]]</p>
    
    </div>
    
    [[+idx:mod:isequalto=`0`:then=`<div class="clear"></div>`:else``]]
    
    



    where the chunk bookStatusGR looks like this:

    <div class="book-status [[+tv.bookStatus]]">
    
    [[+tv.bookStatus : if=`[[+tv.bookStatus]]`:eq=`inProgress`:then=`<p class="book-status-text">Volume in production</p>`:else=``]]
    
    [[+tv.bookStatus : if=`[[+tv.bookStatus]]`:eq=`proposed`:then=`<p class="book-status-text">Proposed volume</p>`:else=``]]
    
    [[+tv.bookStatus : if=`[[+tv.bookStatus]]`:eq=`soon`:then=`<p class="book-status-text">Available Soon</p>`:else=``]]
    
    [[+tv.bookStatus : if=`[[+tv.bookStatus]]`:eq=`preOrder`:then=`<p class="book-status-text">Pre-order now</p>`:else=``]]
    
    [[+tv.bookStatus : if=`[[+tv.bookStatus]]`:eq=`available`:then=`<p class="book-status-text">Now available</p>`:else=``]]
    
    [[+tv.bookStatus : if=`[[+tv.bookStatus]]`:eq=`outOfPrint`:then=`<p class="book-status-text">Out of Print</p>`:else=``]]
    
    </div>


    If I replace the bookStatusGR chunk call in the template with the chunk code, everything works. Otherwise, when I’m calling the bookStatus TV using the chunk call, I get only the TV value of the first resource, and that is repeated for all other resources returned by getResources. Weird, huh?

    ModX 2.0.8-pl
    getResources 1.3.0
      Revo 2.0.8-pl
      • 33968
      • 863 Posts
      I think what’s happening there is your chunk is getting cached, so only the first value calculated is used by getResources.

      Try like this instead and see if it makes a difference:
      [[!$bookStatusGR]]
      

      But... couldn’t you just leave it in the getResources template anyway?

      Also, a couple of minor points:

      • try to avoid spaces around the colon for output modifiers: [[+tv.bookStatus:if=
      • maybe look at using the Switch snippet to replace all those placeholder/output modifier calls in your chunk. I think it will be a bit more efficient...
        • 8109
        • 128 Posts
        Thanks lucas,

        Yes, I could leave the logic it in the template, but it should work as a chunk, and the plan was to re-use the code. Besides, I don’t want to have to track down maddening bugs, which this appears to be.

        The exclamation point didn’t help, sadly, but I think you are right about the caching problem. Doesn’t make a lot of sense to me, but I’ll accept it for now.

        Thank you for directing me to the switch snippet. I was not aware of it, and I thought my method looked terribly inefficient. I just wasn’t sure of a better way.

        Cheers!
          Revo 2.0.8-pl
          • 8109
          • 128 Posts
          I tried the Switch snippet, and it also fails when placed inside a chunk called by the getResources template. Otherwise it works as expected! Good work!
            Revo 2.0.8-pl
            • 33968
            • 863 Posts
            It’s definitely a useful one but not many people seem to use it... don’t know why smiley

            Only other thing I can think of is to try calling the placeholder uncached too. [[!+tv.bookStatus]]
            Actually, try everything uncached!
              • 11858
              • 130 Posts
              Did anyone find a solution to this cached chunk in within a getResources template? I have the same issue and while I could put the chunk contents directly in the template, I am using this chunk in several areas and would be good to just have to update one chunk when the time comes.

              I've tried calling everything uncached but it repeats whatever the value is in the first record of the getResources call.
                • 37260
                • 8 Posts
                Hello,

                I've got the same problem...
                All the placeholders in chunks called from within the getResources template are not processed correctly.
                ie: if cached, it returns me the content of the first resource for all the resources and if not cached, it only returns content for the first resource (others are blank)...

                Here is a sample code:

                GetResources call:
                [[!getResources? &parents=`1091` &tpl=`news` &sortby=`menuindex` &limit=`20` &includeTVs=`1` &processTVs=`1` &showUnpublished=`1` &showHidden=`1` ]]
                


                Template (chunk) 'news'
                <li class="news-item news-[[+idx]]">
                <span class="id">[[+idx]]</span>
                <h2>[[+pagetitle]]
                  [[If? 
                  	&subject=`[[+tv.blogLink]]`
                  	&operator=`notempty`
                	&then=`[[!$blog-link]]`
                  ]]
                  [[If? 
                  	&subject=`[[+tv.galleryLink]]`
                  	&operator=`notempty`
                	&then=`[[!$gallery-link]]`
                  ]]
                </h2>
                [[!$image-news]]
                </li>
                


                (sub) Chunk "image-news" (the "links" chunks are as simple as this one).
                This is where the problem occurs...

                <img class="image-news" width="900" height="345" title="[[+longtitle]]" src="[[+tv.imageNews]]"></img>
                


                Any idea? [ed. note: kiguane last edited this post 12 years, 6 months ago.]
                  • 22303 MODX Staff
                  • 10,725 Posts
                  Quote from: kiguane at Mar 14, 2012, 12:08 PM
                  Hello,
                  ie: if cached, it returns me the content of the first resource for all the resources and if not cached, it only returns content for the first resource (others are blank)...

                  Any idea?
                  You need a unique tag string for each iteration as any tag that is parsed with the same property values is cached and all instances replaced on the Resource from the first execution. Try adding a dummy property to the tag, like so...

                  [[$image-news? &idx=`[[+idx]]`]]


                  And this way you can also call it cacheable, since each run is unique.
                    • 37260
                    • 8 Posts
                    Thank you it works nicely!

                    But one last question, is there a way to escape backticks?
                    For example could I use this "trick" with the IF addon?

                    [[If? 
                        &subject=`[[+tv.galleryLink]]`
                        &operator=`notempty`
                        &then=`[[!$gallery-link &idx=`[[+idx]]` ]]`
                      ]]
                    


                      • 22303 MODX Staff
                      • 10,725 Posts
                      There is no need to escape backticks; the tags are processed from the most deeply nested out...