Skip to content

Please add an early exit feature for latte templates #287

@BernhardBaumrock

Description

@BernhardBaumrock
  • Explain your intentions

As explained in this forum question I'd need an early exit feature in latte templates: https://forum.nette.org/en/35172-early-exit-in-latte-template-file

This follows the guard clause instead of if/else-nesting, see https://www.youtube.com/watch?v=EumXak7TyQ0 why this can be good for reducing code complexity and making it easier to read and maintain.

  • It's up to you to make a strong case to convince the project's developers of the merits of this feature

I've built a page builder for the ProcessWire CMS that looks like this:

img

These content blocks consist of a controller file and a view file. The controller file is PHP (eg Gallery.php), the view file is LATTE (eg Gallery.latte).

A simple view file could look like this:

<h1>{$block->headline}</h1>
<div n:inner-foreach="$block->images() as $img">
   ...
</div>

Now what I'd like to do is to only render the view file if certain conditions are met. In this example it would be great to only render the gallery view file if the block has at least one uploaded image.

{returnif !$block->images()->count()}
<h1>{$block->headline}</h1>
<div n:inner-foreach="$block->images() as $img">
   ...
</div>

Without this feature I need to wrap my view markup in an if condition:

{if $block->images()->count()}
  <h1>{$block->headline}</h1>
  <div n:inner-foreach="$block->images() as $img">
     ...
  </div>
{/if}

Thx for considering :)

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