template

package
v2.4.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 28, 2023 License: MIT Imports: 2 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetAllTmplName

func GetAllTmplName(
	ReadFile func(name string) ([]byte, error),
	filePath string,
	allTmpl []string,
) (filterTmpl []string, err error)

GetAllTmplName 獲取文件中會使用到的所有樣板名稱(子嵌套也都會遞迴尋找)

Example
package main

import (
	"embed"
	"fmt"

	filepath2 "github.com/CarsonSlovoka/go-pkg/v2/path/filepath"
	"github.com/CarsonSlovoka/go-pkg/v2/tpl/template"

	htmlTemplate "html/template"
	"os"
	"path/filepath"
)

//go:embed testdata/tmpl/*
//go:embed testdata/pages/*
var dataFS embed.FS

func main() {
	tplList, err := filepath2.CollectFilesFromFS(dataFS, []string{"testdata/tmpl"}, nil, true)
	if err != nil {
		panic(err)
	}

	var filterTmpl []string
	src := "testdata/pages/demo.gohtml" // 當前所要渲染的頁面
	filterTmpl, err = template.GetAllTmplName(dataFS.ReadFile, src, tplList)
	if err != nil {
		panic(err)
	}
	filterTmpl = append(filterTmpl, src) // 當前的文件也要包含
	fmt.Println(len(filterTmpl))

	// render
	_ = func() {
		tmpl := htmlTemplate.New(filepath.Base(src)).Funcs(nil)
		tmpl, err = tmpl.ParseFS(dataFS, filterTmpl...)
		// tmpl, err = tmpl.ParseFiles(filterTmpl...)
		if err = tmpl.Execute(os.Stdout, nil); err != nil {
			panic(err)
		}
	}
}
Output:

5

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL