如何覆盖FTL模板(Keycloak页面)的特定部分?

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

How to override specific portion of a FTL template (Keycloak pages)?

问题

抱歉,代码部分不需要翻译,以下是翻译好的内容:

免责声明:我是一个Keycloak新手!!!

我有一组Keycloak页面来管理用户操作(登录页面、忘记密码页面,...)。
每个页面都扩展了template.ftl,它是一种“基本模板”。
在Symfony Twig中开发时,几年前我记得我可以在base.html.twig中定义部分,然后在扩展父模板的子模板中覆盖它们。在这里,我真的不明白如何做到这一点!

举个例子:我想在template.ftl中有这样的代码

[某种部分指示器在这里]
<span>我是一个span</span>
[部分指示器结束]

并且在我的子模板中(假设是login.ftl),我想以这种方式覆盖它

[与上面相同的部分指示器在这里]
<p>我是一个p而不再是一个span</p>
[部分指示器结束]

我找到了这个<#nested sectionName>的东西,但它的表现不如我所期望,也就是说,我尝试做类似这样的事情

<#nested mySection>
我是一个span
</#nested>

并尝试从其他页面覆盖它,但它给了我500错误。

我真的很讨厌Keycloak,所以请帮助我少讨厌一点 如何覆盖FTL模板(Keycloak页面)的特定部分?

英文:

DISCLAIMER: KEYCLOAK NEWBIE HERE !!!<br/>

I have a set of Keycloak pages that manage the user actions (login page, forgot password page, ...).<br/>
Every page extends this template.ftl that it's a sort of "base template".<br/>
When I used to develop in Symfony Twig, some years ago I remember that I could define sections in my base.html.twig and then override them in a child template that extended the parent one. Here I really can't understand how to do it!<br/><br/>

As an example: I want to have this kind of code in template.ftl

[some sort of section indicator here]
&lt;span&gt;I am a span&lt;/span&gt;
[end of section indicator]

And in my child template (let's say login.ftl) I want to override it in this way

[same section indicator of above here]
&lt;p&gt;I am a p and not more a span&lt;/p&gt;
[end of section indicator]

I found this &lt;#nested sectionName&gt; thing but it is not acting as I expected, meaning that I tried to do something like this

&lt;#nested mySection&gt;
I am a span
&lt;/#nested&gt;

and trying to override from the other page but it gave me 500 error.

I'm really hating Keycloak, so please help me to hate it a little less 如何覆盖FTL模板(Keycloak页面)的特定部分?

答案1

得分: 1

FreeMarker 不支持这个“slot”功能。相反,您可以将常见的 HTML 放入宏中。就像在layout.ftlh中这样(它是ftlh以启用HTML自动转义):

<#macro page title>
  <html>
     <head>
       <title>${title}</title>
     </head>
     <body>
       <h1>${title}</h1>
       <#nested>
     </body>
  <html>
</#macro>

然后在您的实际页面模板中,您可以这样做:

<#import "layout.ftl" as layout>
<@layout.page title="Hello World!">
  <p>Blah</p>
  <p>Blah</p>
</@layout.page>

在宏调用的layout.page的嵌套内容(两个<p>标签)在宏定义中的<#nested>达到时执行。

因此,这更加冗长,但完全明确/可追踪(没有从外部“神奇地”添加的内容)。

这里的一个大问题是您只能在宏调用中有一个“嵌套”内容。那么,如果您需要指定“主”内容(上面的两个<p>标签)和页脚内容怎么办呢?通常,将页脚内容作为宏参数添加,类似于标题。但与标题不同,您会传入另一个生成页脚的宏。不过...我不确定您是否需要这么深入,所以暂时不详细说明。

此外,我对Keycloak一无所知,也不确定它是否以某种方式破坏了上述内容。例如,是否可以使用#import导入常见宏等等。

英文:

FreeMarker doesn't have this slot thing. Instead, you put common HTML into macros. Like in layout.ftlh you got this (it's ftlh to enable HTML auto-escaping):

&lt;#macro page title&gt;
  &lt;html&gt;
     &lt;head&gt;
       &lt;title&gt;${title}&lt;/title&gt;
     &lt;/head&gt;
     &lt;body&gt;
       &lt;h1&gt;${title}&lt;/h1&gt;
       &lt;#nested&gt;
     &lt;/body&gt;
  &lt;html&gt;
&lt;/#macro&gt;

and then in your actual page template you do this:

&lt;#import &quot;layout.ftl&quot; as layout&gt;
&lt;@layout.page title=&quot;Hello World!&quot;&gt;
  &lt;p&gt;Blah&lt;/p&gt;
  &lt;p&gt;Blah&lt;/p&gt;
&lt;/@layout.page &gt;

where the nested content of the layout.page call (the two p-s) is what's executed when &lt;#nested&gt; is reached in the macro definiton.

So it's more verbose, but totally explicit/traceable (nothing is "magically" added from outside).

One big pain point here is that you can only have one "nested" content per macro call. So what if you need to specify the "main" content (the two p-s above), and also a footer content. Then, usually, add the footer content as macro parameter, similarly to the title. But unlike the title, there you would pass in another macro, that generates the footer. Anyway... not sure if you need to go that far, so I don't detail that further for now.

Also, I know nothing about Keycloak, and I'm not sure if it sabotages the above in any way. Like if you can have #import-s for the common macros, etc.

huangapple
  • 本文由 发表于 2023年7月18日 16:30:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76710863.html
匿名

发表评论

匿名网友

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

确定