Elegant Asset Handling - my first Symfony Plugin “teUrlToAssetPlugin”

August 25, 2009 – 10:17 am

Most websites I work on these days have some sort of header image on almost every page - an optional “banner” if you will, that adds to the general feel of the current section of the site. Since most of these sites as also managed through a CMS setup (symfony, sfDynamicCMS + custom stuff) it is a requirement to be able to adjust these images on a per-page level, even though most pages share the same image within their section..
In short, we often have a need for flexible customization without putting the user through the nightmare of picking a picture through the admin interface every time they want to create a page.

The solution we came up with is rather simple, yet so elegant that we found it to work in all our sites - we use a storage convention based on the page URL.

For example, let’s take a page with the following url:

http://www.somesite.com/sports/outdoors/hiking

where “sports” and “outdoors” are categories, and “hiking” is a specific activity.

The required flexibility now is that the we wanted to be able to have a base-banner for the “sports” section, be able to (optionally) override this for each sub-category, and for each specific activity. As you can see, mapping the url to a directory in the webroot and working backwards would yield just that nice cascade.

Say for example we needed a banner on said page, we would want to look for the following images (in this order):

"/uploads/banner/sports/outdoors/hiking.jpg"
"/uploads/banner/sports/outdoors.jpg"
"/uploads/banner/sports.jpg"
"/uploads/banner.jpg"

Whichever picture is found first, wins.

We add the “banner” directory to be able to pull the same trick for other assets.. maybe a product manual, or a particular flash file for the footer? Never limit your options.. ;)

Now, the code that does this cascading lookup based on url is fairly trivial, but I wanted the whole thing to be very intuitive and simple for our front-end guys, so that they can easily use this functionality from within templates. Turns out this is a perfect case for the ArrayAccess interface, which allows you to access objects via array syntax.

The resulting template code looks as simple as:

$image = $assets["banner"];

and you we even let you specify the file type via an optional extension (defaults to JPG):

$image = $assets["banner.png"];
$document = $assets["manual.pdf"];
$audio = $assets["music.swf"];

As you can see, this setup makes it very easy to distribute general and specific assets throughout your site and administering the files via a wysiwyg or standalone asset manager - without cluttering up the admin area.

Hope you’ll find this as time-saving and elegant as I find it.

Have a great day,
Daniel


Filed under: Symfony — Tags: , — by Richtermeister

7 Comments »

  1. Pillspot.org. Canadian Health&Care.Special Internet Prices(up to 40% off average US price).No prescription online pharmacy.Pillspot.org. Vitamins@buy.online” rel=”nofollow”>.…

    Categories: Stop SmokingSkin Care.Anti-allergic/Asthma.Antidepressants.Womens Health.Pain Relief.Antidiabetic.Weight Loss.Eye Care.Anxiety/Sleep Aid.Mens Health.Cholesterol.Stomach.Antibiotics.Mental Health/Epilepsy.Antiviral.Blood Pressure/Heart….

    Trackback by ENRIQUE — June 25, 2010 @ 4:00 am


  2. Medicamentspot.com. Canadian Health&Care.Special Internet Prices.No prescription online pharmacy.Best quality drugs. Low price drugs. Buy pills online

    Buy:Female Pink Viagra.Zocor.Ventolin.Buspar.Nymphomax.Benicar.Prozac.Lasix.Lipitor.Lipothin.Acomplia.Amoxicillin.Female Cialis.Cozaar.SleepWell.Seroquel.Aricept.Wellbutrin SR.Zetia.Advair….

    Trackback by ERIK — July 21, 2010 @ 4:58 am

  3. ives http://sstgzq13j.bestpartsstore.info/tag/r2+stress+ives/ : ives…

    stress…

    Trackback by stress — August 29, 2010 @ 3:10 am

  4. boot http://xspotcwp.bestpartsstore.info/tag/oakley+Ski+boot/ : oakley…

    Ski…

    Trackback by boot — August 29, 2010 @ 2:19 pm

  5. halotone http://yphilipsom0.ACEHARDWAREE.INFO/tag/halotone+Light+philips/ : Light…

    philips…

    Trackback by philips — August 29, 2010 @ 6:03 pm


  6. CheapTabletsOnline.Com. Canadian Health&Care.No prescription online pharmacy.Best quality drugs.Special Internet Prices. High quality drugs. Order pills online

    Buy:Prednisolone.Petcam (Metacam) Oral Suspension.Zovirax.Human Growth Hormone.Actos.Lumigan.Retin-A.Synthroid.100% Pure Okinawan Coral Calcium.Nexium.Mega Hoodia.Zyban.Accutane.Arimidex.Prevacid.Valtrex….

    Trackback by ALLEN — September 5, 2010 @ 9:40 am


  7. CheapTabletsOnline.com. Canadian Health&Care.No prescription online pharmacy.Best quality drugs.Special Internet Prices. Low price drugs. Buy drugs online

    Buy:Super Active ED Pack.Tramadol.Cialis Professional.Viagra Super Active+.Cialis.Viagra.Soma.Viagra Professional.Zithromax.VPXL.Cialis Super Active+.Maxaman.Propecia.Levitra.Viagra Soft Tabs.Cialis Soft Tabs.Viagra Super Force….

    Trackback by MATT — September 6, 2010 @ 6:00 am

RSS feed for comments on this post. TrackBack URL

Leave a comment

Powered by WordPress