Search code examples
javajspliferayliferay-6

Generating liferay-ui elements in a JSP


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?


Solution

  • 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>