Did you ever came across such requirement, when you have your static resource name in a variable and you have to use that variable to access static resource on your visualforce page. Lets see Using Variable for Static Resource.
Lets understand this with an example.
We have 3 static resources for 3 colours Red, Yellow and Green. On the Contact Object we have a pick-list field Colour (Colour__c) which have values Red, Yellow and Green.
Now we have a custom visualforce page and Apex class, the page shows some basic information of the contact record and it should show image stored in the static resource for the colour.
To achieve this We have to use
1 |
<img src="{!$Resource[Contact.Colour__c]}"/> |
If we have name of static resource in an apex variable.
1 2 3 4 5 6 |
public class ContactDetailsController{ public String colourName {get;set;} public ContactDetailsController(){ colourName = 'Red'; } } |
On the visualforce page we have to use
1 |
<img src="{!$Resource[colourName]}"/> |