VS Code Snippets for Livewire and Alpine.js

Last updated on by

VS Code Snippets for Livewire and Alpine.js image

VS Code - as many code editors do - includes a feature called "snippets". The feature allows you to configure shortcuts for pieces of code that you use often. For example, you could set up a snippet so that you type a few characters like fun, press tab, and it'll expand into a whole function declaration.

I've recently been making another round at optimizing my VS Code setup, so I skimmed through Caleb Porzio's Make VS Code Awesome course again. It reminded me how useful and powerful these snippets can be, so I started thinking about new ones I could add that would make my life easier. Today, I'll share some of those with you and maybe you can take a few away that will be useful to your workflow. Specifically, I'll be sharing ones I use when developing Livewire and Alpine.js apps.

How to configure snippets in VS Code

To start off, how do you configure snippets in VS Code? If you already know how, you can skip this section and get to the good part.

First, open the Command Pallette by pressing cmd+shift+p on Mac or ctrl+shift+p on Windows. Start typing in "Snippets" and it should filter down to an option called "Snippets: Configure User Snippets". Select that option, then it will prompt you to select the language you want to configure snippets for.

Following the basic example I mentioned earlier, if you select "javascript.json (JavaScript)" and paste the following snippet inside, you should have successfully configured a "function" snippet that'll be available when you're writing in a JS file.

{
"Function": {
"prefix": "fun",
"body": [
"function $1($2) {",
" $3",
"}"
]
},
}

The configuration of snippets is relatively simple. Each snippet is a JSON object. The key of the object is the snippet name. The prefix inside is the shortcut you'll use to insert the snippet into your code. The body is an array of lines that will be inserted as part of the snippet. You can also include placeholders in the snippet by using $1, $2, $3, etc. After you insert the snippet, you can continue pressing tab and it'll move your cursor to each of the placeholders. In this example, the placeholders make it really easy to add the function name, parameters, then body.

PHP/Livewire

Now that you know how to configure a snippet let's go over some of my favorites for PHP and Livewire classes. These can be configured by selecting "php.json (PHP)" in the Command Pallette.

Class properties and methods cover the basics, but I recent added snippets for Livewire's mount method, #[Computed] properties (including the PHP attribute), and #[Url] properties.

{
"Public Property": {
"prefix": "pub",
"body": [
"public ${0};"
],
"description": "PHP Public Property"
},
"Computed Property": {
"prefix": "comp",
"body": [
"#[Computed]",
"public function $1()",
"{",
" $2",
"}"
]
},
"Livewire Url Property": {
"prefix": "url",
"body": [
"#[Url$3]",
"public $1 = $2;",
]
},
"Livewire Mount Method": {
"prefix": "mount",
"body": [
"public function mount()",
"{",
" $1",
"}"
]
},
"Method": {
"prefix": "met",
"body": [
"public function $1($2)",
"{",
" $3",
"}"
]
},
"Private Method": {
"prefix": "pmet",
"body": [
"private function ${1}(${2})",
"{",
" ${0}",
"}"
],
"description": "PHP private function"
},
}

Blade and Alpine.js

Some of my favorites for Blade and Alpine snippets are:

  • con for console.log
  • raw for console.log with the value wrapped in Alpine.raw (a tip I covered in a recent article)
  • tailwindcdn for the Tailwind CDN script which is useful for quickly setting up Tailwind for demos and testing ideas out

These can be configured by selecting "blade.json (Blade)" in the Command Pallette.

{
"Console Log": {
"prefix": "con",
"body": [
"console.log($1)"
]
},
"Console Log (Alpine.raw)": {
"prefix": "raw",
"body": [
"console.log(Alpine.raw($1))"
]
},
"Tailwind Play CDN": {
"prefix": "tailwindcdn",
"body": [
"<script src=\"https://cdn.tailwindcss.com\"></script>"
]
},
}

If you're using Livewire Volt's class API, you might find it useful to include you normal PHP snippets in the Blade configuration as well.

I hope you were able to find some of the snippets useful or at least got some inspiration for snippets that will benefit your own workflow! Again, if you haven't checked out Caleb's Make VS Code Awesome course yet, I highly recommend it. It includes a bunch of other snippets that I didn't cover here, plus theming, keyboard shortcuts, extensions, and more.

Jason Beggs photo

TALL stack (Tailwind CSS, Alpine.js, Laravel, and Livewire) consultant and owner of designtotailwind.com.

Filed in:
Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.

Laravel Forge logo

Laravel Forge

Easily create and manage your servers and deploy your Laravel applications in seconds.

Laravel Forge
Tinkerwell logo

Tinkerwell

The must-have code runner for Laravel developers. Tinker with AI, autocompletion and instant feedback on local and production environments.

Tinkerwell
No Compromises logo

No Compromises

Joel and Aaron, the two seasoned devs from the No Compromises podcast, are now available to hire for your Laravel project. ⬧ Flat rate of $7500/mo. ⬧ No lengthy sales process. ⬧ No contracts. ⬧ 100% money back guarantee.

No Compromises
Laravel Idea for PhpStorm logo

Laravel Idea for PhpStorm

Ultimate PhpStorm plugin for Laravel developers, delivering lightning-fast code completion, intelligent navigation, and powerful generation tools to supercharge productivity.

Laravel Idea for PhpStorm
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum
Shift logo

Shift

Running an old Laravel version? Instant, automated Laravel upgrades and code modernization to keep your applications fresh.

Shift
Bacancy logo

Bacancy

Supercharge your project with a seasoned Laravel developer with 4-6 years of experience for just $2500/month. Get 160 hours of dedicated expertise & a risk-free 15-day trial. Schedule a call now!

Bacancy
Lucky Media logo

Lucky Media

Get Lucky Now - the ideal choice for Laravel Development, with over a decade of experience!

Lucky Media
Lunar: Laravel E-Commerce logo

Lunar: Laravel E-Commerce

E-Commerce for Laravel. An open-source package that brings the power of modern headless e-commerce functionality to Laravel.

Lunar: Laravel E-Commerce
LaraJobs logo

LaraJobs

The official Laravel job board

LaraJobs
SaaSykit: Laravel SaaS Starter Kit logo

SaaSykit: Laravel SaaS Starter Kit

SaaSykit is a Multi-tenant Laravel SaaS Starter Kit that comes with all features required to run a modern SaaS. Payments, Beautiful Checkout, Admin Panel, User dashboard, Auth, Ready Components, Stats, Blog, Docs and more.

SaaSykit: Laravel SaaS Starter Kit
Supercharge Your SaaS Development with FilamentFlow: The Ultimate Laravel Filament Boilerplate logo

Supercharge Your SaaS Development with FilamentFlow: The Ultimate Laravel Filament Boilerplate

Build your SaaS application in hours. Out-of-the-box multi-tenancy and seamless Stripe integration. Supports subscriptions and one-time purchases, allowing you to focus on building and creating without repetitive setup tasks.

Supercharge Your SaaS Development with FilamentFlow: The Ultimate Laravel Filament Boilerplate
JetShip - Laravel Starter Kit logo

JetShip - Laravel Starter Kit

A Laravel SaaS Boilerplate and a starter kit built on the TALL stack. It includes authentication, payments, admin panels, and more. Launch scalable apps fast with clean code, seamless deployment, and custom branding.

JetShip - Laravel Starter Kit
Rector logo

Rector

Your partner for seamless Laravel upgrades, cutting costs, and accelerating innovation for successful companies

Rector
MongoDB logo

MongoDB

Enhance your PHP applications with the powerful integration of MongoDB and Laravel, empowering developers to build applications with ease and efficiency. Support transactional, search, analytics and mobile use cases while using the familiar Eloquent APIs. Discover how MongoDB's flexible, modern database can transform your Laravel applications.

MongoDB

The latest

View all →
URI Parsing and Mutation in Laravel 11.35 image

URI Parsing and Mutation in Laravel 11.35

Read article
Securing Laravel Sessions with ID Regeneration image

Securing Laravel Sessions with ID Regeneration

Read article
Paginate Multiple Eloquent Models with the Union Paginator Package image

Paginate Multiple Eloquent Models with the Union Paginator Package

Read article
Whisper.php - Automatic speech recognition and transcription image

Whisper.php - Automatic speech recognition and transcription

Read article
Multiple Column Plucking in Laravel Collections image

Multiple Column Plucking in Laravel Collections

Read article
File Manager Package for Livewire image

File Manager Package for Livewire

Read article