This package provides a collection of useful laravel blade directives.
You can install the package via composer:
$ composer require cssjockey/laravel-directives
Optional: The service provider will automatically get registered. Or you may manually add the service provider in your config/app.php file:
'providers' => [
// ...
CSSJockey\LaravelDirectives\LaravelDirectivesServiceProvider::class,
];
Fetch any model by ID to display any data from result.
@modeldata('App\Models\User'|1|"email")
This will fetch the User
with ID 1 and renders the email address.
Renders the value of an array and supports array dot notation.
@arraydata($array|$variable)
example with simple array:
@arraydata(['key1' => 'value1', 'key2' => 'value2']|'key1')
Output: value1
@arraydata(['key1' => 'value1', 'key2' => 'value2']|'key2')
Output: value2
example with multi-dimensional array:
$data = [
'parent' => [
'child' => 'child value',
'child2' => [
'key' => 'child2 key value'
],
]
];
@arraydata($data|'parent')
Output: {"child":"child value","child2":{"key":"child2 key value"}}
This will return JSON string as the values is array.
Supports dot notation.
@arraydata($data|'parent.child')
Output (String): child value
@arraydata($data|'parent.child2.key')
Output (String): child2 key value
Renders the content in <pre>
tag.
example of inline code
@code('<div>Hello world</div>')
Output: <div>hello world</div>
example with multi-line code block
@code
<div>
<a href="#">Click me</a>
</div>
@endcode
Output: is wrapped in <pre> tag
<div>
<a href="#">Click me</a>
</div>
Die and dump, renders only if APP_ENV is set to local.
example
@dd('die and dump here')
Output: "die and dump here"
Dump, die and debug, renders only if APP_ENV is set to local.
example
@ddd("Dump, Die, Debug")
Inline dump, renders only if APP_ENV is set to local.
example
@ddd("Dump this inline")
Output content for $errors->has('input_name')
to determine if any error message found for the given input field.
@haserror('name'|"Message or class name goes here.")
Output (String): Message or class name goes here.
@haserror('name')
You can display any text or html here if any error message exists for input field name.
@endhaserror()
Quickly check if first parameter is an instance of second parameter.
@instanceof($user|'\App\Models\User')
Yes $user is an instance of '\App\Models\User'
@endinstanceof
Quickly check if the parameter is a specific type.
$variable = 'a valid string';
@typeof($variable|'string')
Yes $variable type is string.
@endtypeof
$variable = ['valid' => 'array'];
@typeof($variable|'array')
Yes $variable type is array.
@endtypeof
Display content if variable of condition is true.
$var1 = 'one';
$var2 = 'one';
$true = ($var1 === $var2);
@istrue($true|"Display this content")
@istrue($var1 === $var2|"Yes it is")
@istrue($true)
Display this line
@endistrue
@istrue($var1 === $var2)
Yes the condition is true
@endistrue
Display content if variable of condition is false.
$var1 = 'one';
$var2 = 'two';
$false = ($var1 === $var2);
@isfalse($false|"Display this content")
@isfalse($var1 === $var2|"No it is not")
@isfalse($false)
Display this line
@endisfalse
@isfalse($var1 === $var2)
No the condition is not true
@endisfalse
Display content only if variable is NULL.
$variable = null;
@isnull($null_variable|"yes it is null")
@isnull($null_variable)
Yes variable is null
@endisnull
Display content only if variable is NOT NULL.
$variable = 'something other than null';
@isnotnull($variable|"yes variable is not null")
@isnotnull($variable)
Yes variable is not null
@endisnotnull
Display content only if the user is logged in.
@isuser("Yes user is logged in!")
@isuser
Yes user is logged in!
@endisuser
Display content only if the user is not logged in.
@isguest("Show this line to the guest!")
@isguest
Show this content to the guest!
@endisguest
Show content only if current route matches the first parameter.
@routeis("route.name.here"|"show this content")
@routeis("route.name.here")
show this content
@endrouteis
Show content only if current route does not match the first parameter.
@routeisnot("route.name.here"|"show this content")
@routeisnot("route.name.here")
show this content
@endrouteisnot
Repeat any content specified number of times.
@repeat(10)
<div class="">Repeat this content</div>
@endrepeat
Create a script
element or include a javascript file.
@script('public/url/to/script.js')
@script
console.log('I run from this directive')
@endscript
Create a style
element or include a javastyle file.
@style('public/url/to/style.css')
@style
body{ overflow: hidden }
@endstyle
Bind attributes to any html tag.
<div @tagattributes(['id' => 'some-id', 'class' => 'css-class', 'data-item' => 'value'])>
content goes here.
</div>
Output:
<div id="some-id" class="css-class" data-item="value">
content goes here.
</div>
Please see the changelog for more information on what has changed recently.
- Add more free icons to the package.
- Create an artisan command to optimize all the SVG files.
Please see contributing for details.
If you discover any security-related issues, please email admin@cssjockey.com instead of using the issue tracker.
Mohit Aneja All Contributors
MIT Please see the license for more information.