DEV Community

Jamiu
Jamiu

Posted on

5 2

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

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read full post →

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more