How to set convenience variables in sightly? | Community
Skip to main content
October 16, 2015
Solved

How to set convenience variables in sightly?

  • October 16, 2015
  • 13 replies
  • 22391 views

Hey all,

is there a way to set variables in sightly? Basically I have a lot of variables in my html template and it's getting kind of messy, especially with things like inherited properties [1] and combination of properties for conditions [2]. In a jsp I could just use <c:set> but not here. Any suggestions?

Thanks!

Paul

[1] example for long properties: ${inheritedPageProperties['parBloc1/topbanner/narrow']}

[2] data-sly-test="${inheritedPageProperties['parBloc1/topbanner/narrow'] && inheritedPageProperties['parBloc1/topbanner/test']}"

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Feike_Visser1

hi,

What you can do is things like this:

<div data-sly-test.narrow="${inheritedPageProperties['parBloc1/topbanner/narrow']}">${narrow}</div>

And then later on...

<div data-sly-test=${ narrow && wcmmode.edit}">...</div>

Let me know if this works for you...

best,

Feike

13 replies

GabrielWalt
Adobe Employee
Adobe Employee
October 16, 2015

His code example is doing exactly that, it sets yourName in the test, which can then be reused within or later on:

<div data-sly-test.yourName="${currentPage.name}">....${yourName}</div>

Have a quick look at the docs of data-sly-test to better understand that behavior.

More generally though, if you need to set many variables in your template, I'd rather advise you to use the Use-API to prepare all the variables you'll need in your template. This will probably be easier to understand by someone reading your template and thus be more maintainable than setting variables in your template.

Feike_Visser1
Adobe Employee
Adobe Employee
November 7, 2018

Since HTL1.4 you can now also use data-sly-set.

Example here: htl-examples/htl1_4.html at master · heervisscher/htl-examples · GitHub

Dinu_Arya
Level 6
February 20, 2019

Will it work in AEM 6.2 or only in 6.4?