Professional Documents
Culture Documents
Sarcina Curs AML 2
Sarcina Curs AML 2
adaptTo() 2014 2
Introduction to Sling Models / Sightly
adaptTo() 2014 3
What is Sling Models
@Model(adaptables = Resource.class)
public class YourCustom {
...
}
adaptTo() 2014 4
What is Sling Models
The ‘old-days’….
@Component
@Service
@Properties({
@Property(name = "adaptables", value = {"Resource" }),
@Property(name = "adapters", value = {"YourCustom" })
})
public class MyAdapterFactory implements AdapterFactory{
public <AdapterType> AdapterType
getAdapter(final Object adaptable, Class<AdapterType> type){
return new MyClassAdapter(adaptable);
}
}
adaptTo() 2014 5
What is Sling Models
Injecting
@Model(adaptables = Resource.class)
public class YourCustom {
@Inject // we expected always an email-property
private String email;
@Model(adaptables = Resource.class)
public class YourCustom {
@Inject // OSGi Service
private Externalizer externalizer;
@PostConstruct
protected void init() {
// gets executed after the class is created
// set to protected, so it can be unit-tested
}
}
adaptTo() 2014 7
What is Sling Models
@Model(adaptables = Resource.class)
public class YourCustom {
adaptTo() 2014 8
Sling Models
Before After
adaptTo() 2014 9
What is Sling Models
Available in AEM6
Can be installed in CQ5.6.1
http://sling.apache.org/documentation/bundles
/models.html
Updates: http://bit.ly/sling-models-package
adaptTo() 2014 10
What is Sightly
<div data-sly-test=“${wcmmode.edit}“>
Show this in edit mode...
</div>
adaptTo() 2014 11
What is Sightly
adaptTo() 2014 12
What is Sightly
adaptTo() 2014 13
What is Sightly
adaptTo() 2014 14
What is Sightly
JSP Sightly
adaptTo() 2014 15
What is Sightly
adaptTo() 2014 16
How Sling Models and Sightly meet?
adaptTo() 2014 17
How Sightly and Sling Models meet?
${ myClass.fullName }
</div>
adaptTo() 2014 18
How Sightly and Sling Models meet?
@Model(adaptables = Resource.class)
public class HeaderComponent {
@Inject @Default(values=“Feike“)
public String firstName; // maps to property firstName
@PostConstruct
protected void init() {
fullname += firstName + “ Visser“;
}
adaptTo() 2014 19
How Sightly and Sling Models meet?
${ myClass.fullName }
</div>
adaptTo() 2014 20
How Sightly and Sling Models meet?
@Model(adaptables = SlingHttpServletRequest.class)
public class HeaderComponent {
@Inject
public Page param1; // maps to param1 parameter
@Inject
public String param2; // maps to param2 parameter
adaptTo() 2014 21
How Sightly and Sling Models meet?
@Model(adaptables = SlingHttpServletRequest.class)
public class HeaderComponent {
@Inject
public Page currentPage; // maps to currentPage binding
adaptTo() 2014 22
How Sightly and Sling Models meet?
<div data-sly-use.person=“project.Person“>
${person.firstName}
</div>
<div>
${properties.firstName}
</div>
adaptTo() 2014 23
Example: Lat-Long component
adaptTo() 2014 24
Lat-long component
adaptTo() 2014 25
Lat-long component
@Model(adaptables=Resource.class)
public class LatLongComponent {
@Inject @Named("address") @Default(values=DEFAULT)
protected String addressDescription;
@PostConstruct
protected void init() throws AddressException {
coordinates = geocode.geocode(addressDescription);
}
}
adaptTo() 2014 26
Lat-long component
adaptTo() 2014 27
Example: Absolute URL
adaptTo() 2014 28
Absolute URL
<meta
data-sly-use.externalizer="components.ExternalUrl"
property="og:url"
content="${externalizer.absoluteUrl @ context='uri'}.html" />
adaptTo() 2014 29
Absolute URL
@Model(adaptables = SlingHttpServletRequest.class)
public class ExternalUrl {
@Inject
private Externalizer externalizer;
adaptTo() 2014 31
This is not lazy enough
adaptTo() 2014 32
Absolute URL
@Model(adaptables = SlingHttpServletRequest.class)
public class ExternalUrl {
@Inject
private Externalizer externalizer;
adaptTo() 2014 33
Absolute URL
@Model(adaptables = SlingHttpServletRequest.class)
public class ExternalUrl {
@PostConstruct
protected void init() {
String relPath = currentPage.getPath();
if ( path != null) { // check if there is a parameter
relPath = path;
}
absoluteUrl = externalize(relPath);
}
<meta
data-sly-use.externalizer="${'components.ExternalUrl'
@ path=resourcePage.path}"
property="og:url"
content="${externalizer.absoluteUrl @ context='uri'}.html" />
adaptTo() 2014 35
Absolute URL (unit-testing?)
@Spy
private ExternalUrl externalUrl = new ExternalUrl(null);
@Before
public void setup() {
String path = "/content/adaptTo/example";
String extPath = "http://localhost:4502/adaptTo";
doReturn(extPath).when(externalUrl).externalize(path);
}
@Test
public void testWhenNoPathParameterIsSpecified() {
externalUrl.init();
Assert.assertNotNull(externalUrl.absoluteUrl);
}
adaptTo() 2014 36
Bindings, Bindings, Bindings
adaptTo() 2014 37
Bindings example
Page-oriented functionality
Reuse the Sightly bindings
Expose this via a custom bindings
Use the binding in your Sling model class
adaptTo() 2014 38
Bindings
Custom Page-class
@Model(adaptables=Page.class)
public class MyCustomPage {
adaptTo() 2014 39
Bindings
@Override
public void addBindings(Bindings bindings) {
if ( bindings.containsKey(WCMBindings.CURRENT_PAGE)) {
Page current = (Page) bindings.get(WCMBindings.CURRENT_PAGE);
bindings.put("customPage", current.adaptTo(MyCustomPage.class));
}
}
adaptTo() 2014 40
Bindings
adaptTo() 2014 41
Bindings
<div>
<h1>${customPage.title}</h1>
</div>
adaptTo() 2014 42
Bindings
@Model(adaptables=SlingHttpServletRequest.class)
public class ContentComponent {
@Inject
private MyCustomPage customPage;
}
adaptTo() 2014 43
data-sly-template example
adaptTo() 2014 44
data-sly-template example
adaptTo() 2014 45
data-sly-template
adaptTo() 2014 46
data-sly-template
adaptTo() 2014 47
(bonus) Sightly questions from the field
adaptTo() 2014 48
If-then-else?
Can we do an if-then-else?
<div data-sly-test.authorMode=“${wcmmode.edit || wcmmode.preview}“></div>
<div data-sly-test=“${!authorMode}“></div>
adaptTo() 2014 49
String-concat?
adaptTo() 2014 50
data-sly-unwrap, friend or enemy?
<sightly
data-sly-test.localVar=“${wcmmode.edit || wcmmode.preview}“
data-sly-unwrap/>
adaptTo() 2014 51
variables
<div data-sly-list=“${currentPage.listChildren}“>
${itemList.index}
${itemList.count}
${itemList.first}
${itemList.last}
${itemList.odd}
${itemList.even}
</div>
adaptTo() 2014 52
formatting
adaptTo() 2014 53
Questions?
adaptTo() 2014 54