Expand my Community achievements bar.

Dynamic styling of CSS using Style Tag Visual studio Code css-identifierexpected warning

Avatar

Level 3

Hi,

I ve got the following code in one of my custom component html templates:

<style data-sly-test="${formInput.getGrowFactor}">
        #${formInput.name @ context="styleString"}Id${formInput.uniqueId @ context="styleString"}Wrapper {
          flex-grow: ${formInput.getGrowFactor @ context="styleString"};
        }
    </style>


the code itself is working, but my IDE Visual Studio Code is generating the following errors:
css-identifierexpected
css-propertyvalueexpected

Looks like htl  ${} syntax is not compliant with some other validation tool.

Anybody knows how I may get rid of these errors ?

Thanks a lot for your support in advance.

 

--

volker
 

5 Replies

Avatar

Community Advisor

Hi @vhochsteinTef 

You can create the whole CSS rule as string in Sling Model and return it to HTL to make whole logic simplified

 

Sling Model

return String.format(
                "#%sId%sWrapper { flex-grow: %s; }",
                name,
                uniqueId,
                growFactor
            );

 

HTL

<style data-sly-test="${formInput.generatedStyle}">
    ${formInput.generatedStyle @ context='styleString'}
</style>

 

 

 

Arun Patidar

AEM LinksLinkedIn

Avatar

Level 3

Hi Arun,

thanks a lot for your suggestion.

Your version generates the following errors:

vhochsteinTef_0-1743080672246.png


Unfortuently, it s not solving the issue.

--

volker

Avatar

Administrator

@vhochsteinTef Did you find the suggestions helpful? Please let us know if you need more information. If a response worked, kindly mark it as correct for posterity; alternatively, if you found a solution yourself, we’d appreciate it if you could share it with the community. Thank you!



Kautuk Sahni

Avatar

Level 3

Hi,

nothing helped so far...


Avatar

Community Advisor

Hi @vhochsteinTef ,

The issue arises because HTL expressions (${}) inside the <style> tag are not recognized as valid CSS by Visual Studio Code’s CSS validation engine.

Even though your code works fine in AEM, VS Code is treating it as invalid because the ${} syntax is not standard in CSS.

Solution 1: Disable CSS Validation in VS Code

Since this is a false positive, you can disable CSS validation for your workspace:

Open VS Code Settings (Cmd + , on Mac or Ctrl + , on Windows).
Search for css validate.
Find CSS › Validate: Enable and uncheck the box (or set to false in settings.json):

"css.validate": false