+ Start a Discussion

Dynamically passing resource name in the image tag

I need to display an image on Vf based upon the value {!IQnews.image}.


I have createad image files for  the values expected in IQnews.image and zipped them in IQNews folder. in the below line I was trying to include the value, but it s not working.


<apex:image value="{!URLFOR($Resource.IQNews, '{!IQnews.image}')}" width="50" height="50"/>

Any ideas about how to deal this scenario.




To reference a file in an archive, use the URLFOR function. Specify the static resource name that you provided when you uploaded the archive with the first parameter, and the path to the desired file within the archive with the second. For example:

<apex:image url="{!URLFOR($Resource.TestZip, 'images/Bluehills.jpg')}"  width="50" height="50"/>

I would like to dynamically pass the file name, in your example Bluehills.jpg, but in my case it comes from the variable{! IQnews.image}.

Any ides how to do it.

Saurabh DhobleSaurabh Dhoble

i'm assuming your file name comes from some sort of a property, something like this :

    public String imgName
            return 'images/controls.png';


In this case, you can use the "imgName" variable like this :

<apex:page standardController="Account" extensions="SimpleExampleController">
    <apex:image value="{!URLFOR($Resource.ColorBox_CSS, imgName)}" />


No need to put the variable name in curly braces if you are already using URLFOR. Let me know if this works.