Download the PHP package erickcomp/laravel-stacked-components without Composer
On this page you can find all versions of the php package erickcomp/laravel-stacked-components. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package laravel-stacked-components
Use Blade Components syntax to insert (push/prepend) content to stacks
This package provides some blade components that you can use to insert content into stacks, most notably, scripts and styles.
Requirements
- PHP 8.2+
- Laravel 12 or 13
For Laravel 10 and 11, use version 0.10.
Installation
To customize the config, publish it to :
Every config value can also be set by an environment variable named plus the config key in upper snake case, like for .
Upgrading from 0.x? See UPGRADE.md.
The vanilla-Blade way
To push some JS file/code to a stack, you have to (from Laravel docs):
or, for inline JS:
It, of course, works. But when using Blade Components, the intention is to stay closer to HTML whenever possible.
The Stacked Components way
JS component
or, for inline JS:
Any other attribute, like or , goes to the tag.
CSS component
pushes . For inline CSS:
pushes a tag. Any other attribute, like , goes to the tag, and and can be overridden.
A component can't have both a and inline code.
The asset function
This package calls Laravel's function on the of the JS and CSS components by default. You can override this behavior to call your own callable or to call nothing at all. To do either of these, you can set the environment variable
or the config value
or, for a single component, the attribute:
The asset function can be any PHP callable or use Laravel's "@" syntax (). The callable is invoked through Laravel's container, which passes the asset's src to its parameter and injects any other dependency it asks for. Global functions with no parameter, like Laravel's , get the src as their first argument instead. To call no function, set the config value or the environment variable to false.
Note that if you use objects (anonymous functions, invokable objects, first-class callables), you won't be able to use Laravel's config cache, since it does not support cache for these types of config values.
Content component
or, for inline JS:
It pushes its content as is.
Div component
pushes its content wrapped in a with the given attributes.
Stack options
All the components accept:
- : the stack to push to. For the JS and CSS components, you can set a default stack in config/env and omit it (see below).
- : whether the same content is pushed only once, like Blade's . It defaults to true for the JS and CSS components and to false for the content and div components.
- : prepends the content to the stack instead of pushing it, like Blade's .
Both take a string (), a bound bool () or no value (, meaning true):
Default stacks
For the CSS and JS components, you can set a default stack in config/env and omit it when using the component, like this:
.env file:
and in your view file:
The head_bottom stack
Every template with a tag gets a stack right before it, so you can push to the end of without declaring a stack in your layout:
To turn it off, set the config value or the environment variable to false, and clear the view cache: templates get the stack when they're compiled.
Resolving name collisions with other components
If your app, or a library you're using, already defines components with the same names as this package's, you have 2 options:
1 - Register a namespace for the components of this package. To do so, you can set the environment variable
or the config value
If you set the namespace to , it will use the namespace "stacked", as in . If you set it to any string, that string will be used as the blade components namespace. For more information on components namespaces check the Laravel docs
2 - Set the config values
or the environment variables
To specify alternate names for the components.
Remember to clear the views caches when changing any of these values. To clear the view cache run the view:clear artisan command:
Extending the components
You can extend the component classes, but their protected methods and properties are internal: they aren't part of the package's API and may change in any release. Extend them at your own risk.
License
The MIT License (MIT). Please see License File for more information.