Sightly data sly test question | Community
Skip to main content
Level 4
Mar 12, 2018 1:44 PM UTC
Solved

Sightly data sly test question

Is it possible to still use code within a containing div even if that containing tag resolves data-sly-test as false?

As an example:

I'm trying to show an image that may or may not have an a tag wrapped around it. I have the following code:

<a data-sly-test="{image.link}" href="{image.link}">

     <img src="${image.path}">

</a>

The image link is optional so some images won't have a link wrapped around them. However, if a link isn't specified, all of the code is removed. Is it possible to keep the <img src> line even if that test in the wrapping a tag is false?

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Radha_Krishna_N
Radha_Krishna_NAccepted solution
Level 3
Mar 12, 2018 1:55 PM UTC

Hi,

If the link is not authored, then data-sly-test="{image.link}" will be false. the content of the anchor tag will not be displayed. AFAIK you should have something like below

<a data-sly-test.noLink="{image.link}" href="{image.link}">

     <img src="${image.path}">

</a>
<img data-sly-test="${!noLink}" src="${image.path}">

Thanks,

Radha Krishna N

7 replies

Nupur_Jain
Employee
Jun 3, 2020 9:06 PM UTC

Hi @alistairp781078 

 

There is a attribute of HTL called data-sly-unwrap which basically removes the wrapping element if not required. You can set data-sly-unwrap conditionally like

<a data-sly-unwrap="{!image.link}" href="{image.link}"> <img src="${image.path}"> </a>

 

To read more about this attribute. Read examples here https://docs.adobe.com/content/help/en/experience-manager-htl/using/htl/block-statements.html

 

Hop it helps!

Nupur

Translate
timw92982900
New Participant
Jun 3, 2020 8:11 PM UTC

There is another option, although it retains an element around your image:

<a data-sly-element="${image.link ? 'a' : 'div'}" href="${image.link}" class="image-block"> <img src="${image.path}"> </a>

If `image.link` is authored, it outputs <a>, else it outputs <div>.

Con:

  • the div will have an empty 'href' 

Pro:

  • you have a consistent element wrapping your image to help control styling.
  • No redundant code just for an if/else statement. 
Translate
smacdonald2008
Level 10
Mar 12, 2018 1:58 PM UTC

That is like saying in Java is there a way to get a statement to work in an If statement as such

if (val1 < val2)

     do something

This will not execute if val2 is less that val1. Same as HTL - if that condition is false - then it will not be included in the page.

Translate
Level 4
Mar 12, 2018 1:57 PM UTC

Cheers for the advice, Radha. I'll go with that solution.

Translate
Radha_Krishna_N
Radha_Krishna_NAccepted solution
Level 3
Mar 12, 2018 1:55 PM UTC

Hi,

If the link is not authored, then data-sly-test="{image.link}" will be false. the content of the anchor tag will not be displayed. AFAIK you should have something like below

<a data-sly-test.noLink="{image.link}" href="{image.link}">

     <img src="${image.path}">

</a>
<img data-sly-test="${!noLink}" src="${image.path}">

Thanks,

Radha Krishna N

Translate
Level 4
Mar 12, 2018 1:54 PM UTC

I'm aware that the content of the containing tag will not show if the test evaluates as false.

What I'm asking is, is there some way of overriding that feature?

<a data-sly-test>

     <img>

<a>

The a tag is an optional feature in the component. I want to add a test on that div, because if a link hasn't been specified by the content editor, I don't want the tag to show up. Regardless, I will always want the img tag to show.

Translate
smacdonald2008
Level 10
Mar 12, 2018 1:48 PM UTC

Are you asking if this equals false if the image will still show up?

This is answered in the spec:  htl-spec/SPECIFICATION.md at master · Adobe-Marketing-Cloud/htl-spec · GitHub

2.2.5. Test

data-sly-test: 

Keeps, or removes the element depending on the attribute value.

Element: shown if test evaluates to true.

Content of element: shown if test evaluates to true.

Attribute value: optional; evaluated as Boolean (but not type-cased to Boolean when exposed in a variable); evaluates to false if the value is omitted.

Attribute identifier: optional; identifier name to access the result of the test.

Removes the whole element from the markup if the expression evaluates to false.

Translate