can I use a block inside an html tag?

ando
Member | 5
+
0
-

How can I achieve something like this?
I want to give my div a dynamic data attribute

<div {block attribute}{/block}>
{block attribute}data-my-attribute{/block}

But it gives me an error :/

m.brecher
Generous Backer | 905
+
+1
-

@ando

Block inside html attribute isnt good way how dynamically add html attributes. The simplest way is use variable:

$this->template->myAttribute = 'data-my-attribute';
<div {$myAttribute}>

In latte is also something like n:attr, but direct rendering variable makes the code cleaner and more modern.

2bfree
Member | 249
+
0
-

@mbrecher

Thanks much for this suggestion. For my case, where I combine template and “layout” overwrite I need to modify it a little bit.

{php $this->global->myAttribute = 'data-my-attribute="true"'}
<div {=$this->global->myAttribute ?? null|noescape}>

But it is also possible to do it more secure and flexible way:

{php $this->global->myAttribute['data-my-attribute'] = 'true'}
<div n:attr="$this->global->myAttribute">

Last edited by 2bfree (2025-09-15 19:38)