Using block value in multiple places

Crell
Member | 3
+
0
-

I'm trying to allow a page title to be set from a template, and then show in multiple places in the overall page. For example, in the <title> tags and in an <h1> in the page body. I'm unclear how to do that, however.

Here's what I'm doing now:

{* Some file somewhere *}
{layout 'layout.latte'}

{block title}Test Page{/block}

{block content}
// ...
{/block}
{* layout.latte *}
{default $title = 'Untitled'}
<html>
<head>
    <title>{$siteName}: {block title}{/block}</title>
</head>
<body>
    <h1>{$title}</h1>
    <main>
        {block content}{/block}
    </main>
</body>
</html>

The <title> gets filled in with the right value just fine. The <h1> obviously does not work, and gets just “Untitled”. But… I'm not sure how to make it work correctly, and use the same string in both places. I'm fine using a block or a variable or whatever, I just don't know what the best practice is here. Please advise.

MajklNajt
Member | 494
+
0
-

I am using this setup:

{* Some file somewhere *}
{layout 'layout.latte'}

{define title}Test Page{/define}

{block content}
// ...
{/block}
{* layout.latte *}
<html>
<head>
    <title>{$siteName}: {ifset title}{include title}{elseifset}Untitled{/ifset}</title>
</head>
<body>
    <h1>{ifset title}{include title}{elseifset}Untitled{/ifset}</h1>
    <main>
		{include content}
    </main>
</body>
</html>

EDIT: elseifset

Last edited by MajklNajt (2024-08-31 19:23)

Crell
Member | 3
+
0
-

Thanks! That kept giving me this compile error:

Fatal error: Uncaught Latte\CompileException: Unexpected end (in ‘…/templates/layout.latte’ on line 10 at column 63) in /app/vendor/latte/latte/src/Latte/Compiler/TagParser.php on line 254

However, it works with {else}, not {elseifset}. Are we perhaps running different versions? I'm on 3.0.18.