ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Embedded Template Cache Problems

April 03, 2008 10:44am

Subscribe [1]
  • #1 / Apr 03, 2008 10:44am

    Sasha

    85 posts

    I’m having a problem when I turn on caching for an embedded template. The scenario:

    I use the Tag module to tag my gallery entries and automatically pull in gallery entries that share the same tag with whatever weblog entry is currently displayed. Pretty basic, and this is working fine.

    In several places on my site, I want to display only the most recent gallery entry with a certain tag. So I’d made a template that does just that, and can then be embedded into all the places where I need to use this:

    static/scanindex:

    {exp:tag:gallery_entries tag="{embed:cattag}"  gallery="scans" orderby="date" sort="desc" limit="1"}{entries}
    {row}{exp:imgsizer:size src="{image_url}" width="110"}{sized}{/exp:imgsizer:size}{/row}
    {/entries}{/exp:tag:gallery_entries}

    I then call it from my regular templates like this:

    {exp:weblog:entries weblog="scans" orderby="title" sort="asc"}
    {assign_variable:index_cat="{exp:tag:tags entry_id='{entry_id}' type='weblog' limit='1'}{tag}{/exp:tag:tags}"}
    
    {embed="static/scanindex" cattag="{index_cat}"}
    
    
    <a href="http://{url_title_path=scans/view}">{title}</a>{/exp:weblog:entries}

    This works fine if there’s just one entry on a page. However, if there are multiple weblog entries on the same page, and this embedded template gets called once for each entry, it displays the exact same thing for each entry - the data of the first one. Here are two screenshots for an example of it happening on my site (I’m probably not explaining it well so a visual might help):

    Caching OFF (this is how it’s meant to look)
    What it looks like once I turn caching ON

    However, turning caching off significantly increases the load on my server as I use this embedded template all over the place.

    What I’d hoped for, is that EE would automatically detect that each time the template is being called with a different embedded variable, and therefore make different caches for each (a little like how it makes different caches for the same page if there are different URL segments present).

    Anyone have any ideas how can I solve this? Is it an error or bug, am I doing something wrong or could I be setting this up in a different way? I can’t really leave caching off - but I can’t seem to figure out a way around my problem!

    Oh - and another reason for making this an embed: the code doesn’t work if I insert it into the template directly (conflicting variables, I think) - it only works from the embed. So, I can’t get rid of the embedded template completely. :/

  • #2 / Apr 03, 2008 11:23am

    Robin Sowell

    13255 posts

    Hm- the cache works based on url- so when you embed it multiple times from the same url?  It’s going to pull up the cached copy for that particular url.  (Which is why it’s ok when you embed on a different url.)  Hm—what happens if you stick an extra segment on the embed- say the {entry_id} or something.  And use dynamic off so it doesn’t jam the embed.  That might do the trick.

    But that said?  I’d also be leery of putting an embed like this in a loop.  Depending on how many you’re pulling back, it could get server intensive fast.

  • #3 / Apr 03, 2008 12:05pm

    Sasha

    85 posts

    Hmmm - the embeds are totally empty when I try to add the {entry_id} as a segment, as if it can’t find the template to embed anymore.

    I just had a brainwave, actually, and have turned caching on for the tag in the embedded template. That cut the memory usage for that template by about 50%:

    {exp:tag:gallery_entries tag="{embed:cattag}" gallery="scans" orderby="date" sort="desc" limit="1" cache="yes" refresh="720"}{entries}{row}
    {exp:imgsizer:size src="{image_url}" width="110"}{sized}{/exp:imgsizer:size}
    {/row}{/entries}{/exp:tag:gallery_entries}

    I see what you’re saying though, about embedding in a loop. I’m not entirely happy with it either and will keep looking for an alternate way of doing this. It seems simple enough, wanting to display one gallery entry for every weblog entry, but it’s turning out to be very tricky lol!

  • #4 / Apr 03, 2008 12:13pm

    Robin Sowell

    13255 posts

    Ah- now that was a slick move.  I’m pretty sure a php include would work as a sneaky workaround, but the tag caching is definitely a better idea.

    Though yea- seems like there should be a leaner way to do this.  But one is not leaping to mind.  Anything I can think of involves queries in some way/shape/form.

    I’d just keep an eye on it- the tag caching would help, making sure there aren’t too many loops will help.  But if it does start bogging down, it would be time for digging in and coming up with an alternative.

    Sound good?

  • #5 / Apr 03, 2008 12:23pm

    Sasha

    85 posts

    Yup, sounds good indeed lol! Thanks for the input!

  • #6 / Apr 03, 2008 12:42pm

    Robin Sowell

    13255 posts

    Good deal- will close it out for now.  Run into problems, we can brainstorm further.  (x-fingers and hopes 2.0 may make this easier.)

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases