8

In lightning, we can reference to static resource by a value provider like:

<img src="{!$Resource.staticResource}"/>

Is there any smart way to make it dynamic like:

<aura:attribute name="resourceName" type="String" default="res"/>
<img src="{!$Resource.v.resourceName}"/>

For VF+Apex it was possible, but the documentation for Lightning does not say anything about that kind of possibility. Have anyone, by chance, found a smart way to do that ?

Thanks!

4 Answers 4

2

Ok, I've found 1 way, but it's not official (not stated anywhere), so not sure how long it would work - use at your own risk!

<img src="{!'/resource/'+v.resourceName}"/>

If anyone founds any other way - please share.

3
  • it's not working for me
    – Suyash
    Commented Nov 26, 2017 at 15:40
  • isn't you also need to use timestamp before resource name?
    – ytiq
    Commented Jul 6, 2018 at 16:21
  • @ytiq It's actually not required, but may cause issues with caches. The reason why the time stamp is used is to perform cache busting. And, as it happens, you don't need to use the time stamp of the file; any random series of numbers (e.g. the current epoch time) will suffice.
    – sfdcfox
    Commented Jul 6, 2018 at 16:23
2

Yes, I found out the way to display the resource name dynamically. You can do that using below statement,

src = "{!$Resource.resourceName + '/parentFolderName/' + v.imageName + '.jpeg'}"

Note : Make sure image extension should ".jpeg' (you can change image extension whichever you want).

Hope this will resolve your problem.

0

To further elaborate on @KamilK answer, if you have a custom URL set up (force.com/customer), you need to add that to the resource URL: {!'/customer/resource/' + v.resourceName}

0

According to this Salesforce Developers Blog post By David Green Advanced SEO for Lightning Communities in the Adding social media tags section

Note that in the future we plan to support $Resource.resourceName in our meta tag editors, but for now you should leverage the following URL pattern for static resources within Community Builder: /sfsites/c/resource/{resource name}.

I tried that and it worked with {!'/sfsites/c/resource/'+v.source}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .