DEV Community

Jamiu
Jamiu

Posted on

EJS tags snippets on vscode

Visual studio code has been the favorite text editor or IDE for web developers in recent years, and for a good reason. It could make your life as a developer way easier if you know how to leverage it's power to customize it to your needs.
I recently started using ejs as my template engine in my node/express app. I got tired of the clunky syntax if I need to inject javascript in it. I installed a couple of extensions to help me with it but I still had the same issues. I found out about custom snippets on vscode and immediately started making my own snippets for ejs. It is surprisingly easy to make. I've been writing faster ejs code ever since. I figured this could be useful to other new developers like me who just started using ejs. This is how I did it:

STEPS

  • Open your vscode and hit the Ctrl + Shift + p
  • In the search box, type "snippets"
  • Click on "Preferences: Configure User Snippets"
  • Click on "New Global Snippets file"
  • Name the file whatever you want (but I named mine "ejs.code-snippets") and click the enter key on your keyboard
  • Clear the file, copy and paste the code below
   {
    "ejs=": {
        "prefix": "ej=",
        "body": "<%= $1 %>",
        "description": "evaluates ejs code"
    },

    "ej": {
        "prefix": "ej",
        "body": "<% $1 %>",
        "description": "'Scriptlet' tag, for control-flow, no output"
    },

    "ej-": {
        "prefix": "ej-",
        "body": "<%- $1 %>",
        "description": "Outputs the unescaped value into the template"
    },

    "ejfor": {
        "prefix": "ejfor",
        "body": [
          "<% for (let $1 of $2){ %>",
          " $3",
          "<% } %>"
        ],
        "description": "creates ejs for of loop"
    }
}
Enter fullscreen mode Exit fullscreen mode

TEST

Create a new file and type "ej". You should see the snippets pop up. Click anyone of the ej snippets to see the use case.
Now you can type "ej=" and click enter and "<%= %>" will show up.

WRAPUP

I only made snippets for the most used ejs tags but feel free to add more to it or even customize it however you like.
Now you know how to create custom snippets

Top comments (0)