Open JacobPrice opened 7 months ago
@Levdbas Is this worth making a PR for? It could be a straightforward implementation that could allow for more code organization.
Hey @JacobPrice ,
I think this would be a welcome addition but it does needs it's own documentation and tests. Are you willing to provide those as well? We can help out with the tests if that holds you back. Documentation wise I would like to add a guide on how to use this feature in the wild with a platform/framework of choice so it can benefit the whole Timber community. Let me know if you have any questions!
I would be interested in trying this out and helping to document this feature. With HTMX, I've experimented with https://github.com/svandragt/htmxpress earlier and have used it on Django side with https://github.com/carltongibson/django-template-partials.
@Levdbas @jasalt I will work on this when I get a chance
Coming from the Wordpress side of things, I think this would be better named render_partial
or render_fragment
since I would expect render_block
to have something to do with the block editor.
With Twig templating it's hard to avoid some confusion with it's "block" concept which is used by plenthora of other templating engines also. Maybe docs could make the difference with it and CMS side block concept more clear.
Seems that django-template-partials separates block and partial concepts however, not sure if that could be applicable to follow here.
While this is in progress, I did some testing with Timber and HTMX using API for HTMX plugin https://github.com/timber/timber/discussions/3044.
Is your feature request related to a problem? Please describe.
Currently, Timber's render method is designed to render entire templates. While this works well for most use cases, there are scenarios, especially in dynamic and hypermedia-driven applications (e.g., those using HTMX), where rendering only a specific part of a template (a block or fragment) would be more efficient and effective. This is particularly relevant when only a small portion of the page needs to be updated in response to an action, avoiding the overhead of rendering and sending the entire template over the network.
Describe the solution you’d like
I propose adding a new method,
render_block
. This method would allow developers to render specific blocks within a Twig template, without needing to render the entire template.A high-level overview of this would look something like this:
An additional argument
$via_block_render
could be added toTimber::compile()
:Lastly, a conditional would be added to the output within the compile method:
To make this accessible via the Timber class an additional method could be added called render_block:
Describe alternatives you’ve considered
An alternative is to use twig partials and include them. This allows for those specific areas to be rendered as needed, but also leads to a slightly more fragmented development experience making it harder to understand the template as a cohesive unit.
Additional context
The above is a working prototype, but I'm sure it could be improved. I'd be more than happy to take some feedback and submit a PR if this is something Timber would benefit from.
Potentially missing things: Implement Timbers standard practice for handling errors/exceptions Maybe some filters are needed? Maybe this should be in a separate compilation?
https://twig.symfony.com/doc/3.x/api.html#rendering-templates https://htmx.org/essays/template-fragments/