Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions docs/1-essentials/02-views.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,18 @@ The `:isset` directive can be used to conditionally render the element it is att
<h1 :isset="$title">{{ $title }}</h1>
```

The `:isset` directive will also detect when you have multiple cases, and will wrap each variable with `isset()` for you. Consider this example:

```html
<h1 :isset="$foo || $bar">Welcome!</h1>
```

If either `isset($foo)` or `isset($bar)` returns `true`, then the condition is met, and the element will be conditionally rendered.

You can also use `!isset($foo)` for inverse if needed.

Note: Ensuring that the expression returns `true` or `false` and thus applies the condition correctly is left down to you, the directive will simply wrap each `$var` with `isset()` preserving operators, without performing any logic checks itself. If you make an incompatible string, it will throw an Exception which you'll be able to view in the Debug log or interface, when enabled.

Since `:isset` is a shorthand for `:if="isset()"`, it can be combined with `:elseif` and `:else`:

```html
Expand Down
2 changes: 1 addition & 1 deletion packages/view/src/Elements/PhpIfElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function setElse(Element $element): self
public function compile(): string
{
if ($condition = $this->wrappingElement->consumeAttribute(':isset')) {
$condition = sprintf('isset(%s)', $condition);
$condition = preg_replace('/\$\w+/', 'isset($0)', $condition);
} else {
$condition = $this->wrappingElement->consumeAttribute(':if');
}
Expand Down
38 changes: 38 additions & 0 deletions tests/Integration/View/TempestViewRendererTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,44 @@ public function test_isset_attribute(): void
);
}

public function test_isset_attribute_dual_cases(): void
{
$this->assertSame(
'',
$this->view->render(view('<div :isset="$foo || $bar">Hello</div>')),
);

$this->assertSame(
'<div>else</div>',
$this->view->render(view('<div :isset="$foo || $bar">Hello</div><div :else>else</div>')),
);

$this->assertSame(
'<div>elseif</div>',
$this->view->render(view('<div :isset="$foo || $bar">Hello</div><div :elseif="true">elseif</div><div :else>else</div>')),
);

$this->assertSame(
'<div>else</div>',
$this->view->render(view('<div :isset="$foo || $bar">Hello</div><div :elseif="false">elseif</div><div :else>else</div>')),
);

$this->assertSame(
'<div>Hello</div>',
$this->view->render(view('<div :isset="$foo || $bar">Hello</div>', foo: true, bar: false)),
);

$this->assertSame(
'<div>Hello</div>',
$this->view->render(view('<div :isset="$foo || $bar">Hello</div>', foo: false, bar: true)),
);

$this->assertSame(
'<div>Hello</div>',
$this->view->render(view('<div :isset="$foo || $bar">Hello</div>', foo: true, bar: true)),
);
}

public function test_if_with_other_expression_attributes(): void
{
$html = $this->view->render('<div :if="$this->show" :data="$data">Hello</div>', show: true, data: 'test');
Expand Down