I want get the custom attribute in AEM HTL use the request object. this attribute already set to request through javascript use API. like this:
test.js
use(function () {
request.setAttribute('someKey', 'someValue');
});
test.html
<html data-sly-use.logic="test.js">
<!-- some code ... -->
<body>
<div>${request.attribute['someKey']}</div>
</body>
</html>
result:
<html data-sly-use.logic="test.js">
<!-- some code ... -->
<body>
<div></div> <!-- empty -->
</body>
</html>
We can get request attribute without Java? Many thanks.
Since HTL syntax is rather declarative than imperative, calling methods/functions with parameters is not supported. That means you cannot do something like ${request.getAttribute('someKey')}. Unfortunately, the Java HttpServletRequest API does not expose the attributes as a map (it does that for parameters) so you cannot do ${request.attributeMap['someKey']} either, you'll need to fetch the attribute and expose it from your use-object/model.
Though you cannot send request attribute the way you are trying to do right now , you can set that as a variable from your server side JS and access it in your code.
In your test.js
use(function () {
return {
someKey: someValue
};
});
In your test.html
<html data-sly-use.logic="test.js">
<!-- some code ... -->
<body>
<div>${logic.someKey}</div>
</body>
</html>
This should print the someValue as output for you.