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. If a MIME-type is detected that can not be rendered as an img, audio, video or object tag, it will default to a standard hyperlink.

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>

Render the full contents of a container

If you need to quickly output the full contents of a Swift container, this is easy to do. The method signature is:

string renderAllFiles ( [ int $limit = 100 [, string $urlType = 'CDN' [, string $prefix = false [, string $suffix = false ]]]] )
  • $limit provides a limit. If a non-integer is provided (like false), there will be no limit set and all resources will be returned.

  • $urlType is the same as above.

  • $prefix is a string representation of a HTML tag prefix. This tag will be prepended to every resource.

  • $suffix is a string representation of a HTML tag suffix. This tag will be appended to every resource.

For example, execution this:

<?= $helper->renderAllFiles(3, 'SSL', '<div>', '</div>'); ?>

will return:

<div>
   <img src="https://container.foo.com/my_container/asset_1.png" />
</div>
<div>
   <img src="https://container.foo.com/my_container/asset_2.png" />
</div>
<div>
   <img src="https://container.foo.com/my_container/asset_3.png" />
</div>