views:

27

answers:

2

The task is relatively straightforward:

A Drupal website displays a list of articles with thumbnails. Some visitors would like to view it without images by clicking on a button/link and have that preference saved.

e.g. http://patterntap.com/collections/index/

alt text

The problem is all visitors are anonymous and given certain traffic, page cache is enabled.

My idea was to use some simple JavaScript to set a cookie, refresh the page and depending on the cookie values (or its presence/absence) display or hide the images.

Except Drupal serves cached pages quite early and the only quick way to modify the cached version that I could find is by hacking includes/bootstrap.inc and add a custom class to the body classes then hide the images with css.

A very wrong approach, I know. But I wonder if there is a way to save different versions of a page and serve the correct version?

Edit:

  • need to keep the same uri
  • the js to show/hide the images without reload and set the cookie is already in place
  • hook_boot() is not really called for cached pages, so can't do it via custom module
  • .htaccess mods?
+1  A: 

So let me get this right. You wanna hide some images on a cached page if the user chooses to?

Why don't you write some jQuery or javascript and load that into your cached page with all the rest of the document?

Then, the client/browser would decide to run your script and hide images depending on some parameters you passed along with the request to that page or in the cookie? The script gets cached and only runs when you call it.

If you were hacking the bootstrap for something like that you'd really need to be rethinking what you were doing. Crazy! :)

Also take a look at cache_get and cache_set:

http://api.drupal.org/api/drupal/includes--cache.inc/6

Rimian
I am already using some javascript that injects the two options which do the job (hiding the images and setting the cookie).The task, however, is to serve the page under the same URL.The hack-the-boostrap-crazy-solution is live on http://thebrowser.com, but I am not comfortable with keeping it like that for long, hence the question :)
zerolab
+1  A: 

I'm not sure I 100% understand what you are trying to do but here are my thoughts. One of your root problems is that you are trying to access what is essentially different content at the same uri.

If this is truly what you want to do, then Rimian's suggestion of checking out chache_get and chache_set may be worthwhile.

Personally, it seems cleaner to me to have your "with thumbnails" and "without thumbnails" be accessed via different uri's. Depending on exactly what you are wanting to accomplish, a GET variable my be an even better way to go. With either of these two options you would hide or show your thumbnails at the theme layer. Pages with different paths or get variables would get cached separately.

If you want the visitor to be able to switch views without a page reload, then jQuery and a cookie would probably suite your needs. This wouldn't require a page reload and switching back and forth would be quite simple.

Icode4food
I may have to convince my bosses that different uri's is the only solution (even though the task is to keep it under one, clean url).The no reload part is already in place, with js and cookie.From what I understand looking at bootstrap.inc is that I can't really do much even by having a cookie set as the cached page is prepared before running `hook_boot()` or `hook_init()`, therefore if the same user returns to the uri with thumbnails even if he/she previously went with the no thumbnails option, they'd still get the cached version for the former page
zerolab