Search code examples
thymeleafbroadleaf-commerce

Thymeleaf th tag in blc tag


my problem is following: in this code

    <header>
        <h1 th:utext="${BLC_PAGE.pageFields[menu_name]}"></h1>
    </header>

    <blc:menu resultVar="menuItems" menuName="${BLC_PAGE.pageFields[menu_name]}" />
    <ul th:if="${not #lists.isEmpty(menuItems)}">
        <li th:each="menuItem : ${menuItems}">

            <a th:href="@{${menuItem.url}}" th:class="${menuItemStat.first}? 'home'">
                <span th:utext="${menuItem.label}"></span>
            </a>

        </li>
    </ul>

the line <h1 th:utext="${BLC_PAGE.pageFields[menu_name]}"></h1> gets the name of actual menu. I need the menuName in this line <blc:menu resultVar="menuItems" menuName="${BLC_PAGE.pageFields[menu_name]}" /> to do the same thing, but "${BLC_PAGE.pageFields[menu_name]}" is not appropriate, as it is from thymeleaf tags library. Any idea how to get this this value in blc:menu tag?


Solution

  • In order to resolve the Thymeleaf expression ${BLC_PAGE.pageFields[menu_name]}, you will need to wrap it like so: th:attr="menuName=${BLC_PAGE.pageFields[menu_name]}". Because menuName is not a standard HTML attribute, you won't be able to simply add the Thymeleaf namespace to menuName (i.e., th:menuName), and, without the th: namespace, Thymeleaf doesn't know that it needs to resolve any expressions. The Thymeleaf attr attribute allows you to use non-standard HTML attributes with Thymeleaf. For more information on setting attribute values using Thymeleaf you can review their documentation.

    Here are links to the Broadleaf and Thymeleaf documentation on Processors and Dialects: