adobe / htl-spec

HTML Template Language Specification
Apache License 2.0
280 stars 146 forks source link

data-sly-resource scope #26

Closed MaartenTutak closed 8 years ago

MaartenTutak commented 8 years ago

Hi,

What is the reasoning behind the following quote?

The scope of the data-sly-resource statement isn't passed to the template of the included resource.

If I understand correctly it's not possible to do component composition using data-sly-resource because the scope is not shared between a parent and a child component.

e.g. passing a variable as a session attribute is not possible

MaartenTutak commented 8 years ago

I guess a more general question would be : what is defined as the "scope" in this case?

paul-bjorkstrand commented 8 years ago

Scope, in this case, means "all locally defined/described variables. Another way of looking at this is that anything that is data-sly-use.IDENTIFIER or data-sly-test.IDENTIFIER are not passed to the included resource. Also, any global variable that is dependent upon the resource that is being included (e.g. currentNode), is not passed in (they are set with the new resource's values).

In reality, the semantics are nearly identical to JSP, with the exception of being able to set scopes on variables. not being built in.

Side note: using session scope is not the best idea for a stateless application. If you just need the parent/child relationship, you may want to look into a request scope attribute. That aside, if you can design your components to not need to share information, then you will be better off, with a more easily maintainable product. I do understand, though, that this kind of behavior sometimes unavoidable given the project constraints (usually not having the time to find a better solution).

To emulate <c:set scope="request" .../> you can use a sightly use script:

set-attribute.js

use(function() {
  "use strict";
  request.setAttribute(this.name, this.value);
});
<sly data-sly-use="${'set-attribute.js' @ name='attribute-name', value='attribute-value'}"/>

get-attribute.js

use(function() {
  "use strict";
  return request.getAttribute(this.name);
});
<sly data-sly-use.attributeValue="${'get-attribute.js' @ name='attribute-name'}"/>
MaartenTutak commented 8 years ago

Ah indeed, I meant to say request attribute instead of session attribute. For example, say that I'm creating a form component which is composed of radio button components. How would you suggest I handle the case where data available in the parent component will determine which child radio button component is active? I've seen data-sly-template being used for this, because parameters can be passed.

paul-bjorkstrand commented 8 years ago

I would recommend -template as well, as long as it is all in the same component. Otherwise, my code sample above (barring any bugs, I wrote it without verifying its functionality) will work the same as <c:set scope="request".../>

MaartenTutak commented 8 years ago

Indeed, in my example the parent component would be using the template of the child component which would be in a different directory. So I conclude that request attributes are preferred over template calling in this case. Thanks for your help!