Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Format Laravel Blade Codes in Visual Studio Code?

I have installed some Visual Studio Code extensions to handle Laravel Blade Codes like

laravel-blade [ https://marketplace.visualstudio.com/items?itemName=KTamas.laravel-blade ]

Laravel Blade Snippets [ https://marketplace.visualstudio.com/items?itemName=onecentlin.laravel-blade ]

But they are not helping me to format Laravel Blade Codes [blade.php files]. I mean they are not auto indenting the codes as I expected. But I have seen blade codes online which are well indented in visual studio Code IDE.

Example :

enter image description here

https://medium.com/@ardanirohman/laravel-%EF%B8%8F-visual-studio-code-debug-environment-65440274d3b0

Is their any specific configuration or technique which I can use to make these extensions work or there is some other way? Thanks in advance

like image 805
coolsaint Avatar asked Sep 17 '17 19:09

coolsaint


People also ask

How do you beautify your code in VS code?

The code formatting is available in Visual Studio Code through the following shortcuts: On Windows Shift + Alt + F. On Mac Shift + Option + F. On Linux Ctrl + Shift + I.

How do I use code formatting in Visual Studio code?

VS Code has great support for source code formatting. The editor has two explicit format actions: Format Document (Ctrl+Shift+I) - Format the entire active file. Format Selection (Ctrl+K Ctrl+F) - Format the selected text.

What is Blade syntax in Laravel?

The Blade is a powerful templating engine in a Laravel framework. The blade allows to use the templating engine easily, and it makes the syntax writing very simple. The blade templating engine provides its own structure such as conditional statements and loops.

How do I change the default format format in VSCode?

Now, highlight your code and right-click. Select Format Document. Once you click on Format Document, a dialog box will tell you to configure your code formatter. This is to set your default code formatter.


1 Answers

  1. First go to "Visual Studio Code" Settings and search for the option "files.associations" If that option is not available in your settings. Please update your Visual Studio Code to latest version.

  2. In the settings overwrite panel past the following snippet

"files.associations": {     "*.blade.php": "html",     "*.tpl": "html" } 

It'll associate .blade.php and .tpl file extensions for html type

Save it and your are good to go. Happy formatting :)

enter image description here

Update: There are still some issues in formatting html with blade code, the formatter tend to bring blade codes in a single line while executing format command. However, if your codes grow that's won't be a problem. Moreover, you can easily manage those by putting a line comment in between your blade blocks. And commenting in code is always a good thing.


Update: Seems updated version adds an option to enable blade formatting in the user settings. To use that,

  1. Please make sure you are using the latest version and "Laravel Blade Snippets" Extension installed.

  2. Then Ctrl+Shift+P : type User settings : Enter

enter image description here 3. In "User settings" > "Extensions" > "Blade Configaration" check the option Enable format blade file.

enter image description here

That's it.

like image 60
Syed Ekram Uddin Avatar answered Sep 18 '22 23:09

Syed Ekram Uddin