|
1 | | -# Leaf Blade |
| 1 | +# Leaf + Blade |
| 2 | + |
| 3 | +<!-- markdownlint-disable no-inline-html --> |
| 4 | + |
| 5 | +<script setup> |
| 6 | +import VideoModal from '@theme/components/shared/VideoModal.vue' |
| 7 | +</script> |
| 8 | + |
| 9 | +Blade is a templating engine included with Laravel that helps you create dynamic views easily. Unlike other templating engines, Blade allows you to use regular PHP code inside your templates together with all the goodness it offers you. Behind the scenes, Blade templates are turned into plain PHP and cached, so they run quickly without slowing down your app. Blade files use the `.blade.php` extension to distinguish them from regular PHP files. |
| 10 | + |
| 11 | +Leaf Blade is a port of the [jenssegers/blade](https://github.com/jenssegers/blade) package that allows you to use blade templates in your Leaf PHP projects. |
| 12 | + |
| 13 | +::: details New to Blade? |
| 14 | + |
| 15 | +This video by The Net Ninja will help you get started with blade. |
| 16 | + |
| 17 | +<VideoModal |
| 18 | + title="New to Blade?" |
| 19 | + subject="Laravel Tutorial for Beginners #5 - Blade Basics" |
| 20 | + description="This video by The Net Ninja will help you get started with blade." |
| 21 | + videoUrl="https://www.youtube.com/embed/pQ2vxa4_f2w" |
| 22 | +/> |
| 23 | + |
| 24 | +::: |
| 25 | + |
| 26 | +## Setting Up |
| 27 | + |
| 28 | +Blade comes with Leaf MVC out of the box, fully configured and ready to use. However, if you're using Leaf Core, you'll need to set up Blade yourself. Don't worry, it's pretty easy. All you need to do is install Blade, configure it to match your project's setup, and you're good to go. |
| 29 | + |
| 30 | +You can install Leaf Blade with the Leaf CLI: |
| 31 | + |
| 32 | +::: code-group |
| 33 | + |
| 34 | +```bash:no-line-numbers [Leaf CLI] |
| 35 | +leaf install blade |
| 36 | +``` |
| 37 | + |
| 38 | +```bash:no-line-numbers [Composer] |
| 39 | +composer require leafs/blade |
| 40 | +``` |
| 41 | + |
| 42 | +::: |
| 43 | + |
| 44 | +After this, you just need to inform Leaf of Blade's existence: |
| 45 | + |
| 46 | +```php |
| 47 | +app()->attachView(Leaf\Blade::class); |
| 48 | +``` |
| 49 | + |
| 50 | +This will magically set up Blade for you and pop up a `blade()` function you can use to render your Blade views. |
| 51 | + |
| 52 | +```php |
| 53 | +app()->blade()->configure([ |
| 54 | + 'views' => 'views', |
| 55 | + 'cache' => 'storage/cache' |
| 56 | +]); |
| 57 | +``` |
| 58 | + |
| 59 | +Magic! You can now use Blade to create and render your views. |
| 60 | + |
| 61 | +## Creating Blade Views |
| 62 | + |
| 63 | +Blade views are a pretty sweet mixture of HTML, PHP, and clean syntax. You can create a new Blade view by creating a new file with the `.blade.php` extension in your "views" folder. Here's an example of a simple Blade view: |
| 64 | + |
| 65 | +::: code-group |
| 66 | + |
| 67 | +```blade [hello.blade.php] |
| 68 | +<!DOCTYPE html> |
| 69 | +<html lang="en"> |
| 70 | +<head> |
| 71 | + <meta charset="UTF-8"> |
| 72 | + <meta http-equiv="X-UA-Compatible" content="IE=edge"> |
| 73 | + <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| 74 | + <title>Document</title> |
| 75 | +</head> |
| 76 | +<body> |
| 77 | + <h1>Hello, {{ $name }}</h1> |
| 78 | +</body> |
| 79 | +</html> |
| 80 | +``` |
| 81 | + |
| 82 | +::: |
| 83 | + |
| 84 | +This should look pretty familiar if you know HTML (of course you do). The only difference is the `{{ $name }}` part. This is Blade's way of creating a variable in your view. When you render this view, Blade will allow you pass in a variable called `$name` and it will be displayed in place of `{{ $name }}`. Let's see how you can render this view. |
| 85 | + |
| 86 | +## Rendering Blade Views |
| 87 | + |
| 88 | +Remember we set up Blade earlier? Now we can use it to render our Blade views. Here's how you can render the `hello.blade.php` view we created earlier: |
| 89 | + |
| 90 | +```php |
| 91 | +echo app()->blade()->render('hello', ['name' => 'Michael']); |
| 92 | +``` |
| 93 | + |
| 94 | +This will render the `hello.blade.php` view and pass in a variable called `name` with the value `Michael`. When you open the view in your browser, you should see a big "Hello, Michael" on your screen. |
| 95 | + |
| 96 | +## Extending Blade Views |
| 97 | + |
| 98 | +Blade allows you to define custom directives using the `directive()` method. When the Blade compiler encounters the custom directive, it will call the provided callback with the expression that the directive contains. The callback is free to return the value of its contents however you like: |
| 99 | + |
| 100 | +```php |
| 101 | +app()->blade()->directive('datetime', function ($expression) { |
| 102 | + return "<?php echo with({$expression})->format('F d, Y g:i a'); ?>"; |
| 103 | +}); |
| 104 | +``` |
| 105 | + |
| 106 | +Which allows you to use the following in your blade template: |
| 107 | + |
| 108 | +```html |
| 109 | +Current date: @datetime($date) |
| 110 | +``` |
| 111 | + |
| 112 | +This will output the current date in the format `F d, Y g:i a`. You can define as many custom directives as you want to make your Blade views more powerful. |
| 113 | + |
| 114 | +## Conclusion |
| 115 | + |
| 116 | +This is just the beginning of what you can do with Blade. Blade is a powerful templating engine that allows you to create dynamic views with ease. You can use Blade to create complex views with loops, conditions, and even include other views. As this is just an adapter for the original Blade package, you can check out the [Blade documentation](https://laravel.com/docs/8.x/blade) for more information on what you can do with Blade. |
0 commit comments