Skip to content

Commit

Permalink
Some docs/more test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnathonKoster committed Feb 8, 2025
1 parent 5953f00 commit 713a767
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 0 deletions.
48 changes: 48 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ The main visual difference when working with Dagger components is the use of the
- [Dynamic Components](#dynamic-components)
- [Custom Component Paths and Namespaces](#custom-component-paths-and-namespaces)
- [Blade Component Prefix](#blade-component-prefix)
- [Compile Time Rendering](#compile-time-rendering)
- [The View Manifest](#the-view-manifest)
- [License](#license)

Expand Down Expand Up @@ -1298,6 +1299,53 @@ Custom components can leverage all features of the Dagger compiler using their c

You are **not** allowed to register the prefix `x` with the Dagger compiler; attempting to do so will raise an `InvalidArgumentException`.

## Compile Time Rendering

The Dagger compiler contains a subsystem known as the Compile Time Renderer, or CTR. This checks to see if all the props on a component are resolvable at runtime; if so, it may elect to compile the component at runtime and insert the pre-rendered results into Blade's compiled output.

This feature has a number of internal safe guards, and here are a few of the things that will internally disable this feature:

- Dynamic/interpolated variable references
- Using Mixins
- Most static method calls
- Referencing PHP's [superglobals](https://php.net/superglobals), such as `$_GET` or `$_POST`
- Using debugging-related functions in a component, such as `dd`, `dump`, `var_dump`, etc.
- Calling functions such as `time`, `now`, or `date`
- Enabling the Attribute Cache on a component
- Components with slots

Imagine we have the following alert component:

```blade
<!-- /resources/dagger/views/alert.blade.php -->
@props(['type' => 'info', 'message'])
<div {{ $attributes->merge(['class' => 'alert alert-'.$type]) }}>
{{ $message }}
</div>
```

If we were to call the alert component like so:

```blade
<c-alert message="My awesome message" />
```

The compiler would detect that all props are resolvable, and the following would be emitted in the compiled Blade output:

```html
<div class="alert alert-info">
The awesome message
</div>
```

However, if we were to call our component like so, the compiler would not attempt to render the component at compile time:

```blade
<c-alert :$message />
```

## The View Manifest

You may have noticed JSON files being written to your compiled view folder. These files are created by Dagger's "view manifest", which tracks dependencies of compiled views.
Expand Down
29 changes: 29 additions & 0 deletions tests/Compiler/DocsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -540,3 +540,32 @@
$this->render($template)
);
});

test('ctr alert example', function () {
$template = <<<'BLADE'
<c-alert message="The awesome message" />
BLADE;

$expected = <<<'EXPECTED'
<div class="alert alert-info">
The awesome message
</div>
EXPECTED;

$this->assertSame($expected, $this->compile($template));
$this->assertSame($expected, $this->render($template));

$template = <<<'BLADE'
<c-alert :$message />
BLADE;

$this->assertNotSame(
$expected,
$this->compile($template),
);

$this->assertSame(
$expected,
$this->render($template, ['message' => 'The awesome message'])
);
});
5 changes: 5 additions & 0 deletions tests/resources/components/alert.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@props(['type' => 'info', 'message'])

<div {{ $attributes->merge(['class' => 'alert alert-'.$type]) }}>
{{ $message }}
</div>

0 comments on commit 713a767

Please sign in to comment.