Animated Circular Progress Bar0
Font.Lokio
CLI

Template Syntax

Panduan lengkap syntax EJS yang bisa digunakan di template files


Template Syntax#

Template Lokio menggunakan format EJS (Embedded JavaScript). Kamu bisa menyisipkan variabel, kondisi, dan loop langsung di dalam kode template.


Dasar: Menyisipkan Variabel#

Gunakan <%= %> untuk menyisipkan nilai:

// Template
export class <%= name %> {}
 
// Input: name = "UserProfile"
// Hasil:
export class UserProfile {}

Transformasi Casing#

Ini fitur paling sering dipakai. Lokio menyediakan fungsi-fungsi untuk mengubah format penulisan variabel secara otomatis.

Tabel Lengkap#

FungsiInputHasil
<%= name %>user profileuser profile (apa adanya)
<%= Name %>user profileUser profile (shortcut capitalize)
<%= capitalize(name) %>user profileUser profile
<%= camelCase(name) %>user profileuserProfile
<%= pascalCase(name) %>user profileUserProfile
<%= kebabCase(name) %>user profileuser-profile
<%= snakeCase(name) %>user profileuser_profile
<%= constantCase(name) %>user profileUSER_PROFILE
<%= dotCase(name) %>user profileuser.profile
<%= pathCase(name) %>user profileuser/profile
<%= titleCase(name) %>user profileUser Profile
<%= sentenceCase(name) %>user profileUser profile
<%= noSpace(name) %>user profileuserprofile
<%= lowercase(name) %>User Profileuser profile
<%= uppercase(name) %>user profileUSER PROFILE
<%= plural(name) %>userusers
<%= singular(name) %>usersuser

Tips: Input bisa berupa format apapun — userProfile, user-profile, user_profile, atau User Profile — semua akan dikenali dan dikonversi dengan benar.

Contoh Nyata#

// Template (input: name = "product order")
 
import { Injectable } from '@nestjs/common';
 
// pascalCase → ProductOrderService
@Injectable()
export class <%= pascalCase(name) %>Service {
 
  // camelCase → productOrderService
  private readonly <%= camelCase(name) %>Repository;
 
  // constantCase → PRODUCT_ORDER_TABLE
  private readonly TABLE = '<%= constantCase(name) %>_TABLE';
 
  // kebabCase → product-order (untuk URL)
  getRoute() {
    return '/api/<%= kebabCase(name) %>';
  }
}

Hasil:

import { Injectable } from '@nestjs/common';
 
@Injectable()
export class ProductOrderService {
 
  private readonly productOrderRepository;
 
  private readonly TABLE = 'PRODUCT_ORDER_TABLE';
 
  getRoute() {
    return '/api/product-order';
  }
}

Kondisi (if / else)#

Gunakan <% if (...) { %> untuk blok kondisional.

Catatan: Blok <% %> (tanpa =) tidak menghasilkan output — hanya logika.

// Template (parameter: useAsync = boolean)
 
export class <%= pascalCase(name) %>Service {
 
<% if (useAsync === 'TRUE') { %>
  async findAll(): Promise<any[]> {
    return [];
  }
<% } else { %>
  findAll(): any[] {
    return [];
  }
<% } %>
 
}

Catatan: Parameter boolean dikonversi menjadi string "TRUE" atau "FALSE" di dalam template.

Input: name = "Order", useAsync = true

Hasil:

export class OrderService {
 
  async findAll(): Promise<any[]> {
    return [];
  }
 
}

Kondisi Berdasarkan Options#

Sangat berguna jika kamu punya parameter bertipe options (pilihan):

// Template (parameter: type = "http" | "grpc" | "queue")
 
<% if (type === 'http') { %>
import { Controller, Get } from '@nestjs/common';
 
@Controller('<%= kebabCase(name) %>')
export class <%= pascalCase(name) %>Controller {}
<% } else if (type === 'grpc') { %>
import { GrpcMethod } from '@nestjs/microservices';
 
export class <%= pascalCase(name) %>GrpcController {}
<% } else { %>
import { MessagePattern } from '@nestjs/microservices';
 
export class <%= pascalCase(name) %>Consumer {}
<% } %>

Loop (for)#

// Template (parameter: fields = "id,name,email" — dipisah koma)
 
export interface <%= pascalCase(name) %> {
<% fields.split(',').forEach(field => { %>
  <%= field.trim() %>: string;
<% }) %>
}

Input: name = "User", fields = "id, name, email"

Hasil:

export interface User {
  id: string;
  name: string;
  email: string;
}

Output Path Dinamis#

Kamu bisa pakai transformasi casing juga di output path di configs.yaml:

templates:
  - name: service
    output: src/services/<%= pascalCase(name) %>Service.ts
    #                     ↑ nama file otomatis PascalCase
Input nameOutput path
usersrc/services/UserService.ts
product ordersrc/services/ProductOrderService.ts
auth-tokensrc/services/AuthTokenService.ts

Komentar di Template#

Gunakan <%# %> untuk komentar — tidak akan muncul di output:

<%# Ini komentar template, tidak akan digenerate %>
export class <%= pascalCase(name) %> {
  <%# TODO: tambahkan method sesuai kebutuhan %>
}

Anotasi Lokio Check#

Anotasi khusus untuk lokio check — mendefinisikan aturan lint otomatis:

<%# @lokio required danger "Harus ada constructor" constructor %>
<%# @lokio forbidden warning "Jangan pakai console.log" console\.log %>
 
export class <%= pascalCase(name) %>Service {
  constructor() {}
}

Lihat Advanced Features → untuk penjelasan lengkap lokio check.


Tips & Trik#

Menghindari Whitespace Berlebih#

EJS bisa menghasilkan baris kosong dari tag <% %>. Gunakan - untuk strip whitespace:

<%- /* strip output */ %>

Atau atur template agar blok kondisi tidak menambah baris kosong:

export class Example {
<% if (withMethod) { %>  doSomething() {}
<% } %>}

Nested Transformasi#

// Menggabungkan plural + pascalCase
<%= pascalCase(plural(name)) %>
// Input: "product" → "Products"
// Input: "user order" → "UserOrders"

Gunakan Variabel JS di Template#

<%
  const className = pascalCase(name) + 'Service';
  const routeName = kebabCase(name);
%>
export class <%= className %> {
  route = '<%= routeName %>';
}