Skip to content

Conditional snippet with n:snippet? #406

@zipper

Description

@zipper

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions