The context
I'm working on an AEM 6 project that uses Sightly as the templating language. I'm facing a use case, in which I want to show or hide certain parts of the markup depending on the presence of a Sling selector.
For example, a request to /content/my-project/my-page.html
should yield a basic page view and when a request is made to /content/my-project/my-page.ubermode.html
, Sling should return the same content represented by a slightly different HTML document.
According to the Sling Cheat Sheet, it should be possible to use a different script.
I managed to implement this in a component by placing two Sightly scripts, mycomponent.html
and ubermode.html
(named after the selector)
/apps/(...)/mycomponent
|- .content.xml
|- _cq_editConfig.xml
|- dialog.xml
|- mycomponent.html
|- ubermode.html
This does require some code duplication when it comes to the HTML structure but it works fine.
However, in this particular case, I need to do the same think at the renderer level (let's call this myapp/core/renderers/fancyPageRenderer
). What's more, the renderer has a different renderer as its sling:resourceSuperType
(let's call this parent renderer myapp/core/renderers/genericPageRenderer
) and relies on a moderately complex series of includes (data-sly-include
).
In fancyPageRenderer
, I override one of the scripts initially defined and included in genericPageRenderer
. This is the part that I'd like to be different when the ubermode
selector is used. Let's call this script mainColumn.html
I've tried different naming conventions to match the selector but none of them worked in a satisfactory way.
This was the initial structure
/apps/(...)/renderers/fancyPageRenderer
|- .content.xml
|- mainColumn.html //this overrides a script included by a parent renderer
Here's what I tried:
/apps/(...)/renderers/fancyPageRenderer
|- .content.xml
|- mainColumn.uber.html
|- mainColumn.html
This simply didn't work and mainColumn.html
would be included every time.
/apps/(...)/renderers/fancyPageRenderer
|- .content.xml
|- uber.html
|- mainColumn.html
This caused the uber.html
script to be used but the resulting page did not contain any markup defined in the other scripts included in the genericPageRenderer
I imagine I could just copy all the relevant scripts and includes to fancyPageRenderer
but that would result in massive and completely unacceptable code duplication.
I also know that it's possible to manually add, remove or replace selectors using data-sly-resource
or just have the original selectors used but in my case, it's data-sly-include
and not data-sly-resource
that is widely used.
Is there an elegant way to work around this problem?