style attribute not being rendered | Community
Skip to main content
carloso98118279
New Participant
Jan 26, 2017 9:17 PM UTC
Solved

style attribute not being rendered

I'm trying to add a style dynamically with HTL.

HTL code is:

<section class="header-page" data-sly-attribute.style="${header.cssStyle}" data-sly-use.header="com.acme.foo.Header"> </section>

And in my Java class I have:

public class Header extends WCMUsePojo { private String imagePath; @Override public void activate() throws Exception { ValueMap properties = getProperties(); this.imagePath = properties.get("fileReference", ""); } public String getCssStyle() { return imagePath.isEmpty() ? "" : String.format("background-image: url('%s')", imagePath); } }

But it's not being rendered, even when imagePath gets a valid value. I also tried using contexts, but nothing seems to be working.

Any ideas as what might be the problem?

Thanks

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
carloso98118279
carloso98118279Accepted solution
New Participant
Jan 27, 2017 1:38 AM UTC

Thanks PuzanovsP!

Unfortunately it was my mistake when trying to put the code here, but in my code the method exists so the error is elsewhere. I edit my question to remove it since it's not really relevant.

What I think is happening is that HTL thinks that putting background-image: url('/some/path') in the file can be harmful, I just don't know why. So my question is still open.

Thanks for the suggestion on SlingModels it seems interesting.

12 replies

carloso98118279
carloso98118279AuthorAccepted solution
New Participant
Jan 27, 2017 1:38 AM UTC

Thanks PuzanovsP!

Unfortunately it was my mistake when trying to put the code here, but in my code the method exists so the error is elsewhere. I edit my question to remove it since it's not really relevant.

What I think is happening is that HTL thinks that putting background-image: url('/some/path') in the file can be harmful, I just don't know why. So my question is still open.

Thanks for the suggestion on SlingModels it seems interesting.

Translate
Peter_Puzanovs
New Participant
Jan 26, 2017 10:30 PM UTC

Hi Carloso,

1) Create getter for cssClass variable, then, it will be shown.

2) Consider using SlingModels, instead of WCMUse, it will promote cleaner code and better system in general.

Regards,

Peter

Translate