How to Subtract numbers in Slightly | Community
Skip to main content
Level 2
Sep 13, 2023 7:11 PM UTC
Solved

How to Subtract numbers in Slightly

Hi Team,

 

I would like to subtract two numbers in Slightly. I  have used  "-" symbol but its not working.

 

Example :

${itemList.Count-1}

 

Regards

MADHU

 

 

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
EstebanBustamante
EstebanBustamanteAccepted solution
New Participant
Sep 13, 2023 8:53 PM UTC

Hi,

 

You cannot subtract using HTL (formerly known as Sightly). The purpose of Sightly is to be a template language, not a logical operator. For this type of scenario, the best approach is to use a Sling Model, where you can perform any custom operation and then expose it back to the HTML.

 

However, if you are trying to manipulate a list index, keep in mind you have these options available: 

itemList: Object holding the following properties:

  • index: zero-based counter ( 0..length-1).
  • count: one-based counter ( 1..length).
  • first: true if the current item is the first item.
  • middle: true if the current item is neither the first nor the last item.
  • last: true if the current item is the last item.
  • odd: true if index is odd.
  • even: true if index is even.

 

If you need to use the Sling model approach instead you could try something like this:
Sling Model:

@Model(adaptables = Resource.class) public class MyModel { @586265 private String propertyFromDialog; @PostConstruct protected void init() { } //Do your logic below. You can use the properties from the dialog once injected public String getSubstraction() { return 1; } }

Then in HTL

<sly data-sly-use.model="my.java.package.MyModel" /> <p>The result is: ${model.substraction}</p>

Please find more information here: https://redquark.org/aem/day-07-sling-models/#usage-in-htl

https://experienceleague.adobe.com/docs/experience-manager-learn/getting-started-wknd-tutorial-develop/project-archetype/component-basics.html?lang=en 

 

Hope this helps

Esteban Bustamante

3 replies

kautuk_sahni
Employee
Sep 14, 2023 12:10 PM UTC

@madhu61 Do you find the suggestion from Esteban useful? Please let us know if more information is required. Otherwise, please mark the answer as correct for posterity. If you have found out solution yourself, please share it with the community.

Translate
Kautuk Sahni
EstebanBustamante
EstebanBustamanteAccepted solution
New Participant
Sep 13, 2023 8:53 PM UTC

Hi,

 

You cannot subtract using HTL (formerly known as Sightly). The purpose of Sightly is to be a template language, not a logical operator. For this type of scenario, the best approach is to use a Sling Model, where you can perform any custom operation and then expose it back to the HTML.

 

However, if you are trying to manipulate a list index, keep in mind you have these options available: 

itemList: Object holding the following properties:

  • index: zero-based counter ( 0..length-1).
  • count: one-based counter ( 1..length).
  • first: true if the current item is the first item.
  • middle: true if the current item is neither the first nor the last item.
  • last: true if the current item is the last item.
  • odd: true if index is odd.
  • even: true if index is even.

 

If you need to use the Sling model approach instead you could try something like this:
Sling Model:

@Model(adaptables = Resource.class) public class MyModel { @586265 private String propertyFromDialog; @PostConstruct protected void init() { } //Do your logic below. You can use the properties from the dialog once injected public String getSubstraction() { return 1; } }

Then in HTL

<sly data-sly-use.model="my.java.package.MyModel" /> <p>The result is: ${model.substraction}</p>

Please find more information here: https://redquark.org/aem/day-07-sling-models/#usage-in-htl

https://experienceleague.adobe.com/docs/experience-manager-learn/getting-started-wknd-tutorial-develop/project-archetype/component-basics.html?lang=en 

 

Hope this helps

Translate
Esteban Bustamante
madhu61Author
Level 2
Sep 14, 2023 6:07 AM UTC

Hi EstebanBustamante,

 

Thanks for your reply. Let me implements suggested approach in sling modals.

 

Regards

MADHU

Translate