emmett-framework / renoir

A templating engine designed with simplicity in mind
BSD 3-Clause "New" or "Revised" License
34 stars 3 forks source link

Template inheritance - example from doc #7

Open areqq opened 4 months ago

areqq commented 4 months ago

I'm getting to know the project, it looks interesting. I checked one of the examples from the documentation and it didn't work as I expected. the word "Index" was not included in the title. Something is wrong or I don't understand the idea.

https://github.com/emmett-framework/renoir/blob/master/docs/quickstart.md Template inheritance output:

<html lang="en">
<head>
    <link rel="stylesheet" href="style.css" />
    <title> - My Webpage</title>
    <style type="text/css">
        .title { color: #336699; }
    </style>
</head>
<body>
    <div id="content">

Index

<h1>Index</h1>
<p class="title">
    Welcome to my awesome homepage.
</p>

    </div>
    <div id="footer">
        Copyright 2020 by you.
    </div>
</body>
</html>
gi0baro commented 4 months ago

@areqq interesting. Probably something changed in years due to code refactors. I'm gonna check this in the following days, the blocks code might be broken in certain scenarios, or the documentation just need updates; I'm sorry for the bother.

In the meantime, you can use variables instead (this is what I usually do for things like page titles):

layout.html:

{{ common_title = globals().get("common_title", "My Webpage") }}
{{ page_title = globals().get("page_title") }}
{{ title = f"{page_title} - {common_title}" if page_title else common_title }}
<!DOCTYPE html>
<html lang="en">
<head>
    {{ block head }}
    <link rel="stylesheet" href="style.css" />
    <title>{{ =title }}</title>
    {{ end }}
</head>
<body>
    <div id="content">
        {{ block main }}
        {{ include }}
        {{ end }}
    </div>
    <div id="footer">
        {{ block footer }}
        Copyright 2020 by you.
        {{ end }}
    </div>
</body>
</html>

index.html:

{{ page_title = "Index" }}
{{ extend "layout.html" }}

{{ block head }}
    {{ super }}
    <style type="text/css">
        .title { color: #336699; }
    </style>
{{ end }}

<h1>Index</h1>
<p class="title">
    Welcome to my awesome homepage.
</p>