Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
D

dagger

> 后端框架
Open source

A robust component authoring library for Laravel Blade.

194 stars0 likes1 views
WebsiteGitHub

About

A robust component authoring library for Laravel Blade.

Dagger Components 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

Index Components

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.php

You could render the accordion component and the nested item like so:

blade

    
        ...
    

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.php

Alternatively, 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.php

Moving Dagger Component Views

While 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:

php

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:

blade
@php
use function Stillat\Dagger\component;

component()->props(['type' => 'info', 'message']);
@endphp

Using Blade directives:

blade
@props(['type' => 'info', 'message'])
 

Slots

Dagger slots behave the same as Blade's component slots. Default slot content is specified between component tag pairs. Assuming the following component definition:

blade

Slot content may be specified like so:

blade

    The Slot Content

Named/Scoped Slots

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:

blade

{{ $slot }}

You may specify content for each slot like so:

blade

    
        Header Content
    
    
    
        Footer Content
    
    
    Default Slot Content

Checking if Slots Exist

You may use the hasSlot method to determine if the component has a named slot:

blade
@if ($component->hasSlot('header'))
    // The developer provided a named "header" slot.
@endif

If you'd like to check if default slot content was provided you may use the hasDefaultSlot() methoid:

blade
@if ($component->hasDefaultSlot())
    // The developer provided a named "header" slot.
@endif

Custom PHP When Using the Component Function

When 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.

blade
@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:

blade
@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.

Calling Component Builder Methods

When using the component() builder function, you should only call the component() function once. Do not make repeated calls to it:

blade
@php
use function Stillat\Dagger\component;

component()->props(['type' => 'info', 'message']);
component()->aware(['message']); ❌
@endphp

...

Instead, always chain the builder methods:

blade
@php
use function Stillat\Dagger\component;

component()->props(['type' => 'info', 'message'])
    ->aware(['message']); ✅
@endphp

...

Renaming the Component Variable

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:

blade
@php
use function Stillat\Dagger\component;

$theAlert = component()->props(['type' => 'info', 'message']);
@endphp

[!NOTE] The component instance will still be accessible via the $component variable inside slots, even if it has been renamed within the component itself. This is intentional to provide a consistent experience for component consumers.

Data Properties / Attributes

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.

blade

@props(['type' => 'info', 'message'])

Given the component above, we may render it like so:

blade

We can also use the props component builder method to achieve the same results:

blade
@php
use function Stillat\Dagger\component;

component()->props(['type' => 'info', 'message']);
@endphp

Notes on Conditional aware and props Directives

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:

blade
@if ($someCondition)
    @props(['title'])
@else
    @props(['title', 'somethingElse'])
    @aware(['title'])
@endif

Accessing Parent Data

You have multiple options for accessing parent data from within a child component.

  • Using the aware directive
  • Using the aware Builder Method
  • Accessing Arbitrary Parent Data

Using the aware Directive

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:

blade

    ...
    ...

The `` component may have an implementation like the following:

blade

 
@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:

blade

@aware([
    'color' => 'gray'
])

merge(['class' => 'text-'.$color.'-800']) }}>
    {{ $slot }}

Using the aware Builder Method

Alternatively, we may also use the aware builder method to specify variables that should be passed to our child component:

blade

@php
    Stillat\Dagger\component()->aware(['color' => 'gray']);
@endphp

merge(['class' => 'text-'.$color.'-800']) }}>
    {{ $slot }}

Accessing Arbitrary Parent Data

We can also access the parent component instance directly using the parent() method:

blade

merge(['class' => 'text-'.$component->parent()->color.'-800']) }}>
    {{ $slot }}

If you are in a deeply nested component, you may access parent instances on parent instances:

blade
{{ $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:

blade
{{ $component->parent('nav')->someValue }}

Aware Variables and Attributes

Aware variables will automatically be added to the component's props list, preventing them from appearing in the attribute bag.

blade

@props(['color'])

  {{ $slot}}
blade

@aware(['color']) // "color" will be automatically added to the component's props.

{{ $slot }}

Property Validation

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:

blade

@php
use function Stillat\Dagger\component;

component()
    ->props(['title'])
    ->validateProps([
        'title' => 'required',
    ]);
@endphp

{{ $title }}

The following would not trigger a validation error:

blade

while the following would:

blade

The following data sources are also considered when validating components:

  • Data made available via. the aware builder method or directive
  • Data provided by mixins

Shorthand Validation Rules

If 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:

blade

@php
use function Stillat\Dagger\component;

component()
    ->props(['title|required']);
@endphp

{{ $title }}

You can still specify default prop values when adding shorthand validation rules:

blade
@php
use function Stillat\Dagger\component;

component()
    ->props([
        'size|numeric|min:1|max:5' => 3,
    ]);
@endphp

The Size: {{ $size }}

Compiler Attributes

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:

blade

Compiler attribute values must be static and cannot contain PHP expressions or reference variables.

Escaping Compiler Attributes

If you need to output an attribute beginning with #, you may escape compiler attributes by prefixing it with another # character:

blade

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.

Conditionally Rendering Components

You may conditionally render a component by adding the #when compiler attribute.

For example, instead of the following:

blade
@if ($account->is_

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

PHPbladelaravellaravel-package

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 18, 2026
Category后端框架
PricingOpen source

> Related tools

N
Node.js
基于 V8 的 JavaScript 运行时
D
Django
Python 高级 Web 框架
S
Spring Boot
Java 生态主流微服务框架