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

606 Views Asked by At

DISCLAIMER: KEYCLOAK NEWBIE HERE !!!

I have a set of Keycloak pages that manage the user actions (login page, forgot password page, ...).
Every page extends this template.ftl that it's a sort of "base template".
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!

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

[some sort of section indicator here]
<span>I am a span</span>
[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]
<p>I am a p and not more a span</p>
[end of section indicator]

I found this <#nested sectionName> thing but it is not acting as I expected, meaning that I tried to do something like this

<#nested mySection>
I am a span
</#nested>

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 :)

1

There are 1 best solutions below

0
ddekany On

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):

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

and then in your actual page template you do this:

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

where the nested content of the layout.page call (the two p-s) is what's executed when <#nested> 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.