handlebars
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseWhen to Use
适用场景
Use this skill when:
- Creating or modifying template files
.hbs - Adding new Handlebars helpers or partials
- Debugging template rendering issues
- Understanding existing template patterns in Aurora CLI
- Working with dynamic code generation
在以下场景使用本技能:
- 创建或修改模板文件
.hbs - 添加新的Handlebars助手(helpers)或片段(partials)
- 调试模板渲染问题
- 理解Aurora CLI中的现有模板模式
- 处理动态代码生成
Critical Patterns
核心模式
Aurora CLI Template Engine Setup
Aurora CLI模板引擎配置
The template engine is configured in :
src/@cliter/utils/template-engine.ts- Uses library with
handlebarsaddon (189+ built-in helpers)handlebars-helpers - Custom helpers are registered in
src/@cliter/handlebars/helpers/ - Partials are registered in
src/@cliter/handlebars/partials/ - Templates are in (back/, front/, pipeline/)
src/templates/
Important: Aurora CLI includes ALL helpers from the package automatically. These 189+ helpers are available in every template without any additional configuration.
handlebars-helpers模板引擎在中配置:
src/@cliter/utils/template-engine.ts- 使用库及
handlebars扩展(包含189+内置助手)handlebars-helpers - 自定义助手在中注册
src/@cliter/handlebars/helpers/ - 片段(partials)在中注册
src/@cliter/handlebars/partials/ - 模板文件位于目录下(分为back/、front/、pipeline/子目录)
src/templates/
重要提示:Aurora CLI会自动包含包中的所有助手。这189+个助手无需额外配置即可在所有模板中使用。
handlebars-helpersHelper Registration Pattern
助手注册模式
typescript
// src/@cliter/handlebars/helpers/my-helper.ts
import * as handlebars from 'handlebars';
handlebars.registerHelper('myHelper', function(param1: string, param2: any, options)
{
// options.data.root contains all template data
// options.fn(this) executes block content
// options.inverse(this) executes else block
return result;
});typescript
// src/@cliter/handlebars/helpers/my-helper.ts
import * as handlebars from 'handlebars';
handlebars.registerHelper('myHelper', function(param1: string, param2: any, options)
{
// options.data.root包含所有模板数据
// options.fn(this)执行块内容
// options.inverse(this)执行else块内容
return result;
});Partial Registration Pattern
片段注册模式
typescript
// src/@cliter/handlebars/partials/my-partial.ts
import * as handlebars from 'handlebars';
handlebars.registerPartial('myPartial', `
{{#each items}}
{{ this.name }}
{{/each}}
`);typescript
// src/@cliter/handlebars/partials/my-partial.ts
import * as handlebars from 'handlebars';
handlebars.registerPartial('myPartial', `
{{#each items}}
{{ this.name }}
{{/each}}
`);Handlebars Syntax Guide
Handlebars语法指南
Basic Expressions
基础表达式
handlebars
handlebars
Whitespace Control
空白字符控制
handlebars
handlebars
Block Helpers
块级助手
handlebars
content if true
content if false
- - -
content if false
handlebars
条件为真时显示的内容
条件为假时显示的内容
- - -
条件为假时显示的内容
Custom Block Helpers (Aurora CLI)
自定义块级助手(Aurora CLI)
handlebars
content if equal
content if not equal
handlebars
值相等时显示的内容
值不相等时显示的内容
Aurora CLI Helper Categories
Aurora CLI助手分类
String Transformation Helpers
字符串转换助手
| Helper | Description | Example |
|---|---|---|
| Convert to camelCase | |
| Convert to PascalCase | |
| Convert to kebab-case | |
| Convert to snake_case | |
| Concatenate strings | |
| Remove line breaks | |
| 助手 | 说明 | 示例 |
|---|---|---|
| 转换为camelCase格式 | |
| 转换为PascalCase格式 | |
| 转换为kebab-case格式 | |
| 转换为snake_case格式 | |
| 字符串拼接 | |
| 移除换行符 | |
Variable Management Helpers
变量管理助手
handlebars
handlebars
Array/Object Helpers
数组/对象助手
handlebars
has items
handlebars
包含元素
Conditional Helpers
条件判断助手
handlebars
is undefined
not in array
handlebars
是undefined
不在数组中
Property Filter Helpers
属性过滤助手
Most property helpers filter :
schema.aggregatePropertieshandlebars
:
大多数属性助手用于过滤:
schema.aggregatePropertieshandlebars
:
Import Manager Helper
导入管理助手
handlebars
handlebars
ID Generation Helpers
ID生成助手
handlebars
handlebars
Handlebars-Helpers Package (Built-in)
Handlebars-Helpers包(内置)
Aurora CLI includes all 189+ helpers from the package. These are available automatically in all templates.
handlebars-helpersAurora CLI包含包中的所有189+个助手。这些助手可直接在所有模板中使用,无需额外配置。
handlebars-helpersString Helpers (36 helpers)
字符串助手(36个)
handlebars
is stringhandlebars
是字符串Array Helpers (28 helpers)
数组助手(28个)
handlebars
found
is array
has 5
at
handlebars
找到元素
是数组
包含5个元素
位于索引
Comparison Helpers (24 helpers)
比较助手(24个)
handlebars
both truthy
at least one truthy
falsey
equal
loosely equal
not equal
greater than
greater or equal
less than
less or equal
comparison
found
matches
even
odd
every 2nd
truthy
falsey
both falsey
not equal
not greater
not lesshandlebars
两者都为真
至少一个为真
为假
相等
松散相等
不相等
大于
大于等于
小于
小于等于
比较结果为真
找到元素
匹配模式
偶数
奇数
每第2个元素
为真值
为假值
两者都为假
不相等
不大于
不小于Math Helpers (16 helpers)
数学助手(16个)
handlebars
handlebars
Number Helpers (9 helpers)
数字助手(9个)
handlebars
handlebars
Object Helpers (14 helpers)
对象助手(14个)
handlebars
has key
is object
:
:
handlebars
包含该键
是对象
:
:
URL Helpers (9 helpers)
URL助手(9个)
handlebars
handlebars
Date Helpers (3 helpers)
日期助手(3个)
handlebars
handlebars
Path Helpers (8 helpers)
路径助手(8个)
handlebars
handlebars
Collection Helpers (2 helpers)
集合助手(2个)
handlebars
empty
handlebars
集合为空
Inflection Helpers (2 helpers)
词形变化助手(2个)
handlebars
handlebars
Regex Helpers (2 helpers)
正则表达式助手(2个)
handlebars
matcheshandlebars
匹配HTML Helpers (7 helpers)
HTML助手(7个)
handlebars
handlebars
Markdown Helpers (2 helpers)
Markdown助手(2个)
handlebars
handlebars
Misc Helpers (5 helpers)
其他助手(5个)
handlebars
handlebars
Logging Helpers (11 helpers)
日志助手(11个)
handlebars
Full Reference: See references/handlebars-helpers-reference.md for complete documentation.
handlebars
完整参考:请查看references/handlebars-helpers-reference.md获取完整文档。
Decision Tree
决策树
Need string transformation? → toCamelCase, toPascalCase, toKebabCase, toSnakeCase (Aurora)
→ camelcase, pascalcase, snakecase, dashcase (handlebars-helpers)
Need to store temporary data? → setVar
Need to build arrays/objects? → array, object, push
Need conditional rendering? → if, unless, eq, unlessEq, ternary, and, or, not, compare
Need to iterate? → each, loops, forEach, eachIndex, forIn, forOwn
Need to filter properties? → get*Properties helpers (Aurora custom)
Need to manage imports? → importManager with importsArray
Need unique IDs? → uuid, nanoid
Need math operations? → add, subtract, multiply, divide, ceil, floor, round
Need string manipulation? → trim, split, replace, truncate, append, prepend
Need array operations? → first, last, sort, unique, filter, map, join
Need object access? → get, hasOwn, forIn, forOwn
Need URL handling? → encodeURI, decodeURI, urlParse, stripQuerystring
Need path operations? → basename, dirname, extname, resolve需要字符串转换? → toCamelCase, toPascalCase, toKebabCase, toSnakeCase(Aurora自定义)
→ camelcase, pascalcase, snakecase, dashcase(handlebars-helpers内置)
需要存储临时数据? → setVar
需要构建数组/对象? → array, object, push
需要条件渲染? → if, unless, eq, unlessEq, ternary, and, or, not, compare
需要遍历? → each, loops, forEach, eachIndex, forIn, forOwn
需要过滤属性? → get*Properties系列助手(Aurora自定义)
需要管理导入? → importManager结合importsArray
需要生成唯一ID? → uuid, nanoid
需要数学运算? → add, subtract, multiply, divide, ceil, floor, round
需要字符串操作? → trim, split, replace, truncate, append, prepend
需要数组操作? → first, last, sort, unique, filter, map, join
需要对象访问? → get, hasOwn, forIn, forOwn
需要URL处理? → encodeURI, decodeURI, urlParse, stripQuerystring
需要路径操作? → basename, dirname, extname, resolveCode Examples
代码示例
Example 1: Creating a New Helper
示例1:创建新助手
typescript
// src/@cliter/handlebars/helpers/is-required-property.ts
import * as handlebars from 'handlebars';
import { Property } from '../../types';
handlebars.registerHelper('isRequiredProperty', function(
property: Property,
)
{
return !property.nullable && !property.hasOwnProperty('defaultValue');
});typescript
// src/@cliter/handlebars/helpers/is-required-property.ts
import * as handlebars from 'handlebars';
import { Property } from '../../types';
handlebars.registerHelper('isRequiredProperty', function(
property: Property,
)
{
return !property.nullable && !property.hasOwnProperty('defaultValue');
});Example 2: Using Multiple Helpers in Template
示例2:在模板中使用多个助手
handlebars
export interface Input
{
?: ;
}handlebars
export interface Input
{
?: ;
}Example 3: Complex Import Management
示例3:复杂导入管理
handlebars
handlebars
Example 4: Conditional Blocks with Context
示例4:带上下文的条件块
handlebars
// Handle enum type
// Handle other types
handlebars
// 处理枚举类型
// 处理其他类型
Commands
命令
bash
undefinedbash
undefinedBuild after adding helpers
添加助手后执行构建
yarn build
yarn build
Find all helpers
查找所有助手
ls src/@cliter/handlebars/helpers/
ls src/@cliter/handlebars/helpers/
Find helper usage in templates
在模板中查找助手的使用
grep -r "helperName" src/templates/
grep -r "helperName" src/templates/
Test template rendering
测试模板渲染
yarn test
---yarn test
---Common Mistakes to Avoid
常见错误规避
-
Missing whitespace control: Useto trim unwanted whitespace
~handlebarscontent content -
Wrong context access: Useto access parent context in loops
../handlebars -
Forgetting to register helper: Add import in
src/@cliter/handlebars/helpers/index.ts -
Usinginstead of
{{: For HTML/code output, use triple braces to avoid escaping{{{
-
遗漏空白字符控制:使用移除不必要的空白字符
~handlebarscontent content -
上下文访问错误:在循环中使用访问父上下文
../handlebars -
忘记注册助手:在中添加导入
src/@cliter/handlebars/helpers/index.ts -
错误使用而非
{{:对于HTML/代码输出,使用三重大括号避免转义{{{
Resources
资源
- Templates: See assets/ for helper and partial templates
- Aurora Helpers: See references/helpers-reference.md for Aurora CLI custom helpers
- Built-in Helpers: See references/handlebars-helpers-reference.md for handlebars-helpers package
- GitHub: https://github.com/helpers/handlebars-helpers
- 模板:查看assets/获取助手和片段模板
- Aurora助手:查看references/helpers-reference.md获取Aurora CLI自定义助手文档
- 内置助手:查看references/handlebars-helpers-reference.md获取handlebars-helpers包的完整文档
- GitHub:https://github.com/helpers/handlebars-helpers