We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 20371
    • 58 Posts
    When the template specified in a getResources call is evaluated/executed/processed, it has access to placeholders relating to the page the template is being applied to. Is it possible to use placeholders from the page calling getResources?

    That's pretty much it in brief, but I'll give you some context in case anyone can help me with a work around.

    I have a page which just outputs an unformatted list of recent blog/article posts. So for each one, there's an image, heading, and summary. The page is only accessed by an xhr / ajax request, and the response is used to populate a slideshow / banner thingy.

    The site itself is responsive (media queries), so it will display the slideshow at either 300px, 600px, or 850px wide. With that in mind, the ajax request can specify the actual size of the images it needs. I include this information in the post data.

    So the idea is the page containing that list can firstly use the awesome setPlaceholders plugin something like this:
    [[setPlaceholders? &staticCache=`On` &ph=`
      slideWidth == post.slideWidth !! "850" ||
      slideHeight == post.slideHeight !! "300"
      '
    ]]
    


    Then call getResources, with a template that looks something like this:
    <div class='frame'>
      <img src='[[+tv.slideImage:phpThumbOf=`w=[[+slideWidth]]&h=[[+slideHeight]]&zc=1`]]'>
      <h1><a href='[[~[[+id]]]]'>[[+pagetitle]]</a></h1>
      <p>[[+introtext]] <a href='[[~[[+id]]]]'>( read more ... )</a></p>
    </div>
    


    So as you can see.. the [[+slideWidth]] is a placeholder on the calling resource, where [[+pagetitle]] is a placeholder on the resource the template is being applied to. So basically, this doesn't work, and slideWidth is null when parsed in the template.

    I can't see any way in the getResources docs to access these placeholders from within the template like this.

    The only workaround I can think of is to use imageSlim - a plugin that applies phpThumbOf to all images included in a chunk.

    So if I have chunkGetResources which only contains the getResources call, then I can use a chunkImageSlim, which is like a wrapper for chunkGetResources which will have access to [[+slideWidth]].

    The problem with this is that I need this extra 'wrapper' for each different chunk I want to do this trick with (ok.. there's only two). I could of course place the imageSlim calls in the template itself.. but somehow it just seems wrong to put that kind of logic in a template.

    Maybe I'm being too picky, but I'd like to find a nice neat way to do this which I or others will be able to understand when reviewing this site.

    This question has been answered by Bruno17. See the first response.

    • discuss.answer
      • 4172
      • 5,888 Posts
      setting placeholders and reading them in a getResources-tpl should work. It may be a caching issue somewhere in your situation.
        -------------------------------

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!
        • 20371
        • 58 Posts
        Quote from: Bruno17 at Jun 24, 2013, 08:47 AM
        setting placeholders and reading them in a getResources-tpl should work. It may be a caching issue somewhere in your situation.

        Oh okay. I don't think it's a caching issue. Even if I put the setPlaceholders call inside the getResources-tpl it still doesn't work. I wouldn't do that in production, but just when I was trying to narrow down this issue.

        I'll do some more testing...
          • 20371
          • 58 Posts
          Quote from: Bruno17 at Jun 24, 2013, 08:47 AM
          setting placeholders and reading them in a getResources-tpl should work. It may be a caching issue somewhere in your situation.

          It was indeed caching.. forgot the ! in [[!setPlaceholders]] - so silly. Thanks so much.
          • Quote from: Mr5o1 at Jun 24, 2013, 10:12 AM
            It was indeed caching.. forgot the ! in [[!setPlaceholders]] - so silly. Thanks so much.

            Yes, placeholders in a getResources chunk should just work like normal. Since you're getting those values from POST data though—which can potentially change every time somebody hits the page—you're right to call setPlaceholders uncached. Otherwise whatever those values are the first time the page is accessed gets cached and won't change again till the cache is cleared.
              Extras :: pThumbResizerimageSlimsetPlaceholders