javaliteactiveweb

activeweb ContentFor tag render nothing?


I have read content for and yield tag section of activeweb doc. and activeweb version

16:59 $ mvn dependency:tree | grep activeweb
[INFO] +- org.javalite:activeweb:jar:2.2:compile
[INFO] +- org.javalite:activeweb-testing:jar:2.2:test

The layout.ftl template code lists below:

{
"ext": [<@yield to="ext"/>],
"data": ${page_content}
}

and the view page:

<@content for="ext">
"hello", "world"
</@content>
["this is data"]

I run the controller and ext render nothing. Did I miss something?

BTW, how can I check the ContentForTag contain some content or not?, I want the layout.ftl look like

{
<#if ext?has_content?>
"ext": [<@yield to="ext"/>],
</#if>
"data": ${page_content}
}

Solution

  • you have a two part question:

    1. I created the @ContentFor tag and it is not rendering anything.
    2. How to render its content conditionally.

    To answer your first question, I can say not sure why it is not working for you. I created an identical example based on your code, and it is working as expected. Please, see the last commit on ActiveWeb Simple project content_for branch: https://github.com/javalite/activeweb-simple/tree/content_for You can run this project to see that your code is working.

    enter image description here

    TO answer your second question, you need to use a condition in the view, not in the layout. So, the view would look like this:

    <#if condition >
        <@content for="ext">
        "hello", "world"
        </@content>
        ["this is data"]
    </#if>