statamic / ideas

💡Discussions on ideas and feature requests for Statamic
https://statamic.dev
30 stars 1 forks source link

Continue and break control structure support in Antlers #1128

Open godismyjudge95 opened 4 months ago

godismyjudge95 commented 4 months ago

Would be awesome if Antlers supported the continue and break control structures: https://www.php.net/manual/en/control-structures.break.php https://www.php.net/manual/en/control-structures.continue.php

Currently if you have a loop and you want to skip an item:

{{ posts }}
    {{ if id !== 1 }}
        {{ title }}
    {{ /if }}
{{ /posts }}

But if we had continue support (I think this would work? Not sure about the ?= syntax):

{{ posts }}
    {{ id === 1 ?= {continue} }}
    {{ title }}
{{ /posts }}

Or another scenario where this would be cleaner:

{{ bard_content }}
    {{ type === 'hr' ?= {break} }}
    <!-- Content as an excerpt (before the hr) -->
{{ /bard_content }}
godismyjudge95 commented 4 months ago

Probably isn't helpful one bit but here is how Laravel Blade does it - https://github.com/laravel/framework/blob/10.x/src/Illuminate/View/Compilers/Concerns/CompilesLoops.php#L126-L152

daun commented 4 months ago

This would be super useful. Some prior art: in Latte templates these are called continueIf and breakIf:

{foreach $rows as $row}
  {continueIf $row->date < $now}
  {breakIf $row->parent === null}
  ...
{/foreach}