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

    This is my first foray into MODx smiley
    I am following the example DB code using newQuery in the docs: http://rtfm.modx.com/display/revolution20/Snippets

    When I call the chunk while looping through the resource array, is it possible to use the template variables of that resource inside the chunk?
    Right now the relevant part of my snippet looks like:
    $resources = $modx->getCollection('modResource', $specs);
    foreach ($resources as $resource) {
    $output .= $modx->getChunk('MyChunk',$properties);
    }
    


    And inside the chunk I have:
    <div class="prop">[[*my_template_var]]</div>
    


    Instead of using the resource’s template variables specified in the loop, somehow the current page’s template variables are being used instead.
    How can I use the queried resources’ variables instead?

    Thanks for any help
      • 22427
      • 793 Posts
      <div class="prop">[[*my_template_var]]</div>
      Try [tt][[+my_template_var]][/tt] instead of [tt][[*my_template_var]][/tt].
        • 3749
        • 24,544 Posts
        As Ottogal suggests, you definitely want placeholders rather than content tags -- [[+TvName]], not [[*TvName]]. You’ll also need to set the $properties argument for each resource:

        Try this:

        $resources = $modx->getCollection('modResource', $specs);
        foreach ($resources as $resource) {
           $properties = $resource->toArray();
           $output .= $modx->getChunk('MyChunk',$properties);
        }


        Unfortunately, that won’t get you the TV values. There’s a good chance that you can avoid the whole problem by using getResources, which I think does exactly what you want.

        If not, you’ll have to get each TV value separately and add it to the $properties array with something like this:


        $resources = $modx->getCollection('modResource', $specs);
        foreach ($resources as $resource) {
           $properties = $resource->toArray();
        
           $tvVal = $resource->getTVValue('MyTv');
           $properties[] = 'MyTv'=>$tvVal;
        
           $output .= $modx->getChunk('MyChunk',$properties);
        }


        The middle two lines could also be on a single line:

           $properties[] = 'MyTv'=>$resource->getTVValue('MyTv');




        Again, though, getResources is designed for exactly this task.
          Did I help you? Buy me a beer
          Get my Book: MODX:The Official Guide
          MODX info for everyone: http://bobsguides.com/modx.html
          My MODX Extras
          Bob's Guides is now hosted at A2 MODX Hosting
          • 34393
          • 4 Posts
          Ah, thank you, getResources does do what I want laugh