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
  • 22378 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

Dinu_Arya
New Participant
October 16, 2015

Feike Visser wrote...

thanks for the feedback. But I would still recommend to limit the use of data-sly-unwrap. It might make your components a bit messy..

This...

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

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

 

Can also be:

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

 


Hi Feike,

Then how can I assign some value to a variable and check the condition in the same step? Please suggest.

Thanks,

AryA.

GabrielWalt
Employee
October 16, 2015

I agree with Feike. Keep in mind that each data-sly-unwrap you're doing will represent one step you're moving away from having your template markup to correspond to your resulting markup. The goal of Sightly is to keep the two as close together as possible for making it as obvious as possible for someone who reads the template to understand what the final resulting markup is going to be.

GabrielWalt
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.