Template Fragments in Latte
- n0nag0n
- Member | 8
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
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
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
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
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
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