I would like to set a randomly generated background image.
I am using Meteor and am calling Flickr API to provide a random image URL which I would like to set as my background image.
From what I've read, it seems that CSS is now the recommended way to set a background image. Is there a way to inject the dynamically generated url into the CSS? I can't seem to find examples showing mixing of Handlebars.js and CSS - is this possible? Or should I be avoiding CSS and setting the background image using the traditional HTML way?
I've been trying two different strategies:
1) To create the CSS attribute in the HTML with the url injected via handlebars - but I can't seem to concatenate the CSS attribute with Handlebars.
2) The other method I've tried is to create the CSS attribute as a variable in the template code, and to return that into the HTML via Handlebars, like so:
Template.backgroundImage.background = function(){
//runs function to generate url and add to Session object
FlickrRandomPhotoFromSet(setID);
//create variable with html and css attribute concatenated to url
var backgroundImage = '<div style="background-image: url('+Session.get("RandomURL")+')"></div>';
//return resultant variable to HTML via template
return backgroundImage;
};
Then in the HTML I have inserted the following:
<template name="backgroundImage">
{{background}}
</template>