n:ifcontent doesn't work if var is not set, and also PHP throws no notice

Raoul
Member | 7
+
0
-

Example:
<div n:ifcontent="$foo">Content</div>

The div is shown if $foo is not set and PHP doesn't raise a notice.

When I use
<div n:if="$foo">Content</div>

The PHP Notice is:

PHP Notice: Undefined variable: foo

I thought n:ifcontent works like when I use !empty() in an if condition.

David Grudl
Nette Core | 8199
+
+1
-

No, it omitts empty element https://latte.nette.org/en/tags#…

Keeehi
Member | 36
+
+1
-

No. n:ifContent doesn't check the content of a variable. In fact, you shoud not put anything behind it. It check, if there is a content between openging and closing html tag.

Raoul
Member | 7
+
0
-

Thanks all, so I just wrote two macros if you are interested:

$set->addMacro('notempty', 'if (!empty(%node.args)) {', '}');
$set->addMacro('empty', 'if (empty(%node.args)) {', '}');

I use them only in this way:
<div n:empty="$foo">Content</div>
<div n:notempty="$foo">Content</div>
so the div tag is or isn't shown

David Grudl
Nette Core | 8199
+
0
-

And it's not enough to use n:if="empty($foo)" ?

Instead od notempty, you can use n:ifset="$foo" (as if (isset($foo))

Raoul
Member | 7
+
0
-

And it's not enough to use n:if="empty($foo)" ?

Well, it's a little less to write

Instead od notempty, you can use n:ifset="$foo" (as if (isset($foo))

Not exactly as isset returns true also if value is 0 or '' or ‘ ’…