-
-
Notifications
You must be signed in to change notification settings - Fork 112
Description
In our project, we have a lot of reusable Latte components and sometimes we want these components to conditionally become a snippet. Currently, the best way to achieve this is something like this:
{default ?string $snippetName = null}
{define inner}
...
{/define}
{if $snippetName}
<div n:snippet="$snippetName"
n:tag="$tag"
n:attr="$nAttrs"
n:class="$className">
{include #inner}
</button>
{else}
<div n:tag="$tag"
n:attr="$nAttrs"
n:class="$className">
{include #inner}
</div>
{/if}Since you cannot use n:snippet="null", you have to define the inner part and then repeat all the attributes in the pattern above. To avoid duplication, this requires storing classes and attributes in variables, which leads to unclear and potentially confusing code since they are detached from the actual element. It would be much better if there was a possibility to use a pattern like this:
{default ?string $snippetName = null}
<div n:snippet="$snippetName"
n:tag="$tag"
n:attr="$nAttrs"
n:class="'class-name', $foo ? 'bar'">
...
</button>Not only is this shorter, but in my opinion it is also more readable (and potentially even n:attr could be removed or reduced in favor of native attributes). The n:snippet="null" would simply ignore the attribute and would not create any snippet. Are there any internal reasons why this cannot be done, or would this be considered a feature to be added?