Expand my Community achievements bar.

Guidelines for the Responsible Use of Generative AI in the Experience Cloud Community.

HTL: How to dump the contents of my variable?

Avatar

Level 9

So I have this line in my HTML

<sly data-sly-use.menuItem="${'com.myhost.core.impl.view.global.TopNavigationItemView'}"></sly>

menuItem has many properties like id, activeclass and etc.

I would like to dump/show the full contents of menuItem without having to specify the properties (example: menuItem.id) individually.

Is this possible? If yes, how?

I tried this

<dl data-sly-list="${menuItem}">

    <dt>index: ${itemList.index}</dt>

    <dd>value: ${item.title}</dd>

</dl>

but I cannot see it in my page. I even tried "view page source" and nothing there.

Any ideas? Thanks!

4 Replies

Avatar

Level 4

You may need to create a new method in your Use Class as follows:

public String variableDump() {

  return new ToStringBuilder(this).

// All your variables

  append("name", name).

  append("age", age).

  append("smoker", smoker).

  toString();

  }

Then you can call this function in your HTL Template to dump all the variables.

Avatar

Level 9

why that would work for a custom class? what happens if I want to dump the contents of AEM built-in variables like $properties/$pageProperties?

Thanks

Avatar

Level 4

Did you forget the concept of Encapsulation???

If you can get a dump of properties (which may be protected or private) then what is the benefit of encapsulation???

Avatar

Level 9

try this example in sample6

HTL introduction part 1

  • inside a data-sly-list you have access to a list-variable that contain things like : index, count, odd, even, first, last, middle
  • in the expression you see the use of the ternary operator