generator สร้าง route บนพื้นฐานของไฟล์ท่ีได้จัดการ
Synopsis
hexo.extend.generator.register(name, function(locals){ |
argument locals จะส่งเข้า function โดยมี site variables เข้าด้วยกัน ผู้ใช้สามารถใช้ argument นี้เพื่อได้ data ของเว็บไซต์ ด้งนั้นจะไม่ต้องเข้าถึง database โดยตรง
Update Routes
hexo.extend.generator.register('test', function(locals){ |
| Attribute | Description |
|---|---|
path |
Path not including the prefixing /. |
data |
Data |
layout |
Layout. Specify the layouts for rendering. The value can be a string or an array. If it’s ignored then the route will return data directly. |
เมื่ออัปเดท source file แฃ้ว hexo จะ execute generator ทั้งหมดและสร้างขึ้น route ใหม่ กรุณาอย่าเข้าถึง router โดยตรง
Example
Archive Page
สร้างเพจ archive ได้ท่ี archives/index.html โพสต์จะเป็นแบบ data ท่ีส่งเข้า template. data นี้คล้ายกับ variable page ของ template
ต่อไปจะตั้งค่า attribute ของ layout เพื่อ render theme template เฉพาะ ในตัวอย่างต่อไปจะตั้งค่า layout อย่างนี้: ถ้า layout archive ไม่มี จะใช้ layout index แทน
hexo.extend.generator.register('archive', function(locals){ |
Archive Page with Pagination
ผู้ใช้สามารถใช้เครื่องมือทางการ hexo-pagination อย่างสะดวกไปสร้างเพจ archive ท่ีมีหมายเลขหน้า
var pagination = require('hexo-pagination'); |
Generate All Posts
โพสต์ทั้งหมดจะมีอยู่ใน locals.posts ด้วยแล้วจะสร้าง route ให้โพสต์ทั้งหมด
hexo.extend.generator.register('post', function(locals){ |
Copy Files
คราวนี้ data จะเป็น function และ route fs.ReadStream ของ data จะสร้างขึ้นมาในเวลาที่ต้องการ
var fs = require('hexo-fs'); |