Software Development

แนะนำ Templ

Templ เป็น template engine สำหรับ GoLang ที่ช่วยให้เราทำงานกับ html ไฟล์ได้สะดวกขึ้น
แนะนำ Templ
Share this

Templ เป็น template engine สำหรับ GoLang ที่ช่วยให้เราทำงานกับ html ไฟล์ได้สะดวกขึ้น


Templ

Templ ช่วยให้เราสามารถเขียน web application ด้วย GoLang ได้สะดวกขึ้น โดยหลักการทำงานคือเราจะสามารถสร้าง template file แล้วใช้ Templ ในการแปลง (compile) จาก template file ให้กลายเป็น .go ไฟล์ และเรียกใช้ในโปรแกรมของเราได้เลย

ข้อดีของ Templ

  • เขียน html template ได้ง่าย เพราะสามารถแทรกคำสั่งของ GoLang เข้าไปใน html ได้เลย (คล้ายกับ PHP)
  • รองรับการเขียน template ไฟล์เป็น component เพื่อให้นำกลับมาใช้ซ้ำได้ง่าย
  • ไม่จำเป็นต้องมีการ parse template files เหมือนการใช้ template engine แบบอื่น
  • html page ที่ได้จะถูกรวมเข้าไปกับ binary ของ Go เลย ไม่จำเป็นต้อง deploy html ต่างหาก

เนื่องจากต้องมีการ compile tmpl ไฟล์ต่างหากก่อนที่จะรันโปรแกรม Go จึงมีขั้นตอนเพิ่มขึ้น แต่เราสามารถแก้ปัญหาด้วยการใช้ Air มาช่วยในการ compile Templ และรัน Go ในขณะ dev ได้โดยอัตโนมัติ สามารถอ่านรายละเอียดเพิ่มเติมได้ ที่นี่

ตัวอย่าง Templ ไฟล์: hello.templ

package templates

templ Hello() {
  @Header()
  for range 5 {
    <h1 style="color: red;">Hello, Boon! 5555</h1>
  }
  @Footer()
}

ตัวอย่าง Templ ไฟล์: hello_templ.go ที่ถูกสร้างขึ้นโดยอัตโนมัติ

// Code generated by templ - DO NOT EDIT.

// templ: version: v0.3.819
package templates

//lint:file-ignore SA4006 This context is only used if a nested component is present.

import "github.com/a-h/templ"
import templruntime "github.com/a-h/templ/runtime"

func Header() templ.Component {
    return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
        templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
        if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
            return templ_7745c5c3_CtxErr
        }
        templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
        if !templ_7745c5c3_IsBuffer {
            defer func() {
                templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
                if templ_7745c5c3_Err == nil {
                    templ_7745c5c3_Err = templ_7745c5c3_BufErr
                }
            }()
        }
        ctx = templ.InitializeContext(ctx)
        templ_7745c5c3_Var1 := templ.GetChildren(ctx)
        if templ_7745c5c3_Var1 == nil {
            templ_7745c5c3_Var1 = templ.NopComponent
        }
        ctx = templ.ClearChildren(ctx)
        templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 1, "<h1>Header</h1><hr>")
        if templ_7745c5c3_Err != nil {
            return templ_7745c5c3_Err
        }
        return nil
    })
}

var _ = templruntime.GeneratedTemplate

คำแนะนำ

  1. เราสามารถใช้ Air package ในการ compile และ build Go application เพื่อความสะดวกในการพัฒนา
  2. ควรกำหนด .gitignore ที่เหมาะสม เช่น *_templ.go
  3. การค้นหาข้อมูลเกี่ยวกับ Templ ในอินเทอร์เน็ตอาจจะหายาก เพราะคำว่า templ เป็นคำที่ค่อนข้างทั่วไป แนะนำว่าเวลาค้นหาให้ใช้ keyword ว่า a-h templ เพราะ a-h เป็นชื่อ account ใน github ของผู้พัฒนา
Post Views: 320