When Sightly Meets
Slice
Tomasz Niedźwiedź
Software Engineer
<%@ include file="/libs/foundation/global.jsp" %>
<% // Some Java %>
<script> //JavaScript anyone? </script>
<style> /* Inline CSS rules! */ </style>
<% // Some more Java %>
<h1>If you’re careful enough, you can find HTML</h1>
<c:if test="someVariableSetSomewhere">
<p>HTML elements are best mixed with JSP tags</p>
</c:if>
Java Server Pages – The Good, the Bad
and the Very Ugly
<%@ include file="/libs/foundation/global.jsp" %>
<% // Some Java %>
<script> //JavaScript anyone? </script>
<style> /* Inline CSS rules! */ </style>
<% // Some more Java %>
<h1>If you’re careful enough, you can find HTML</h1>
<c:if test="someVariableSetSomewhere">
<p>HTML elements are best mixed with JSP tags</p>
</c:if>
Java Server Pages – The Good, the Bad
and the Very Ugly
• Almost plain HTML
• Iteration
• Hiding/showing elements
• Including scripts
• Access to bindings
• Calling backend code
A sightly, minimalistic beauty
<h1 data-sly-test="${properties.jcr:title}">
${properties.jcr:title}
</h1>
${inheritedPageProperties.twitterHandle}
<h1>
${properties.jcr:title || "Default title"}
</h1>
A sightly, minimalistic beauty
Access to JCR data, really basic logic
<ul data-sly-list="${ currentPage.listChildren }">
<li>${item.name}</li>
</ul>
<div data-sly-repeat="${currentPage.listChildren}">
${item.name}
</div>
data-sly-list
data-sly-repeat (AEM 6.1)
A sightly, minimalistic beauty
Iteration
<template data-sly-template.validateBlank="${@validatableWithBlank, name}">
<sly data-sly-test="${!validatableWithBlank.blank}"
data-sly-call="${validate @ validatable=validatableWithBlank,
name=name}"
data-sly-unwrap />
<div data-sly-test="${validatableWithBlank.blank && wcmmode.edit}"
data-sly-include="blank.html"
class="blank">
</div>
</template>
data-sly-template
data-sly-call
data-sly-include
data-sly-resource
A sightly, minimalistic beauty
Code reuse
• Markup is clearly visible and easy to understand
• Easy to learn, simple, with just the necessary set of features
• Cannot affect the state of the application
• Good momentum, regular improvements
What I like about it
Sightly
Use interface
WCM Bindings
Adapter Factories
Sling Models
SliceJSP
Slice
CQ 5.3
CQ 5.4
CQ 5.6
AEM 6.0
AEM 6.1
...and onwards
Repository access
@SliceResource
public class ArticleModel {
@JcrProperty("jcr:title")
private String title;
@JcrProperty
private String text;
//getters
}
Mapping content to POJOs...
Repository access
@SliceResource
public class ArticleModel {
@JcrProperty
private PublicationMetadata publicationMetadata;
@JcrProperty
@Children(Author.class)
private List<Author> authors;
//getters
}
Slice
Repository access
@Model(adaptables = Resource.class)
public class ArticleModel {
@Inject
private PublicationMetadata publicationMetadata;
@Inject
private List<Author> authors;
//getters
}
Sling Models
Repository access
Neat! But...
Repository access
Neat! But...
Slice – Does it still count?
The landscape has changed
Sling Models provide an excellent way to
supply components with data
Using Java classes in scipts is easier than
ever
Simple models are
not really the point!
The killer feature of Slice?
The killer feature of Slice?
It’s built around Guice
• The focus is not on repository access.
• It’s about a clear, well-established dependency
injection model based on industry standards.
• Guice provides a consistent framework for the
entire application.
• JSR 330
The killer feature of Slice?
It’s built around Guice
Proven to speed up development in many projects
over the last several years.
The killer feature of Slice?
• How many objects are to be created?
• What’s allowed to share instances?
• How much control does the
programmer have?
Dependency injection with Guice
Scopes
• Method interception
Dependency injection with Guice
Aspect Oriented Programming
Dependency injection with Guice
Bindings
• Slice core
• Slice Addons
• CQ 5.5
• CQ 5.6
• AEM 6.0 (also compatible with 6.1)
Slice brings Guice to AEM
1. Instantiate an injector in your
bundle activator
2. Register OOTB modules
3. Create a new module for each
logical unit of functionality
4. Let the injector take care of wiring
up all of the dependencies in your
whole project
How to set it up
• Verbose
• Complex
• Exposes services to the whole environment.
• Sometimes you want simple things that you
want to hide and yet be able to inject them.
What about OSGi?
It does have certain dependency injection
capabilities.
Peaberry used before Slice 4.1
@OsgiService annotation in
newer versions
Slice & OSGi
public class MyClass {
@Inject
@OsgiService
ResourceResolverFactory factory;
//...
}
OSGi services can be injected
with minimal effort
• Object trees can be built thanks to
dependency injection
• Not as powerful as Guice
• AOP
• Context management
• Caching
• Less information available online on good
practices and how the model works
DI in Sling Models
Perfectly sufficient for backing your
sightly scripts... but
• Not a mature library
• Closed
• Competes with Sightly or Sling
Misconceptions
Sightly & Slice
Can they work together?
Sightly & Slice
Can they work together?
YES
Using Slice in Sightly
<div data-sly-use.model="com.example.app.ArticleModel">
<span class="text">${model.text}</span>
<ul data-sly-list="${model.links}">
<li><a href="${item.path}">${item.title}</a></li>
</ul>
</div>
Sightly without Slice
Using Slice in Sightly
<div data-sly-use.model="com.example.app.ArticleModel">
<span class="text">${model.text}</span>
<ul data-sly-list="${model.links}">
<li><a href="${item.path}">${item.title}</a></li>
</ul>
</div>
Sightly WITH Slice... Exactly the same
Slice & Sightly
• Very clear presentation layer
• Visibility of markup semantics and
structure
• Easy for frontent developers
• Easily manageable architecture of the
AEM project
• Minimal boilerplate code
• Fine-grained control over objects
Learn More
Slice 4.3 (Q3 2015)
• Slice persistence
• New Felix console tools
Slice 5.0 (Q1 2016)
• Easier consumption of models in
Servlets and Services
• Mapper improvements
http://cognifide.github.io/slice/
Learn More
Slice your code in Sightly - http://goo.gl/5HTKal
A presentation introducing Google Guice - https://youtu.be/hBVJbzAagfs
Q&A
Tomasz Niedźwiedź
Software Engineer at Cognifide
tomasz.niedzwiedz@cognifide.com
@toniedzwiedz
Thanks for
listening!

When Sightly Meets Slice by Tomasz Niedźwiedź

  • 1.
    When Sightly Meets Slice TomaszNiedźwiedź Software Engineer
  • 3.
    <%@ include file="/libs/foundation/global.jsp"%> <% // Some Java %> <script> //JavaScript anyone? </script> <style> /* Inline CSS rules! */ </style> <% // Some more Java %> <h1>If you’re careful enough, you can find HTML</h1> <c:if test="someVariableSetSomewhere"> <p>HTML elements are best mixed with JSP tags</p> </c:if> Java Server Pages – The Good, the Bad and the Very Ugly
  • 4.
    <%@ include file="/libs/foundation/global.jsp"%> <% // Some Java %> <script> //JavaScript anyone? </script> <style> /* Inline CSS rules! */ </style> <% // Some more Java %> <h1>If you’re careful enough, you can find HTML</h1> <c:if test="someVariableSetSomewhere"> <p>HTML elements are best mixed with JSP tags</p> </c:if> Java Server Pages – The Good, the Bad and the Very Ugly
  • 5.
    • Almost plainHTML • Iteration • Hiding/showing elements • Including scripts • Access to bindings • Calling backend code A sightly, minimalistic beauty
  • 6.
  • 7.
    <ul data-sly-list="${ currentPage.listChildren}"> <li>${item.name}</li> </ul> <div data-sly-repeat="${currentPage.listChildren}"> ${item.name} </div> data-sly-list data-sly-repeat (AEM 6.1) A sightly, minimalistic beauty Iteration
  • 8.
    <template data-sly-template.validateBlank="${@validatableWithBlank, name}"> <slydata-sly-test="${!validatableWithBlank.blank}" data-sly-call="${validate @ validatable=validatableWithBlank, name=name}" data-sly-unwrap /> <div data-sly-test="${validatableWithBlank.blank && wcmmode.edit}" data-sly-include="blank.html" class="blank"> </div> </template> data-sly-template data-sly-call data-sly-include data-sly-resource A sightly, minimalistic beauty Code reuse
  • 9.
    • Markup isclearly visible and easy to understand • Easy to learn, simple, with just the necessary set of features • Cannot affect the state of the application • Good momentum, regular improvements What I like about it
  • 10.
    Sightly Use interface WCM Bindings AdapterFactories Sling Models SliceJSP
  • 11.
    Slice CQ 5.3 CQ 5.4 CQ5.6 AEM 6.0 AEM 6.1 ...and onwards
  • 12.
    Repository access @SliceResource public classArticleModel { @JcrProperty("jcr:title") private String title; @JcrProperty private String text; //getters } Mapping content to POJOs...
  • 13.
    Repository access @SliceResource public classArticleModel { @JcrProperty private PublicationMetadata publicationMetadata; @JcrProperty @Children(Author.class) private List<Author> authors; //getters } Slice
  • 14.
    Repository access @Model(adaptables =Resource.class) public class ArticleModel { @Inject private PublicationMetadata publicationMetadata; @Inject private List<Author> authors; //getters } Sling Models
  • 15.
  • 16.
  • 17.
    Slice – Doesit still count? The landscape has changed Sling Models provide an excellent way to supply components with data Using Java classes in scipts is easier than ever
  • 18.
    Simple models are notreally the point!
  • 19.
  • 20.
    The killer featureof Slice? It’s built around Guice
  • 21.
    • The focusis not on repository access. • It’s about a clear, well-established dependency injection model based on industry standards. • Guice provides a consistent framework for the entire application. • JSR 330 The killer feature of Slice? It’s built around Guice
  • 22.
    Proven to speedup development in many projects over the last several years. The killer feature of Slice?
  • 23.
    • How manyobjects are to be created? • What’s allowed to share instances? • How much control does the programmer have? Dependency injection with Guice Scopes
  • 24.
    • Method interception Dependencyinjection with Guice Aspect Oriented Programming
  • 25.
  • 26.
    • Slice core •Slice Addons • CQ 5.5 • CQ 5.6 • AEM 6.0 (also compatible with 6.1) Slice brings Guice to AEM
  • 27.
    1. Instantiate aninjector in your bundle activator 2. Register OOTB modules 3. Create a new module for each logical unit of functionality 4. Let the injector take care of wiring up all of the dependencies in your whole project How to set it up
  • 28.
    • Verbose • Complex •Exposes services to the whole environment. • Sometimes you want simple things that you want to hide and yet be able to inject them. What about OSGi? It does have certain dependency injection capabilities.
  • 29.
    Peaberry used beforeSlice 4.1 @OsgiService annotation in newer versions Slice & OSGi public class MyClass { @Inject @OsgiService ResourceResolverFactory factory; //... } OSGi services can be injected with minimal effort
  • 30.
    • Object treescan be built thanks to dependency injection • Not as powerful as Guice • AOP • Context management • Caching • Less information available online on good practices and how the model works DI in Sling Models Perfectly sufficient for backing your sightly scripts... but
  • 31.
    • Not amature library • Closed • Competes with Sightly or Sling Misconceptions
  • 32.
    Sightly & Slice Canthey work together?
  • 33.
    Sightly & Slice Canthey work together? YES
  • 34.
    Using Slice inSightly <div data-sly-use.model="com.example.app.ArticleModel"> <span class="text">${model.text}</span> <ul data-sly-list="${model.links}"> <li><a href="${item.path}">${item.title}</a></li> </ul> </div> Sightly without Slice
  • 35.
    Using Slice inSightly <div data-sly-use.model="com.example.app.ArticleModel"> <span class="text">${model.text}</span> <ul data-sly-list="${model.links}"> <li><a href="${item.path}">${item.title}</a></li> </ul> </div> Sightly WITH Slice... Exactly the same
  • 36.
    Slice & Sightly •Very clear presentation layer • Visibility of markup semantics and structure • Easy for frontent developers • Easily manageable architecture of the AEM project • Minimal boilerplate code • Fine-grained control over objects
  • 37.
    Learn More Slice 4.3(Q3 2015) • Slice persistence • New Felix console tools Slice 5.0 (Q1 2016) • Easier consumption of models in Servlets and Services • Mapper improvements http://cognifide.github.io/slice/
  • 38.
    Learn More Slice yourcode in Sightly - http://goo.gl/5HTKal A presentation introducing Google Guice - https://youtu.be/hBVJbzAagfs
  • 39.
  • 40.
    Tomasz Niedźwiedź Software Engineerat Cognifide tomasz.niedzwiedz@cognifide.com @toniedzwiedz Thanks for listening!

Editor's Notes

  • #2 Hi, my name is Tomek Niedźwiedź. I work as a software engineer at Cognifide and I’m happy to be here together with you. What I’d like to share with you today is a number of observations about the modern AEM stack. Most notably, just how much easier it is now to develop for AEM than it used to be just a couple of years ago.
  • #3 AEM 6.0 released in May 2014 while on the 28th of May this year, we saw the release of AEM 6.1 Apart from exciting new features like the new touch UI or responsive layouts, to name a few, we’ve seen the introduction of new technologies relevant to us, AEM developers. Sightly Sling Models These tools attempt to solve long standing problems that we’ve always had to deal with in different ways. A long time ago, back in the days of CQ 5.4, Cognifide developed a library called Slice that allowed us to greatly improve the ease and speed of backend development in CQ. As the platform evolved, so did Slice. Thanks to continuous improvements, we’re still using it in our AEM projects and I’m going to explain what makes it relevant regardless of the recent changes in the AEM ecosystem. One of the questions we tend to get quite often is that of whether Slice can be used with Sightly. I’ll try to keep the number of code snippets as low as I can... And believe me, as a developer, I usually find it really tempting to use MANY
  • #4 FOCUS <> on what’s important/allowed in the view layer
  • #5 JSP is powerful... Perhaps too much so. Mention core components like parsys, commerce components, etc. /libs/commerce /libs/foundation I’ve seen this happen in many places JSP was use, on completely different stacks. Big and small companies alike. To the point where you could find five (or more) different languages in a single file. Can you guess which language this line is written in? Mind boggling and even impossible to handle by editor support in a reasonable manner. Not bad since JSP 2.0... As long as the developers maintain some discipline The language places a lot of power in the hands of a programmer
  • #6 And when you meddle with powers difficult to comprehend, who knows what cosmic terror you might awaken.
  • #7 Sightly doesn’t allow us to do much... And that’s pretty awesome when you think about it. It’s expressive power is rudimentary... But you don’t really need more in the presentation layer. I had my doubts about the possibilities taken from me when moving from JSP to Sightly. But eventually, I felt relieved rather than diminished. Write HTML... It’s a templating language, it’s supposed to output HTML. No DSLs, just elements.
  • #8 in simple cases, a Java/JS model is not even required.
  • #9 Iteration (and simple tests) are all we get when flow control statements are concerned.
  • #10 Data-sly-template – define a reusable, parametrized script, resembles JSP tag definitions Data-sly-call – call a Java/JavaScript backend, call a Sightly template Data-sly-include – Include another script, just like a JSP include Data-sly-resource – include a resource, goes via Sling, selectors are respected Data-sly-unwrap – no longer required in AEM 6.1 Everything achieved by means of attributes. A very non-invasive way to define the minimal logic necessary without obfuscating the markup
  • #11 Markup is finally visible, almost no clutter. One can concentrate on what’s important in the presentation layer. Semantics, document structure. Developers can deliver value in terms of accessibility and SEO instead of wasting time on visually debugging a script. Frontend developers do not shun it. With JSP, they tend to go „Meh, you do it”. The JavaScript use API comes in handy when a UI engineer wants to mock the backend. Virtually free of language-specific constructs (JSP tags, scriptlets anyone) A frontend developer with no backend/AEM experience can easily introduce changes. Very easy to learn. A developer can skim the documentation page and immediately dive into writing components. The only thing I really miss are tag libraries and their relative testability. It also feels a bit strange sometimes to repeat a bit of markup a given number of times.
  • #12 When we look at how AEM components are developed, there are usually two distinct kinds of code that we will see. The component will have a script placed somewhere in /apps/appname/components That script will be either a Sightly or a JSP file placed next to the component dialog. This script will, in turn, reference a bit of Java code responsible for providing the script with data. The separation between the business logic and the presentation layer is quite clear... Unless we write some really bad JSP. Additional WCM bindings can be provided by registering an OSGi service. Keep the number low or the code might be difficult to understand. Mention that Sling Models were inspired by Slice The Use interface, along with the WcmUse and WcmUsePojo classes are Sightly-specific. Designed for use with the data-sly-use attribute
  • #13 Not a new thing, we’ve been using it for 3 years. Dozens of projects completed. At the point when we open-sourced it, CQ 5.4 was around
  • #14 How do I write a component? There’s usually a bit of Sightly or JSP in /apps/thingamajig/components/core and you need a bit of code for it to interact with.
  • #15 Composition of models. Use of collection. Mapping of nested content structures.
  • #16 How is it different from what you get in Sling Models? It’s not... It’s conceptually identical. You get different annotations, marginally different syntax. You could spot some minute differences if you took a really close look at how various types are mapped between the JCR and the Java code... But we are pragmatic developers and we would rather get the job done than spend their time looking for wally/waldo. Both libraries are doing an excellent job here.
  • #17 You could spot some minute differences if you took a really close look at how various types are mapped between the JCR and the Java code... But we are pragmatic developers and we would rather get the job done than spend their time looking for wally/waldo. Both libraries are doing an excellent job here.
  • #18 Constructor injection, qualifiers
  • #19 It’s now easier than ever to read a bunch of properties from JCR in a clean, declarative manner. The functionality is readily available in OOTB libraries. If you have an additional library that can do just that, you gain nothing... Apart from being able to feel smug about how hip you were to use this several years ago.
  • #21 When doing introductory training for new developers at Cognifide, we get a lot of comments and questions about the resemblance between Slice and ORMs like Hibernate. That’s how you may see it when implementing a trivial component. But there’s something else that makes the library relevant despite the recent advancement of similar tools.
  • #22 Guice itself is immensely popular, used in many Google projects, widely adopted by the community. It is used by the Play framework, it’s been ported to Android.
  • #23 Guice itself is immensely popular, used in many Google projects, widely adopted by the community. It is used by the Play framework, it’s been ported to Android. In fact, the name of Slice i a portmanteau of Sling and Guice
  • #24 Guice itself is immensely popular, used in many Google projects, widely adopted by the community. It is used by the Play framework, it’s been ported to Android.
  • #25 Slice itself has been used with great success by our teams developing software for various clients as well as in internal projects and by our clients’ own development teams. Mention the other parties using Slice
  • #26 By default, Guice returns a new instance each time it supplies a value. This behaviour is configurable via scopes. Scopes allow you to reuse instances: for the lifetime of an application (@Singleton), a session (@SessionScoped), or a request (@RequestScoped). Guice includes a servlet extension that defines scopes for web apps. Custom scopes can be written for other types of applications. @ContextScoped – one object instantiated per request or thread (depending on how an injector is obtained)
  • #27 By default, Guice returns a new instance each time it supplies a value. This behaviour is configurable via scopes. Scopes allow you to reuse instances: for the lifetime of an application (@Singleton), a session (@SessionScoped), or a request (@RequestScoped). Guice includes a servlet extension that defines scopes for web apps. Custom scopes can be written for other types of applications. @ContextScoped – one object instantiated per request or thread (depending on how an injector is obtained)
  • #28 By default, Guice returns a new instance each time it supplies a value. This behaviour is configurable via scopes. Scopes allow you to reuse instances: for the lifetime of an application (@Singleton), a session (@SessionScoped), or a request (@RequestScoped). Guice includes a servlet extension that defines scopes for web apps. Custom scopes can be written for other types of applications. @ContextScoped – one object instantiated per request or thread (depending on how an injector is obtained)
  • #29 What these modules do is instruct Guice how to obtain and provide various objects provided by the APIs that AEM is based on. Resources, paths, services. ConfigurationPageModel Slice can be thought of as a set of modules that define bindings to common AEM and Sling APIs
  • #31 Why not OSGi dependency injection? Why give up the benefits it offers? Oh, we’re not giving them up.
  • #32 OSGi is applicable when you want to expose a bit of functionality to the whole OSGi environment. It’s actually a very good idea to expose parts of your application as services. Consumption of services should, however, be consistent.
  • #33 Sling Models is a remarkable library/piece of software
  • #34 Not a mature library – this is plainly wrong, it has been in continuous use for several years now, dozens of projects have been delivered successfully. It really is a battle-hardened lib, so to say It costs a lot to introduce – no it doesn’t, it’s completely free. I’ve seen people believe it was somehow tied to the Zen Garden. It simply isn’t. Zen Garden uses it but there’s no dependency the other way round. It is proprietary to Cognifide – Slice is an open source library, licensed under the Apache license, version 2.0. You can read the code, help work on it, fork it, change it, turn it into something of your own, come up with derived work, sell the derived work. Quite often, people don’t even know what it is... And they think they do. I’ve met people confusing Slice with Sightly (sic!) or even Sling itself.
  • #37 With Sightly, it’s easier than ever to quickly prepare a script rendering a component.
  • #40 How do I write a component? There’s usually a bit of Sightly or JSP in /apps/thingamajig/components/core
  • #41 How do I write a component? There’s usually a bit of Sightly or JSP in /apps/thingamajig/components/core
  • #42 I hope you found out something new. If by any chance you feel that Slice could help you in your next projects, be sure to grab me or one of the other Cognifide fellows during the breaks. We happen to have Maciej, the lead developer of the Slcie project among us today. Even if you strongly believe that Slice is simply not the right tool for you, giving some thought to the concepts described might just influence the way we work with Sling Models. If you found something about this presentation particularly interesting, thought-provoking, good, bad or plainly wrong, be sure to give me a shout via email or twitter. I’ll be more than pleased to have a chat in person during the conference.
  • #43 I hope you found out something new. If by any chance you feel that Slice could help you in your next projects, be sure to grab me or one of the other Cognifide fellows during the breaks. We happen to have Maciej, the lead developer of the Slcie project among us today. Even if you strongly believe that Slice is simply not the right tool for you, giving some thought to the concepts described might just influence the way we work with Sling Models. If you found something about this presentation particularly interesting, thought-provoking, good, bad or plainly wrong, be sure to give me a shout via email or twitter. I’ll be more than pleased to have a chat in person during the conference.