0

I'm trying to use a label within a lightning web component. In particular it's value is something like

blah blah see <a target="_blank" href={labels.ANOTHER_LABEL}>link</a> for more information

where we are keeping the link's address in another custom label. I can't seem to get the lightning web component to recognize {labels.ANOTHER_LABEL} as a variable, it keeps trying to call it as a string literal.

I've tried both the lightning app builder and lightning web component formats from https://help.salesforce.com/s/articleView?id=sf.cl_about.htm&type=5 and neither work (the "{") doesn't get recognized as code.

6
  • You cannot do this. You can use "mailmerge" approaches, substituting values where the "pattern" includes markers like {0} or {1}. However, embedding HTML in labels is not a good idea and certainly would not be allowed to be done in managed packages which are reviewed by Salesforce's security team since an admin could inject hacks into pages by crafting HTML that includes JavaScript. See this other Q&A for more info.
    – Phil W
    Commented Jan 11, 2023 at 18:25
  • This isn't for a managed package. It's for a Salesforce Site that we made in experience builder so a hacker wont have access to the labels. Is there a better way to reference a url stored in a label from another label?
    – J. Larson
    Commented Jan 11, 2023 at 18:34
  • 1
    Storing URLs in labels instead of as custom settings is actually an anti-pattern unless you need different labels for different languages. You need to use something like mailmerge to combine the labels. In apex this uses String.format. If you want to do it in JavaScript use something like this. You then use {0} in the first label and pass the second label to the formatting function.
    – Phil W
    Commented Jan 11, 2023 at 19:29
  • BTW, my point about labels and security is still valid even outside a managed package; a disgruntled admin could hide malicious code in a label that is explicitly rendered as HTML in a page.
    – Phil W
    Commented Jan 11, 2023 at 19:31
  • Did you import the label in JS as shown here. developer.salesforce.com/docs/component-library/documentation/… Commented Jan 12, 2023 at 3:10

0

You must log in to answer this question.

Browse other questions tagged .