在基础模板中检查子模板中是否设置了Freemarker变量。

huangapple go评论79阅读模式
英文:

Check for freemarker variable set in child in the base template

问题

I am using freemarker to render my frontend.

This is an extract of my base.ftlh that I use in my pages as a layout. A lot of pages have a header in a similar style therefore they can set the banner icon and the texts for themselves. However, some pages should not have this header, and I want to define a variable like just a boolean header to only render this part if it is set to true, which should be the default.

<#-- Application Main Content - Start -->
<div class="content-grid">
    <#if header!true>
        <div class="section-banner">
            <#if title_banner??>
                <img class="section-banner-icon" src="<@title_banner />" alt="badges-icon">
            </#if>
            <#if title??>
                <p class="section-banner-title"><@title /></p>
            </#if>
            <#if subtitle??>
                <p class="section-banner-text"><@subtitle /></p>
            </#if>
        </div>
    </#if>
    <#if page_body??>
        <@page_body/>
    </#if>
</div>

This is the top of one of my pages profile.ftlh where I want to use the base and tell it to not use the header:

<#include  "/base.ftlh">

<#macro page_title>
    Profil-Suche
</#macro>

<#assign header = false>

<#macro page_body>
    ...

However, this does not work, and my header variable I set in my profile.ftlh is completely ignored... How can I achieve this? I'd rather not put the header manually in every page that needs it; I'd prefer it to stay in the base.

英文:

I am using freemarker to render my frontend.

This is an extract of my base.ftlh that I use in my pages as a layout. A lot of pages have a header in a similar style therefore they can set the banner icon and the texts for themselves. However, some pages should not have this header, and I want to define a variable like just a boolean header to only render this part if it is set to true, which should be the default.

<#-- Application Main Content - Start -->
<div class="content-grid">
    <#if header!true>
        <div class="section-banner">
            <#if title_banner??>
                <img class="section-banner-icon" src="<@title_banner />" alt="badges-icon">
            </#if>
            <#if title??>
                <p class="section-banner-title"><@title /></p>
            </#if>
            <#if subtitle??>
                <p class="section-banner-text"><@subtitle /></p>
            </#if>
        </div>
    </#if>
    <#if page_body??>
        <@page_body/>
    </#if>
</div>

This is the top of one of my pages profile.ftlh where I want to use the base and tell it to not use the header:

<#include  "/base.ftlh">

<#macro page_title>
    Profil-Suche
</#macro>

<#assign header = false>

<#macro page_body>
    ...

However, this does not work and my header variable I set in my profile.ftlh is completely ignored... How can I achieve this? I'd rather not put the header manually in every page that needs it, I'd prefer it to stay in the base.

答案1

得分: 2

问题在于您在设置header变量之前包含了base.ftl。相反,将#include放在最后执行。

请注意,与许多其他编程语言一样,子程序(使用#function/#macro定义)可以在定义语句之前调用,这就是为什么那种方式有效的原因。但赋值操作无法执行此类技巧。因此,最好在包含base.ftl之前完成所有操作。

英文:

The problem is that you are including base.ftl before setting the header variable. Instead, do that #include as the very last thing.

Note that like in many languages, subroutines (defined with #function/#macro) can be called before reaching the defining statements, so that's why that worked. But assignments can't do such a trick. So it's just cleaner if you do everything before including base.ftl.

huangapple
  • 本文由 发表于 2023年6月6日 05:05:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76410003.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定