I think there might be an issue with using the r($condition) and isNotEmpty(). I’m running the latest Kirby (2.5.8).
So if I wanted to build a variable if a field was filled in I might want to use something like the following.
<?php r($page->tel()->isNotEmpty(), $tel = 'tel: ' . $page->tel() ) ?>
It seems to execute if the condition is false (e.g. the field IS empty).
The following code works as expected.
<?php if($page->tel()->isNotEmpty()): $tel = 'tel: ' . $page->tel(); endif ?>
I’m wondering if someone else can check this on their end.
The return values are always executet. No matter what the condition is.
<?php
$r = r(false, $test = 'test');
// will dump test
var_dump($test);
// will dump null
var_dump($r);
?>
Edit: What @texnixe wrote thats not the intended use case.
I don’t think you are supposed to use this with variables like that. But this should work:
<?php
$tel = r($page->tel()->isNotEmpty(), 'tel: ' . $page->tel(), '' );
echo $tel;
?>
Instead, you can use the ternary operator:
$tel = $page->tel()->isNotEmpty() ? 'tel: ' . $page->tel() : '';
echo $tel;
Thank you both for your input.
The following does indeed work for my case.
$tel = r($page->tel()->isNotEmpty(), 'tel: ' . $page->tel());
Now, what would be the way to echo this only if it holds a value? This get’s me often. Would it be the following?
<?php e($tel, $tel) ?>
You can just do this:
<?php e($page->tel()->isNotEmpty(), 'tel: ' . $page->tel()) ?>
Right, but I"m building a variable for output later.
$output = ‘something’;
$output .= ‘something else’;
You can just echo the variable later, like in my statements above. Or do anything else with it. Or, you can check if it contains an empty string or a value and react on that.
<?= !empty($tel)? $tel.'something else': ''; ?>
@lukaskleinschmidt What do you mean by “it’s not the intended use case”? Assigning variables does not yield the expected result, no matter what you do?
This all works as expected:
$result = r(1==2, [1,2,3], [4,5,6] );
var_dump($result); // => array(3) { [0]=> int(4) [1]=> int(5) [2]=> int(6) }
$result = r(1==2, [1,2,3]);
var_dump($result); // ==> NULL
$result = r(1==2, 'hello');
var_dump($result); // ==> NULL
$result = r(!(1==2), 'hello');
var_dump($result); // 'hello'
$result = r(!(1==2), [1,2,3], [4,5,6] );
var_dump($result); // => array(3) { [0]=> int(1) [1]=> int(2) [2]=> int(3) }
I mean assigning a variable (result) inside the function is not the intended use case of r()
.
r(false, $result = 'my result');
// $result will always be 'my result'
Oh, alright, then we are on the same page.