FlashHelper - a wrapper for the SwfObject js class

By Alexander Morland (alkemann)
After having had small issues every time I try to embed flash in my apps, and always resorting to using the javascript SwfObject, I decided it was time to just make a wrapper helper for this helpful vendor. So, if you need a fast, easy and reliable way of adding flash to your projects, look now further, the FlashHelper is here to help.

What it does

It gives you a oneliner to embed flash into your views as easy as the HtmlHelper::image lets you embed pictures. It does this using a javascript class called SwfObject [1]


Requirements

  • This helper in ur /app/views/helpers folder (it's found on next page)
  • The helper added to your controller's helpers property
  • The javascript file in ur /app/webroot/js folder (found [2])

How to use it

The helper has only two public functions and one of them is optional.


init($options)

The optional init function has the job of linking the document to the required javascript,
but as long as the helper is able to use the layouts $scripts_for_layout, calling this method
multiple times is not a problem.

The other feature of this method is taking in options that subsequent calls of renderSwf will use, ie for reusing a set of options for multiple flash embeds, set them here. Note, that if you
do not call this method, the renderSwf will.


renderSwf($swfFile,$width,$height,$divDomId,$options)

This is used once per flash file you wish to embed. Think of it as the flash equalent of $html->image. The parameters are pretty straight forward, but let me explain the few interesting aspects.

The first parameter is a string of the swf files name, relative to the webroot.

The width and height may be set through the options array of the init function, if you wish.

The divDomId parameter defaults to false, which will make the helper not look for an existing div, but render one at the place the helper is called. If you wish the helper to use a div of your choosing, supply the dom id.

Note : The div is replaced by the flash, make another wrapper div for styling

If you wish to have alternate content that is displayed if the flash may not be embedded or javascript is disabled, put that content in the div with the supplied dom id.


The options array is a nested array with 3 first level keys that it looks for;

  • flashvars
  • params
  • attributes

The documentation for what valid keys and values these three accepts may be found at [3]


Examples

Download code <div id="flashy"><p>No flash loaded</p></div>
<?php echo $flash->renderSwf('test.swf',400,200,'flashy');?>
Download code <?php 
$flash
->init(array('width'=>200,'height'=>100));
echo 
$flash->renderSwf('test1.swf');
echo 
$flash->renderSwf('test12swf');
?>
Download code <?php 
echo $flash->renderSwf('fl_countdown_v3_3.swf?mo=1&da=24&snd=off&co=AA3333',800,250,false,array('params' => array('movie'=>'?mo=1&da=24&snd=off&co=AA3333')));
?>

Feedback

Please leave a comment or report any issue / improvements to me [4] and I will try to keep the helper updated


Links

  1. http://code.google.com/p/swfobject/
  2. http://code.google.com/p/swfobject/downloads/detail?name=swfobject_2_1.zip
  3. http://code.google.com/p/swfobject/wiki/documentation
  4. http://code.google.com/p/alkemann/issues/entry

Page 2: The Code

Comments 857

CakePHP Team Comments Author Comments
 

Comment

1 Awesome

Awesome job. And well written article. If I need to use flash in a project ever I'll come back here.

Good job
Posted Nov 16, 2008 by Jonah Turnquist
 

Comment

2 Great !

I was looking forward for such a helper.

A few days ago I read this Adobe article which is also about SWFObject: http://www.adobe.com/devnet/flashplayer/articles/alternative_content.html
SWFObject seems to be the best way to include flash in webpages, and is supposed to become a standard, according to another article from the same author, Bobby van der Sluis. See http://www.adobe.com/devnet/flashplayer/articles/swfobject.html
So thanks for Flash Helper!

P.S. : I was wondering if there is any place where we can follow future developments of Flash Helper (if any) and more precisely, know about its future releases?
I saw your page at http://code.google.com/p/alkemann/issues/entry but I was not able to browse the code. Am I wrong? Or maybe Flash Helper is in the Cakeforge (http://cakeforge.org/)]?
Posted Dec 2, 2008 by Maxim
 

Comment

3 Re: Maxim

To follow the changes in the "alkemann" project, see here : http://code.google.com/p/alkemann/updates/list
I have also set up a SVN now which you can get at http://code.google.com/p/alkemann/source/checkout
Posted Dec 3, 2008 by Alexander Morland
 

Comment

4 I belive your 3rd example is not accurate

Correct me if I'm wrong, but shouldn't your 3rd example look more like this?

View Template:


<?php
echo $flash->renderSwf('fl_countdown_v3_3.swf?mo=1&da=24&snd=off&co=AA3333',800,250,false, array('flashvars' => array('mo' => 1'da' => 24'snd' => off'co' => 'AA3333')));
?>

as opposed to this...

View Template:


<?php 
echo $flash->renderSwf('fl_countdown_v3_3.swf?mo=1&da=24&snd=off&co=AA3333',800,250,false,array('params' => array('movie'=>'?mo=1&da=24&snd=off&co=AA3333')));
?>
Posted Jan 3, 2009 by Ben Pesso
 

Question

5 flash helper in js

Hi!!
How can I use flash helper to create an object which can be assigned to a javascript variable in my view?? I want to do dhtml.

Something like:
//inside my view on top of all html

var obj= 'renderSwf('veppa_photo_album2.swf?xml='+path+'gallery_x1.xml',600,400,false,array('params'=>array('movie'=>'veppa_photo_album2.swf?xml='+path+'gallery_x1.xml'))));?>';

& I can use this obj for dhtml. I hope i am clear. Thanks!!
Posted May 25, 2009 by Zuber