Latte version 2.6.0 released

David Grudl
Nette Core | 8218
+
+10
-

Latte v2.6.0 has just been released!

  • added custom functions via $latte->addFunction()
  • Added optional chaining $var?->prop?->elem[1]?->call()?->item
  • Short ternary operator now requires braces around array (for future optional chaining) (BC break)
  • PhpWriter: short ternary checks if value exists
  • CachingIterator: fixed IteratorAggregate → Iterator conversion
  • BlockMacros: check when dynamic snippet is combined with id attribute [Closes nette/application#242]
  • BlockMacros: added $snippetAttribute for custom HTML attribute with snippet id [Closes nette/application#242]
  • removed latte.php

For the details you can have a look at the GitHub.

Optional chaining

{if $blogPost?->count('*')}  // means if (isset($blogPost) && $blogPost->count('*'))
	...
{/if}

{$order->item?->name}  // means isset($order->item) ? $order->item->name : null

Custom functions

$latte->addFunction('sum', function(...$nums) {
	return array_sum($nums);
});
{if sum($a, $b) > 10}
	...
{/if}

{sum(1, 5, 9)}

Another example:

$latte->addFunction('isLinkCurrent', [$presenter, 'isLinkCurrent']);
<a n:class="isLinkCurrent() ? active" n:href="Product:show">...</a>

instead of

<a n:class="$presenter->isLinkCurrent() ? active" n:href="Product:show">...</a>

This custom function will be automatically available in the next release of nette/application.

Martk
Member | 661
+
0
-

Is there any way to achive something like

$latte->addFunction('isLinkCurrent', function (Template $template, ...$args): bool {
	return $template->global->uiControl->isLinkCurrent(...$args);
});

?

SamuelThorn
Member | 29
+
+1
-

Hi there. I have come across this error message yesterday and I was a little bit confused by it. It said:

Short ternary operator now requires braces around array (for future optional chaining) (BC break)

The code that caused the error was:

<span n:attr="$variable ? ['key1' => 'val1', 'key2' => 'val2']">{$text}</span>

It took me a while to figure out that braces “{}” suggested in the message were in fact parentheses “()”.

<span n:attr="$variable ? (['key1' => 'val1', 'key2' => 'val2'])">{$text}</span>

I am not sure, if this was an error in the message or my fault, but either way I have decided to post it here, so it might help somebody dealing with the same problem.

David Grudl
Nette Core | 8218
+
+6
-

@SamuelThorn good point, I'll fix it

SamuelThorn
Member | 29
+
0
-

David Grudl wrote:

@SamuelThorn good point, I'll fix it

Thanks.