Laravel Blade: How to Enable Auto-completion in VS Code

Updated: January 18, 2024 By: Guest Contributor Post a comment

Introduction

Laravel Blade templates offer a powerful and elegant templating engine for PHP developers working with the Laravel framework. However, when it comes to writing Blade templates in Visual Studio Code (VS Code), having auto-completion can significantly boost productivity and accuracy. In this tutorial, we’ll explore steps to enable auto-completion for Laravel Blade in VS Code and demonstrate it through various examples.

Setting Up Visual Studio Code for Blade

Before we start, ensure you have Visual Studio Code installed. Follow the steps below to set up your environment for Blade auto-completion:

  1. Open Visual Studio Code and go to the Extensions view by pressing Ctrl+Shift+X or clicking on the squared icon on the sidebar.
  2. Search for ‘Laravel Blade Snippets’ in the Extensions view search box and install the extension. There are a few options available, so you may try ‘Blade Spacer’ or ‘Laravel Blade formatter’ as well.
  3. Once the extension is installed, it will automatically enable auto-completion for Blade syntax. However, if it doesn’t work as expected, you might need to reload VS Code or manually enable the extension in the settings.

Configuring User Settings for Enhanced Auto-completion

You can fine-tune your VS Code settings for an even better Blade auto-completion experience. Add the following configurations to your settings.json file:

{
    "files.associations": {
        "*.blade.php": "blade"
    },
    "emmet.includeLanguages": {
        "blade": "html"
    },
    "blade.format.enable": true,
    "[blade]": {
        "editor.quickSuggestions": true
    }
}

The above settings will ensure that all files with a .blade.php extension are recognized as Blade templates and that Emmet abbreviations are enabled for Blade, giving you HTML auto-completions within Blade files.

Code Examples and Usage

Now that you’ve set up auto-completion, let’s explore some examples to demonstrate its power. Here’s how you can use Blade auto-completion:

Example 1: Auto-completing Blade Directives

@section('content')

When you type @ followed by the first few letters of a Blade directive, the auto-completion suggestions should pop up. For example, as ‘sec…’, it would suggest the @section directive with its structure.

Example 2: Auto-completing Blade Control Structures

@if($condition)
    ...
@endif

For Blade control structures such as @if, typing ‘@if’ should trigger the snippet that auto-completes the entire @if ... @endif block.

Example 3: Using Emmet in Blade

Thanks to the Emmet integration, you can type HTML abbreviations inside your Blade files and have them expand. For instance, typing ‘ul>li*3’ and pressing Tab will expand it into a list with three items.

Further Tips and Tricks

Besides auto-completion, here are some additional tips to enhance your Blade templates editing experience in VS Code:

  • Use the Blade formatter extension to auto-format your Blade files on save.
  • Explore other Blade-related extensions for additional snippets or integrations with Laravel development processes.
  • Customize your snippets by adding your own frequently used pieces of code to VS Code’s User Snippets under the ‘Blade’ language.

Conclusion

Enabling auto-completion in VS Code for Laravel Blade templates not only speeds up the development process but also ensures a more error-free coding experience. By following the steps outlined in this tutorial, you will be well-equipped to make the most out of your Blade templates. Continue to explore and customize your environment to suit your workflow and happy coding!

Remember, a well-configured development environment is a productive one – take the time to set it up correctly for a smoother and more efficient coding journey with Laravel Blade and Visual Studio Code.