Alfeu
Alfeu

Reputation: 991

AEM Sightly - Is it possible to create custom data-sly attributes?

I was wondering if there is a way to create custom attributes, e.g.:

   <div data-sly-myAttribute="${whatever}"></div>

Just like custom JSP tags.

EDIT:

As of May 4, 2014, it was not possible, according to this blog. Is there any news on this?

Upvotes: 4

Views: 2444

Answers (2)

Jordan Shurmer
Jordan Shurmer

Reputation: 1066

EDIT: Looks like this is not doable at this point, since the impl classes that would be needed are not exported by the bundle they come in. Thanks to Radu Cotescu for pointing that out in a comment.

I'll leave my original answer below. If someone really needed or just wanted to you could fork the Sling repo on github and add/deploy your own plugin or simply export the necessary impl package and add the plugin to your own codebase



Looking through the Sightly source code you can see a list of what they call Plugins, which provide the implementation for each of the sightly block statements. Those can be found here: https://github.com/apache/sling/tree/trunk/bundles/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/plugin

This is just a guess, and I haven't tried it yet, but it seems like you may be able to provide your own class which extends org.apache.sling.scripting.sightly.impl.plugin.PluginComponent and is a Plugin OSGi service. I would try copying one of the existing Plugins and see if you can get it to work with a new name. Maybe the TextPlugin, it seems pretty straight forward.

Hopefully this will point you in a direction that will lead to some fun :)

Something like this perhaps

@Component
@Service(Plugin.class)
@Properties({
        @Property(name = Plugin.SCR_PROP_NAME_BLOCK_NAME, value = "foo"),
        @Property(name = Plugin.SCR_PROP_NAME_PRIORITY, intValue = 9)
})
public class FooPlugin extends PluginComponent {

    @Override
    public PluginInvoke invoke(final Expression expression, PluginCallInfo callInfo, final CompilerContext compilerContext) {
        return new DefaultPluginInvoke() {

            @Override
            public void beforeChildren(PushStream stream) {
                String variable = compilerContext.generateVariable("fooContent");
                stream.emit(new VariableBinding.Start(variable,
                        compilerContext.adjustToContext(expression, MarkupContext.TEXT, ExpressionContext.TEXT).getRoot()));
                stream.emit(new OutVariable(variable));
                stream.emit(VariableBinding.END);
                Patterns.beginStreamIgnore(stream);
            }

            @Override
            public void afterChildren(PushStream stream) {
                Patterns.endStreamIgnore(stream);
            }
        };
    }
}

then use it in a sightly file

<div data-sly-foo="${properties.jcr:description}">This text should get replaced</div>

I'll update this answer if I ever get around to trying it out.

note: I will say however, if you are trying to do this in an actual scenario there is probably a better way to solve the problem you're trying to solve this way. The sightly team has attempted to give us everything we need to do what sightly is intended to do.

Upvotes: 0

Radu Cotescu
Radu Cotescu

Reputation: 534

No, it's not possible to create your own block elements since that implementation will not conform to the specification [0]. There are more subtleties involved than just adding a new plugin, one of them being block elements priority when multiple blocks are used on the same HTML element; not to mention the fact that if this was possible nothing would stop you from overriding the provided plugins.

However, if you think that there's a need for a new block element then please send a pull request to the specification, with a well defined use case. Furthermore, it would probably help if you'd discuss your use case on the Apache Sling development mailing list [1] - maybe what you need is something that other developers have thought of as well, in which case collaboration definitely helps finding the best solution to the problem.

[0] - https://github.com/Adobe-Marketing-Cloud/sightly-spec/blob/1.2/SPECIFICATION.md
[1] - https://sling.apache.org/project-information.html#mailing-lists

Upvotes: 3

Related Questions