3

I need to generate several <liferay-ui:panel>s. The idea would be to have a JSP that looks something like this:

<liferay-ui:panel-container extended="true">
    <%=MyJavaClass.generatePanel() %>
</liferay-ui:panel-container>

and Java code along the lines of:

class MyJavaClass {
    public static String generatePanel() {
        String result="<liferay-ui:panel collapsible=\"false\" extended=\"true\" title=\"Some Title\">Some Content</liferay-ui:panel>";
        return result;
    }
}

Liferay won't convert the <liferay-ui panel...>. I'm guessing this is because it only does so before the java code is executed, so I'm not getting any panels.

Is there any way to get Liferay to go through the JSP after I generated the panels? Or am I missing a better way to do this?

icke
  • 1,568
  • 1
  • 19
  • 31

1 Answers1

3

Move the line <liferay-ui:panel collapsible=\"false\" extended=\"true\" title=\"Some Title\">Some Content</liferay-ui:panel>" to a jsp tag located at WEB-INF/tags/liferay-panel.tag.

And include the tag in the required jsp.

Below is the solution for the same:

liferay-panel.tag

<liferay-ui:panel collapsible=\"false\" extended=\"true\" title=\"Some Title\">
    Some Content
</liferay-ui:panel>

And include the tag like this:

<%@taglib prefix="tags" tagdir="/WEB-INF/tags"% >
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<liferay-ui:panel-container extended="true">
     <c:forEach var="i" begin="1" end="20" step="1">
        <tags:liferay-panel/>
     </c:forEach>
</liferay-ui:panel-container>
Ramesh PVK
  • 15,200
  • 2
  • 46
  • 50
  • How would I use that fragment? With ` – icke Jun 20 '12 at 08:46
  • I have given the code, please check and let me know if you have any doubts. – Ramesh PVK Jun 20 '12 at 08:48
  • I think my code example was poorly chosen. The problem is that I don't know how many panels I'm going to need at compile time (and that the content of each panel must be generated as well). – icke Jun 20 '12 at 08:51
  • Edited my answer can you check now. – Ramesh PVK Jun 20 '12 at 09:00
  • I tried, but can not get `<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>` to work. I googled and found some changes in recent Liferay versions. How would I go about including that TLD in Liferay 6.1? – icke Jun 20 '12 at 09:26
  • Look here http://stackoverflow.com/questions/9976281/the-absolute-uri-http-java-sun-com-jstl-core-cannot-be-resolved-in-either-web/9976952#9976952. Add the jstl jar in WEB-INF/lib. – Ramesh PVK Jun 20 '12 at 09:30
  • Your example works. Can I somehow access my variables in that tag file? – icke Jun 20 '12 at 09:51
  • Figured it out. Using `<%@ attribute name="myVariable" required="true" type="javax.servlet.ServletContext" %>` in the tag works just fine. Thank you for your help! – icke Jun 20 '12 at 10:10
  • You can access variables in pagecontext, session, request. On top of that if you want to something else, you should use tag attributes. A Tag file can take attributes. – Ramesh PVK Jun 20 '12 at 10:11