martenframework / marten

The pragmatic web framework.
https://martenframework.com
MIT License
396 stars 21 forks source link

Add support for the `contextual` and `isolated` modifiers to `include` template tags #199

Closed ellmetha closed 3 months ago

ellmetha commented 4 months ago

Description

Let's make it possible to include template snippets/partials by explicitly stating whether the current template context should be made available to the included template or not through the use of new contextual and isolated tag modifiers.

For example:

{# Include my_snippet.html with the current context #}
{% include "my_snippet.html" with foo="bar", test="xyz" contextual %}

{# Include my_snippet.html without the current context #}
{% include "my_snippet.html" with foo="bar", test="xyz" isolated %}

Additionally, a new templates.isolated_included_templates setting should also be introduced in order to let developers choose whether or not included templates receive the current context by default (the default value of this setting should be false in order to ensure we don't introduce a backward-incompatible behavior).

notramo commented 4 months ago

This is required for clean code, and I like the proposed solution.

In order to encourage clean code in Marten projects by default, I recommend to set the default to true in a future version (1-2 releases later).