A robust component authoring library for Laravel Blade.
A robust component authoring library for Laravel Blade.
Dagger is a component authoring library for Laravel's Blade templating engine. Dagger components are heavily inspired by Laravel's anonymous components. Dagger's differentiating features are its compiler and expanded capabilities.
The Dagger compiler works hard to inline your component's code, perform various optimizations, as well as enable powerful new features, such as the Attribute Cache, Attribute Forwarding, and Slot Forwarding. The end result is a powerful, performant component authoring library with a familiar syntax.
The main visual difference when working with Dagger components is the use of the ` 'info', 'message'])
```blade
…
Like with Blade's anonymous components, you may use the `.` character to indicate if a component is contained within a sub-directory. For a component defined at `resources/dagger/views/inputs/button.blade.php`, you may render it like so:
```blade
Dagger components follow the same rules as Laravel's Anonymous Index Components, allowing you to group components into their own self-contained directories.
Assuming the following component directory structure:
/resources/dagger/views/accordion.blade.php
/resources/dagger/views/accordion/item.blade.phpYou could render the accordion component and the nested item like so:
...
If you'd prefer to not have the "root" view be located within the /resources/dagger/views/ directory, you may create a file with the same name as the component within the component's directory:
/resources/dagger/views/accordion/accordion.blade.php
/resources/dagger/views/accordion/item.blade.phpAlternatively, you may also create a view named index within the component's directory:
/resources/dagger/views/accordion/index.blade.php
/resources/dagger/views/accordion/item.blade.phpWhile it is strongly discouraged to move Dagger components into Laravel's resources/views/components directory, it is technically possible, and you are free to do what you want in your own project. If you'd like to move the Dagger component path, you may add the following to your applications service provider:
The Dagger compiler supports Blade's @props and @aware directives, but also provides a new functional approach to defining components. When using the functional approach, the first thing within your component definition must be a PHP block, where the component is defined.
The following component definitions would produce identical output.
Using the component builder:
@php
use function Stillat\Dagger\component;
component()->props(['type' => 'info', 'message']);
@endphp
Using Blade directives:
@props(['type' => 'info', 'message'])
Dagger slots behave the same as Blade's component slots. Default slot content is specified between component tag pairs. Assuming the following component definition:
Slot content may be specified like so:
The Slot Content
Dagger components also support named or scoped slots. Accessing scoped slots is done through the $slots variable, which is different from Blade components. This is done to help prevent collisions with variables that may have the same name as desireable slot names.
Assuming the following component definition:
{{ $slot }}
You may specify content for each slot like so:
Header Content
Footer Content
Default Slot Content
You may use the hasSlot method to determine if the component has a named slot:
@if ($component->hasSlot('header'))
// The developer provided a named "header" slot.
@endifIf you'd like to check if default slot content was provided you may use the hasDefaultSlot() methoid:
@if ($component->hasDefaultSlot())
// The developer provided a named "header" slot.
@endifWhen using the functional syntax, it is important to note that PHP code that appears before the component() call may be removed from the compiled output. You can use this space to perform logic at compile time or set variables.
@php
use function Stillat\Dagger\component;
// Danger zone.
$myCustomVariable = 'the value';
component()->props(['type' => 'info', 'message']);
// Safe zone.
@endphp
To be safe, any custom PHP code you'd like to execute should appear after the component() call:
@php
use function Stillat\Dagger\component;
component()->props(['type' => 'info', 'message']);
$myCustomVariable = 'the value';
@endphp
This does not apply to use statements and variable assignments. Any custom PHP code that appears before the component() call will be executed at compile time, however.
When using the component() builder function, you should only call the component() function once. Do not make repeated calls to it:
@php
use function Stillat\Dagger\component;
component()->props(['type' => 'info', 'message']);
component()->aware(['message']); ❌
@endphp
...Instead, always chain the builder methods:
@php
use function Stillat\Dagger\component;
component()->props(['type' => 'info', 'message'])
->aware(['message']); ✅
@endphp
...If you'd like to rename the automatic $component variable within your component's definition, you may simply assign the results of the component() function to a variable:
@php
use function Stillat\Dagger\component;
$theAlert = component()->props(['type' => 'info', 'message']);
@endphp
[!NOTE] The component instance will still be accessible via the
$componentvariable inside slots, even if it has been renamed within the component itself. This is intentional to provide a consistent experience for component consumers.
To ease development, and provide a familiar starting place, the Dagger compiler supports Blade's @props directive to help differentiate between which data is a property of the component, and what data should be placed inside the component's attribute bag.
@props(['type' => 'info', 'message'])
Given the component above, we may render it like so:
We can also use the props component builder method to achieve the same results:
@php
use function Stillat\Dagger\component;
component()->props(['type' => 'info', 'message']);
@endphp
Both the @aware and @props directives are supported by the Dagger compiler. However, because the compiler needs to know about their values ahead of time, conditional use of these directives is not supported in Dagger components.
You must not have Dagger component templates like the following:
@if ($someCondition)
@props(['title'])
@else
@props(['title', 'somethingElse'])
@aware(['title'])
@endif
You have multiple options for accessing parent data from within a child component.
The most familiar option will be to use Blade's @aware directive.
Assuming we had a menu component with a parent and a child component:
...
...
The `` component may have an implementation like the following:
@props([
'color' => 'gray'
])
merge(['class' => 'bg-'.$color.'-200']) }}>
{{ $slot }}
Because the color prop was passed into the parent (), it won't be immediately available inside . We can use the @aware directive to make that variable available inside `` as well:
@aware([
'color' => 'gray'
])
merge(['class' => 'text-'.$color.'-800']) }}>
{{ $slot }}
Alternatively, we may also use the aware builder method to specify variables that should be passed to our child component:
@php
Stillat\Dagger\component()->aware(['color' => 'gray']);
@endphp
merge(['class' => 'text-'.$color.'-800']) }}>
{{ $slot }}
We can also access the parent component instance directly using the parent() method:
merge(['class' => 'text-'.$component->parent()->color.'-800']) }}>
{{ $slot }}
If you are in a deeply nested component, you may access parent instances on parent instances:
{{ $component->parent()->parent()->someValue }}You may also supply the name of the parent component you'd like to retrieve data from. Doing so will return access to the nearest parent instance with that name:
{{ $component->parent('nav')->someValue }}Aware variables will automatically be added to the component's props list, preventing them from appearing in the attribute bag.
@props(['color'])
{{ $slot}}
@aware(['color']) // "color" will be automatically added to the component's props.
{{ $slot }}
You may use Laravel's validation features to validate the props of your Dagger components. To do this, you may use the validateProps builder method to specify the prop and validation rules you'd like to enforce. As an example, the following would ensure that a title property was supplied to the button component:
@php
use function Stillat\Dagger\component;
component()
->props(['title'])
->validateProps([
'title' => 'required',
]);
@endphp
{{ $title }}The following would not trigger a validation error:
while the following would:
The following data sources are also considered when validating components:
aware builder method or directiveIf you are using only string-based validation rules, you can skip the additional method call and specify them on the props directly. To do this, separate the prop name from the rules using the | character:
@php
use function Stillat\Dagger\component;
component()
->props(['title|required']);
@endphp
{{ $title }}You can still specify default prop values when adding shorthand validation rules:
@php
use function Stillat\Dagger\component;
component()
->props([
'size|numeric|min:1|max:5' => 3,
]);
@endphp
The Size: {{ $size }}The Dagger compiler introduces the concept of "compiler attributes", which have special meaning to the compiler. The most common of these is the #id attribute, which may be used to name a nested component:
Compiler attribute values must be static and cannot contain PHP expressions or reference variables.
If you need to output an attribute beginning with #, you may escape compiler attributes by prefixing it with another # character:
In general, you should avoid using props or attributes beginning with # as they are likely to be further developed upon and made available as an extension point, or may conflict with forwarded attributes.
You may conditionally render a component by adding the #when compiler attribute.
For example, instead of the following:
@if ($account->is_No open issues yet, or sync has not completed.