Skip to content

Using the CloudFiles View Helper

Jamie Hannaford edited this page Jan 16, 2014 · 3 revisions

Bundled with this module is an easy-to-use helper feature that allows you to interact with Swift containers and objects in your HTML view files. To set this up, just specify the name of your container:

Setup

<? $helper = $this->cloudFilesHelper('my_container'); ?>

This will instantiate a facade object that provides a simpler interface for OpenCloud\ObjectStore\Resource\Container.

In order for this to work, you must specify a region in your opencloud.local.php configuration file. Possible options are ORD (Chicago), DFW (Dallas), IAD (Virginia), LON (London), HKG (Hong Kong) and SYD (Sydney). If no region is provided, a fatal exception will be raised.

Rendering objects in HTML

You can also render remote objects in full HTML, tags included:

// render an <img /> tag
<?= $helper->renderFile('my_photo.jpg');

// render an <audio> tag
<?= $helper->renderFile('band_practice.mp3');

// render a <video> tag
<?= $helper->renderFile('vacation_highlights.ogg');

// even render some flash ;-)
<?= $helper->renderFile('game.swf');

This helper method will detect the MIME-type of your remote file, find the appropriate handler and then render it into a string form for use in the DOM. You can also provide additional tag attributes:

<?= $helper->renderFile('me.png', array('class' => 'avatar', 'id' => $randomId)); ?>

With the Swift API, you have multiple URL types for each resource. This accommodates different viewing platforms (non-SSL, SSL, iOS, streaming). You can specify the URL type as the third parameter:

// load HTTPS asset
<?= $helper->renderFile('doge.gif', array(), 'SSL');

For a full list of different URL types, see this constants class. Ideally, you should pass in the third param as a class constant (so you'll need to pass in UrlType through the ViewModel), but if this is too much hassle a simple string representation will do.

Render the URI only

If your requirements are more complex, you can retrieve the URI of a resource and use it in your own bespoke HTML:

<div style="background-image:<?= $helper->renderFileUrl('header.png'); ?>"></div>
Clone this wiki locally