data-sly-call in sightly not invoking - aem

I was trying the sample example given at [a link]http://docs.adobe.com/docs/en/aem/6-0/develop/sightly/use-api-in-java.html. I have created the component SightlyTest in which the data-sly-call to the template does not work. Below are my files inside component:
extra.html
<template data-sly-template.extra="${# text}"
data-sly-use.extraHelper="${'ExtraHelper' # text=text}">
<p>${extraHelper.reversedText}</p>
</template>
ExtraHelper.java
package apps.AEMProject.components.content.SightlyTest;
import com.adobe.cq.sightly.WCMUse;
public class ExtraHelper extends WCMUse {
private String reversedText;
public String getReversedText() {
return reversedText;
}
#Override
public void activate() throws Exception {
String text = get("text", String.class);
reversedText = new StringBuilder(text).reverse().toString();
System.out.print("reversedText ::: "+reversedText);
}
}
SightlyOp.java
package apps.AEMProject.components.content.SightlyTest;
import com.adobe.cq.sightly.WCMUse;
public class SightlyOp extends WCMUse {
private String lowerCaseTitle;
private String lowerCaseDescription;
#Override
public void activate() throws Exception {
lowerCaseTitle = getProperties().get("title", "").toLowerCase();
lowerCaseDescription = getProperties().get("description", "").toLowerCase();
}
public String getLowerCaseTitle() {
return lowerCaseTitle;
}
public String getLowerCaseDescription() {
return lowerCaseDescription;
}
}
SightlyTest.html
<div data-sly-use.sg="SightlyOp"
data-sly-use.extra="extra.html">
<h1>${sg.lowerCaseTitle}</h1>
<p>${sg.lowerCaseDescription}</p>
<div data-sly-call="${extra # text=properties.description}"></div>
</div>
sg.lowerCaseTitle & sg.lowerCaseDescription is working fine, but nothing display for data-sly-call
Thanks

Try this in SightlyTest.html instead,
<div data-sly-use.sg="SightlyOp" data-sly-use.extra1="extra.html">
<h1>${sg.lowerCaseTitle}</h1>
<p>${sg.lowerCaseDescription}</p>
<div data-sly-call="${extra1.extra # text=properties.description}"></div>
</div>
Modified to data-sly-use.extra1 to differentiate between the variable and the template being called.

I realize I've come to the party a little late, but I'd like to expand on Aditya's answer.
Think of the file extra.html more like a "library" of data-sly-templates rather, since it could contain as many of them as you want (each with a different name). So when you "use" the extra.html file you're sort of importing those templates into a namespace you provide on the use statement. You can then call the templates using that "namespace".
<div data-sly-use.sg="SightlyOp"
data-sly-use.extra="extra.html">
<!--/*${extra} is now a namespace for the templates in extra.html. (you can name it whatever you like for more clarity*/-->
<h1>${sg.lowerCaseTitle}</h1>
<p>${sg.lowerCaseDescription}</p>
<!--/*since your template is called extra, and it's in the namespace called extra you call it with ${extra.extra}*/-->
<div data-sly-call="${extra.extra # text=properties.description}"></div>
</div>

Related

ZK: loading value/text into selectbox

I have a selectbox and want to load the value and text into the template, similar to an HTML dropdown box. I am using ZK framework with Java on the back end.
<selectbox id="buListbox" model="${$composer.buModel}" >
<template name="model">
<label value="${each}" />
</template>
</selectbox>
When using ZK, you don't need the value to identify the selected object like in HTML.
When using the MVC pattern, binding a model via model attribute, the selected item is also stored in that model and can be retrieved in java via model.getSelection().
Furthermore, a model is not restricted to lists of String, but it can hold any object type you want. In the template section, you can display any property of that object. Then the properties' toString() method is used to get the value which is displayed. This also applies to ${each} itself.
Example:
Assuming your model is a ListModelList of type ValueType:
public class ValueType {
private String value;
private String text;
public ValueType(String value, String text) {
this.value=value;
this.text=text;
}
public String getText() {
return this.text;
}
public String getValue() {
return this.value;
}
}
private ListModelList<ValueType> typesModel;
public ListModelList<ValueType> getTypesModel() {
return typesModel;
}
You than can use the selectbox's model/template to display it's text property:
<selectbox id="typesSelectbox" model="${$composer.typesModel}">
<template name="model">
${each.text}
</template>
</selectbox>
In java, you then get the selected item via typeModel.getSelection() .
Here you can find a working ZKFiddle example.

ZK MVVM : Passing parameter to another viewmodel using <include>

What I want to do is passing value from page1.zul to page2.zul through <include> and viewModel.
From page1.zul, I have
<include processId="#bind(vm.selectedProcess.id)" src="#load('page2.zul')"></include>
And then it should pass to a viewModel in page2.zul
#Init
public void init(#ExecutionArgParam("processId") String processId){
System.out.println("processInstanceId : " + processId);
}
However, I always get a null value. Any idea how to do this thing ? page2.zul looks something like this :
<div apply="org.zkoss.bind.BindComposer"
viewModel="#id('vm') #bind('com.mrye.viewModel')">
<label value="#load(vm.processId)"></label>
</div>
First of all, make distinguish names for Id's of the VM.
Then zul :
<div apply="org.zkoss.bind.BindComposer"
viewModel="#id('vm') #init('com.mrye.viewModel', processId = parentVM.selectedProcess.id )">
And VM2:
#Init
public void init (#BindingParam("processId") MyObject processID) {
Edit after the comment :
As you can see in this fiddle it works, but your parameter has to be initialized.
If you want to have "live data" passed to other zul (not other viewmodel), you can use #ref or just use parentVM.
If the live data need's to be in the viewmodel itself, you can use a non visible textbox where you load the data from parentVM and save it in the includedVM just before some action happens.
See updated fiddle here.
Here you can check
Index.zul
<?page title="URL Parameters Test" contentType="text/html;charset=UTF-8"?>
<zk>
<window title="URL Parameters Test" border="normal">
<include src="header.zul?test=5" />
</window>
</zk>
In above code you can check it pass argument with URL here test is argument name and value=5
<?page contentType="text/html;charset=UTF-8"?>
<zk>
<window border="none" width="100%" height="100%" apply="pkg$.HeaderComposer">
<label id="lblHeader" />
<div>
Load from EL [ <label value="${param.test}" />]
</div>
</window>
</zk>
In this page we used ${param.test} to get the parameter passed in index.zul , HeaderComposer.java
import org.zkoss.zk.ui.*;
import org.zkoss.zk.ui.event.*;
import org.zkoss.zk.ui.util.*;
import org.zkoss.zk.ui.ext.*;
import org.zkoss.zk.au.*;
import org.zkoss.zk.au.out.*;
import org.zkoss.zul.*;
public class HeaderComposer extends GenericForwardComposer{
Label lblHeader;
#Override
public void doAfterCompose(Component comp) throws Exception {
try {
super.doAfterCompose(comp);
}
catch (Exception e) {
e.printStackTrace();
}
/*
* retrieve url parameters
*/
String[] parameter = (String[]) param.get("test");
if (parameter != null)
lblHeader.setValue( "Congratulations! Your parameters value is " + parameter[0] );
else
lblHeader.setValue( "No parameters found. URL should be something like http://yourserver/yoursite/main.zul?parameter=param-value" );
}
}
for more way you can check http://zkframeworkhint.blogspot.in/2014/05/zk-include-how-to-pass-and-get.html
Just be sure on VM1 the value vm.selectedProcess.id is correctly initialized and it has a value, in the VM2 add the #AfterCompose lifecycle annotation then on #Init get the value, according to your code this should work on VM2:
Long processId;
#AfterCompose
public void initAfterCompose(#ContextParam(ContextType.VIEW) Component view) {
Selectors.wireComponents(view, this, false);
}
#Init
public void init() {
//get dynamic attribut
processId = (Long) Executions.getCurrent().getAttribute("processId");
}

how do I get key/value in sightly from java use class hashmap

I have a basic java use class object that extends WCMUSE and a simple hashmap method - in the sightly code - I have something like
${item}
${item.key}
${item.value}
does not work - how do I return key/value pair in sightly code
There is an example at Sightly Intro Part 3 and the use of ${item} and ${itemList} as a variables is documented on the AEM Docs Sightly Page. This page also gives the following example for accessing dynamic values:
<dl data-sly-list.child="${myObj}">
<dt>key: ${child}</dt>
<dd>value: ${myObj[child]}</dd>
</dl>
Here is an example with a simple HashMap.
HTML with Sightly:
<div data-sly-use.myClass="com.test.WcmUseSample" data-sly-unwrap>
<ul data-sly-list.keyName="${myClass.getMyHashMap}">
<li>KEY: ${keyName}, VALUE: ${myClass.getMyHashMap[keyName]}</li>
</ul>
</div>
Java:
package com.test;
import java.util.HashMap;
import java.util.Map;
import com.adobe.cq.sightly.WCMUse;
public class WcmUseSample extends WCMUse {
private Map<String, String> myHashMap;
public void activate() throws Exception {
myHashMap = new HashMap<String, String>();
for (int i = 0; i < 10; ++i) {
myHashMap.put(""+i, "Hello "+i);
}
}
public Map<String,String> getMyHashMap() {
return myHashMap;
}
}
You can also try the following (AEM 6.4) :
Note the data-sly-test.hashMap for emptycheck.
<div data-sly-use.pageController="com.corp.wcms.core.pages.anypage.PageController"
<div data-sly-test.hashMap="${pageController.myHashMap}" data-sly-unwrap>
<ul data-sly-list.keyName="${hashMap}">
<li>KEY: ${keyName}, VALUE: ${hashMap[keyName]}</li>
</ul>
</div>
</div>

Liferay : How to receive the parameters of renderURL through a Portlet class

I read that the renderURL will be responsible to execute the renderPhase only (That is the doView Method of the java class )
Now in one of the JSP i have a Hyper Link to navigate to the another page as shown
(This is the starting page of the Portlet)
<a href="<portlet:renderURL>
<portlet:param name="goto" value="IpByHourPage"/>
<portlet:param name="jspPage" value="/page2.jsp" />
</portlet:renderURL>">
Click here to go to Second Page
</a>
Now my question is that , is it possible taht instead of getting the parameters inside the page2.jsp and processing it , is it possible that to recieve these parameters inside the java file that is
I want to recieve this parameters inside the SecondPort as shown below .
For example
public class SecondPort extends MVCPortlet {
public void doView(RenderRequest renderRequest, RenderResponse renderResponse throws IOException, PortletException
{
// do something in this code here .
}
Yes, you can get your parameter set in <portlet:param> tag in your portlet class.
You can read that parameter in doView method by following :-
public class SecondPort extends MVCPortlet {
public void doView(RenderRequest renderRequest, RenderResponse renderResponse throws
IOException, PortletException
{
String goto = renderRequest.getParameter("goto");
String jspPage= renderRequest.getParameter("jspPage");
//Do something here....
}
like this..

Number format in ZKOSS label

is there any way to format number in ZK label component that looks like like
<label value="${each.value}" /> ? Values are doubles and I want to separate thousands etc... I know that doublebox for example has format property but what if I just want to display number as label? Thanks for any help.
Feature Request
First of all I have opened a feature request for this on ZK's tracking system you can find it here. Please follow this if you want updates.
Ways of implementing
There are in fact ways of implementing this depending on what pattern & techniques you are using.
MVC & EL
You can create an EL function which will do the formatting for you in your ZUL file. First of all create a class such as this:
public class FormatElNumber {
public static String formatStock(double stock) {
final NumberFormat nf = NumberFormat.getInstance();
nf.setMaximumFractionDigits(2);
return nf.format(stock);
}
}
This will output numbers with two decimal places. Secondly you need to add this to the top of your zul file:
<?xel-method prefix="c" name="formatStock" class="demo.grid.hierarchy.FormatElNumber"
signature="java.lang.String formatStock(double)"?>
Then when you have a label you can do as follows:
<label style="color:red;" value="${c:formatStock(each.averageHigh)}" />
More infomration on this technique is available here.
MVVM
The MVVM is actually easier to implement, you create what's called a Converter, for example (please note this class is untested, but you get the idea).
public class NumberFormatConverter implements Converter {
#Override
public Object coerceToBean(Object val, Component comp, BindContext ctx) {
return null;
}
#Override
public Object coerceToUi(Object val, Component comp, BindContext ctx) {
if(!(val instanceof Integer)) {
throw new IllegalArgumentException("The argument must be a number!");
}
final Object tmp = ctx.getConverterArg("length");
int length = 0;
if(tmp instanceof Integer) {
length = (Integer)tmp;
}
final NumberFormat nf = NumberFormat.getInstance();
nf.setMaximumFractionDigits(length);
return nf.format(val);
}
}
Then all you do in your zul file is specify you would like to use a converter on the value, for example:
<label value="#load(vm.message) #converter(vm.myConverter)"/>
For more information on this technique you can refer here.
From the docs: http://books.zkoss.org/wiki/ZUML_Reference/EL_Expressions/Core_Methods/formatNumber
<?taglib uri="http://www.zkoss.org/dsp/web/core" prefix="c"?>
<zk>
<label value="${c:formatNumber(2332315231, '$ ###,###,###.00')}" />
</zk>