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#
| Fungsi | Input | Hasil |
|---|---|---|
<%= name %> | user profile | user profile (apa adanya) |
<%= Name %> | user profile | User profile (shortcut capitalize) |
<%= capitalize(name) %> | user profile | User profile |
<%= camelCase(name) %> | user profile | userProfile |
<%= pascalCase(name) %> | user profile | UserProfile |
<%= kebabCase(name) %> | user profile | user-profile |
<%= snakeCase(name) %> | user profile | user_profile |
<%= constantCase(name) %> | user profile | USER_PROFILE |
<%= dotCase(name) %> | user profile | user.profile |
<%= pathCase(name) %> | user profile | user/profile |
<%= titleCase(name) %> | user profile | User Profile |
<%= sentenceCase(name) %> | user profile | User profile |
<%= noSpace(name) %> | user profile | userprofile |
<%= lowercase(name) %> | User Profile | user profile |
<%= uppercase(name) %> | user profile | USER PROFILE |
<%= plural(name) %> | user | users |
<%= singular(name) %> | users | user |
Tips: Input bisa berupa format apapun —
userProfile,user-profile,user_profile, atauUser 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 PascalCaseInput name | Output path |
|---|---|
user | src/services/UserService.ts |
product order | src/services/ProductOrderService.ts |
auth-token | src/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 %>';
}