18

I would like to control which style sheet is included in my Visualforce page at load time, so instead of having:

<link rel="stylesheet" type="text/css" href="{!$Resource.eng_mobile}" />

I would like:

<link rel="stylesheet" type="text/css" href="{!myTheme}" />

So, in my page controller, what is the syntax to get the path to a static resource. Something like this, maybe?

String myTheme = $Resource.getPath(lang + '_' + size);

Searching for "$Resource" on developerforce.com gives at least 54,000 results.

Edit:

For my use case I probably need to get the resource url in the controller:

   if (!customCss) {
      myTheme = $Resource.getPath(lang + '_' + size);
   } else {
      myTheme = ApexPages.currentPage().getParameters().get('css');
   }
15

You can get the URL using the following util class.

 public class StaticResourceURL
    {
        // Pass the resource name
        public static String GetResourceURL(String resourceName){

            // Fetching the resource
            List<StaticResource> resourceList= [SELECT Name, NamespacePrefix, SystemModStamp FROM StaticResource WHERE Name = :resourceName];

            // Checking if the result is returned or not
            if(resourceList.size() == 1){

               // Getting namespace
               String namespace = resourceList[0].NamespacePrefix;
               // Resource URL
               return '/resource/' + resourceList[0].SystemModStamp.getTime() + '/' + (namespace != null && namespace != '' ? namespace + '__' : '') + resourceName; 
            }
            else return '';
        }
    }

This is a excerpt from this blog http://forceguru.blogspot.in/2012/05/static-resource-url-in-apex.html

  • Nice! It actually handles managed package static resources as well. It's worth noting that this (probably) isn't officially supported and in theory could change in the future, but generally should be pretty safe to do. – ca_peterson Dec 3 '13 at 3:42
10

You can also refer to resources dynamically:

<link rel="stylesheet" type="text/css" href="{!$Resource.eng_mobile}" />

becomes:

<link rel="stylesheet" type="text/css" href="{!$Resource[myTheme]}" />

Which would at least save you a query to StaticResource if you had a property on you controller and set it as:

   if (!customCss) {
      myTheme = lang + '_' + size;
   } else {
      myTheme = ApexPages.currentPage().getParameters().get('css');
   }
  • Ha! this looks very promising, but my use case is just a bit more complicated. I need to set the whole value in the controller. But +1 for this tip – JannieT Dec 3 '12 at 14:12
  • So you could just set myTheme value within the controller implementing any required logic... – Phil Hawthorn Dec 3 '12 at 14:22
  • See the edit at the end of my question – JannieT Dec 3 '12 at 15:26
  • @JannieT Yep, i changed the answer too :) – Phil Hawthorn Dec 3 '12 at 15:31
  • Sorry, won't work. I'm using: http://mydomain.com/myvisualpage?css=http://someotherdomain.net/assets/fancy_yello.css – JannieT Dec 3 '12 at 17:35
2

Have you tried something like this:

<apex:stylesheet value="{!myTheme}"/>

If you are referencing CSS pages based on their specific URLs, you aren't really using static resources at all, so you would not need $Resource. Static Resources are those files which will generally remain unchanged and so can be uploaded directly to Salesforce (where you can't really edit them, but may upload new versions). Judging from your comment on Phil's answer, it looks like you want to reference external files instead and so your code should work if you leave out the static resource code altogether.

  • Thanks @snugsfbay, but I want to use a css file that is a static resource, unless someone calls the page with a url parameter that points to a self-hosted (external) css file. – JannieT Dec 4 '13 at 16:57
  • Why not have all your CSS files in the same place, either in static resources or external files? If that simply can't be accomplished, why not take advantage of the built in hierarchy of CSS and load a second file from a different source only if needed to override the first? – snugsfbay Dec 4 '13 at 23:04
  • Sorry, the answer to that one is too long to type in here. – JannieT Dec 5 '13 at 6:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.