Rythm Template Inheritance

130 Views Asked by At

We have a common header/footer template as parent template which we will reuse for 100 of sub templates. Extends directive is not supporting this...

When i go over the Rythm documentation, i found a way to achieve this by include/invoke directives but the primary purpose of include/invoke directive is to invoke common function. Extends directive is supporting in a reverse way by putting main template content with a render directive as a parent and header/footer template as a subtemplate but the realtime usecase is totally different

Is that my understanding right? Is there a way to solve my problem?

Edited:

I have coded like below to achieve it:

footer.html

@def header1() {
    <h3>This is footer1 section</h3>
}

@def header2() {
    <h3>This is footer2 section</h3>
}

template1.html

@include("footer.html")
@args String who
<html>
    <head>
        <title>Hello world from Rythm</title>
    </head>
    <body>
        <h1>Hello @who</h1>
        @if(footer.equals("footer1){
            @header1();
        } else {
            @header2();
        }
    </body>
</html>

What i have done is with the help of include/invoke method invocation i have got the result but when i use extends it doesn't work. If it is possible can u solve my case using extends?

1

There are 1 best solutions below

0
Gelin Luo On

To use @extends to achieve the same effect, you should have:

layout.html

<html>
    <head>
        <title>Hello world from Rythm</title>
    </head>
    <body>
        @render()
    </body>
</html>

header1.html

<h3>This is footer1 section</h3>

header2.html

<h3>This is footer2 section</h3>

template.html

@extends(layout)
@args String who, String footer

<h1>Hello @who</h1>
@if(footer.equals("footer1")){
    @header1();
} else {
    @header2();
}