Find your content:

Search form

You are here

Dynamically get the path to a static resource

 
Share

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');
   }

Attribution to: JannieT

Possible Suggestion/Solution #1

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


Attribution to: Avidev9

Possible Suggestion/Solution #2

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');
   }

Attribution to: Phil Hawthorn

Possible Suggestion/Solution #3

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.


Attribution to: snugsfbay
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/5004

My Block Status

My Block Content