How to Refer to an Image Resource from CSS in Grails

How do I refer to an image resource from CSS in grails?

Try adding "../" at the beginning of the URI. For example:

../images/outbound-blue.png

The "../" at the start of the URI tells the browser to go up one level to the parent directory then look in the images directory. Currently you have it set up to look for a subdirectory called images in the directory containing stylesheets.

Grails 2.0: Use variable for image resource

You are trying to embeed Expresion Language inside Expresion Language.

Replace:

 <img src="${resource(dir:'images',file:"${logo}")}" alt="Logo" border="0" /> 

By

 <img src="${resource(dir:'images',file:logo)}" alt="Logo" border="0" />

Inside EL you can refer to variables directly

Grails: Serving Static Content from outside the application

After much research and questioning, I decided to package the static resources (such as images) in the database. The advantages of this are:

  1. You don't need to package your images within your application (where a redeploy could cause you to lose them all)
  2. You don't need to store your images in another location on the server, away from your application.
  3. You don't need to work with a 3rd party service such as Amazon S3.
  4. Writing a simple controller to serve files from the database is relatively simple.

The disadvantages are:

  1. You cannot access/manipulate the files on the filesystem.
  2. The database grows and can become very slow.
  3. You need to make a database call every time someone requests an image.

I decided to go with the solution, even though there are these disadvantages. This is how I dealt with them:

  1. This isn't a problem as it's a 100% online system.
  2. My images are small and there aren't many of them, so this isn't a problem.
  3. Made use of a efficient and easy caching mechanisms. See below:

I'm using Smart Browser Caching by making use of etags (documentation) and using EHCache (documentation) to cache the images server side. As soon as a new image is uploaded, the e-tag is changed, and the cache is cleared, forcing the browser to download a fresh copy.

So far, the loss of performance from the MySQL database has been un-noticable, and performance is lightning fast.

using a resource inside a taglib definition in grails?

Taglibs are available from within other taglibs (and controllers) via a variable matching their namespace (the value of the static namespace field). In the case of Grails' built-in taglibs:

g.resource(dir: 'images/icons/flag-icons/', file: 'gb.png')

Grails 2.3 changes css font-face url to resource:/...

The solution was to disable CSS processing in Config.groovy:

grails.resources.rewrite.css = false

I found the tip how to do that on the Grails mailing list.

How to link to an image from a CSS file when using the resource plugin 1.2.14

Looks like adding grails.resources.rewrite.css = false can solve my problem. Found here.



Related Topics



Leave a reply



Submit