Template Fragments in Latte

n0nag0n
Member | 8
+
0
-

Just a quick question. Just helping out another dev who's building out an article for htmx.org. He wrote this article and was looking to see if it's possible to only render a specific portion of a template, rather than the whole template?

https://htmx.org/…e-fragments/

I imagine this would be something like:

{extends 'layout.latte'}

{block content}
<p>Some content</p>
{/block}

{block footer}
<footer>Yay!</footer>
{/block}

Is there a way to only render the footer block (or if it needs to be an embed/import whatever) and leave the other parts of this all alone?

Rick Strafy
Nette Blogger | 81
+
0
-

Hi, of course, it's called snippets and often it can make React/Angular/Vue obsolete. There are the docs https://doc.nette.org/…ication/ajax. You can mark some part of the template like snippet, for instance {snippet leftPanel}content{/snippet}, and then it's like html element that can be redrawn from the presenter via $this->redrawControl('leftPanel') and nette will send only that part of the template via ajax call to the same endpoint where you stand.

Currently the best library for handling nette ajax calls is https://naja.js.org/.

m.brecher
Generous Backer | 872
+
0
-

Hi, there is also another possibility than snippets how to render only one specific block from the template file:

documentation: https://latte.nette.org/…-inheritance#…

example:

{include footer from 'main.latte'}
n0nag0n
Member | 8
+
0
-

Rick Strafy wrote:

Hi, of course, it's called snippets and often it can make React/Angular/Vue obsolete. There are the docs https://doc.nette.org/…ication/ajax. You can mark some part of the template like snippet, for instance {snippet leftPanel}content{/snippet}, and then it's like html element that can be redrawn from the presenter via $this->redrawControl('leftPanel') and nette will send only that part of the template via ajax call to the same endpoint where you stand.

Currently the best library for handling nette ajax calls is https://naja.js.org/.

Thank you for the reply! So is the only way to do that through Nette, or can that be done with only Latte? Is there a $html_snippet = $Latte_Engine->redrawControl('leftPanel'); type of method to only render that one small piece?

Last edited by n0nag0n (2022-08-31 19:35)

n0nag0n
Member | 8
+
0
-

m.brecher wrote:

Hi, there is also another possibility than snippets how to render only one specific block from the template file:

documentation: https://latte.nette.org/…-inheritance#…

example:

{include footer from 'main.latte'}

I see where you are going with this. I think that the article I pointed out was less of creating a new template file just to handle one snippet and more of something like: $template_html_piece = $Latte_Engine->renderAsString('path/to/file.latte', [ 'foo' => 'bar' ], 'footer'); or similar so you could only render a single piece of the template.

jiri.pudil
Nette Blogger | 1032
+
+2
-

Hi, Latte\Engine::render*() methods have an optional $block parameter that makes them render only the given block. See https://github.com/…e/issues/101 for more context

n0nag0n
Member | 8
+
0
-

Awesome I think that's what I'm after.....and I'm embarrassed I didn't see that 3rd parameter soon enough XD

materix
Backer | 83
+
0
-

Hi @n0nag0n,

I also find the htmx-project to be really exciting.

Were you able to make template-fragments work with Nette and Latte?

n0nag0n
Member | 8
+
0
-

@materix In my own project I did get it working with Latte just fine. That project uses Fat-Free instead of Nette, but Latte would work in either!