diff --git a/.github/workflows/codeql.yml b/.github/workflows/codeql.yml new file mode 100644 index 0000000..4a8bbb5 --- /dev/null +++ b/.github/workflows/codeql.yml @@ -0,0 +1,72 @@ +# For most projects, this workflow file will not need changing; you simply need +# to commit it to your repository. +# +# You may wish to alter this file to override the set of languages analyzed, +# or to provide custom queries or build logic. +# +# ******** NOTE ******** +# We have attempted to detect the languages in your repository. Please check +# the `language` matrix defined below to confirm you have the correct set of +# supported CodeQL languages. +# +name: "CodeQL" + +on: + push: + branches: [ "master" ] + pull_request: + # The branches below must be a subset of the branches above + branches: [ "master" ] + schedule: + - cron: '40 14 * * 6' + +jobs: + analyze: + name: Analyze + runs-on: ubuntu-latest + permissions: + actions: read + contents: read + security-events: write + + strategy: + fail-fast: false + matrix: + language: [ 'go' ] + # CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python', 'ruby' ] + # Learn more about CodeQL language support at https://aka.ms/codeql-docs/language-support + + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + # Initializes the CodeQL tools for scanning. + - name: Initialize CodeQL + uses: github/codeql-action/init@v3 + with: + languages: ${{ matrix.language }} + # If you wish to specify custom queries, you can do so here or in a config file. + # By default, queries listed here will override any specified in a config file. + # Prefix the list here with "+" to use these queries and those in the config file. + + # Details on CodeQL's query packs refer to : https://docs.github.com/en/code-security/code-scanning/automatically-scanning-your-code-for-vulnerabilities-and-errors/configuring-code-scanning#using-queries-in-ql-packs + # queries: security-extended,security-and-quality + + + # Autobuild attempts to build any compiled languages (C/C++, C#, or Java). + # If this step fails, then you should remove it and run the build manually (see below) + - name: Autobuild + uses: github/codeql-action/autobuild@v3 + + # ℹ️ Command-line programs to run using the OS shell. + # 📚 See https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsrun + + # If the Autobuild fails above, remove it and uncomment the following three lines. + # modify them (or add more) to build your code if your project, please refer to the EXAMPLE below for guidance. + + # - run: | + # echo "Run, Build Application using script" + # ./location_of_script_within_repo/buildscript.sh + + - name: Perform CodeQL Analysis + uses: github/codeql-action/analyze@v3 diff --git a/README.md b/README.md index e5b60f8..75e548b 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,8 @@ [![Go Report Card](https://goreportcard.com/badge/github.com/gookit/easytpl)](https://goreportcard.com/report/github.com/gookit/easytpl) [![Unit-Tests](https://github.com/gookit/easytpl/workflows/Unit-Tests/badge.svg)](https://github.com/gookit/easytpl/actions) -A simple template renderer based on the `html/template`, but much simpler to use. Support layout rendering, including templates. +A simple template renderer based on the Go `html/template`, but much simpler to use. +Support layout rendering, including templates. > **[中文说明](README.zh-CN.md)** @@ -17,6 +18,8 @@ A simple template renderer based on the `html/template`, but much simpler to use - support layout render. - eg `{{ include "header" }} {{ yield }} {{ include "footer" }}` - support include other templates. eg `{{ include "other" }}` +- support `extends` base templates. eg `{{ extends "base.tpl" }}` +- support custom template functions - built-in some helper methods `row`, `lower`, `upper`, `join` ... ## Godoc @@ -80,7 +83,7 @@ func main() { } ``` -> more APIS please [GoDoc](https://pkg.go.dev/github.com/gookit/easytpl) +> more APIs please [GoDoc](https://pkg.go.dev/github.com/gookit/easytpl) ## Layout Example @@ -140,7 +143,7 @@ templates/ - `templates/home.tpl` -```html +```gotemplate title="home.tpl"
Lorem ipsum dolor sit amet, consectetur adipisicing elit.
@@ -149,7 +152,7 @@ templates/ ### Usage ```go -v := easytpl.NewInitialized(func(r *easytpl.Renderer) { +v := easytpl.NewInited(func(r *easytpl.Renderer) { // setting default layout r.Layout = "layouts/default" // equals to "layouts/default.tpl" // templates dir. will auto load on init. @@ -159,10 +162,74 @@ v := easytpl.NewInitialized(func(r *easytpl.Renderer) { http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { v.Render(w, "home", easytpl.M{"Name": "tom"}) }) -log.Println("Listening port: 9100") +slog.Println("Listening port: 9100") http.ListenAndServe(":9100", nil) ``` +## `extends` example + +A base template can be inherited using the `{{ extends "base.tpl" }}` statement. + +> Note: The `extends` statement must be on the first line of the template file + +```text +templates/ + |_ base.tpl + |_ home.tpl + |_ about.tpl +``` + +`templates/base.tpl` base template contents: + +```gotemplate title="base.tpl" + + +Lorem ipsum dolor sit amet, consectetur adipisicing elit.
+{{ end }} +``` + +### Usage + +```go +package main + +import ( + "net/http" + + "github.com/gookit/easytpl" + "github.com/gookit/slog" +) + +func main() { + v := easytpl.NewExtends(easytpl.WithTplDirs("templates")) + + http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + v.Render(w, "home", easytpl.M{"Name": "tom"}) + }) + + slog.Info("Listening port: 9100") + http.ListenAndServe(":9100", nil) +} +``` + ## Available Options ```go @@ -172,7 +239,7 @@ Debug bool Layout string // Delims define for template Delims TplDelims -// ViewsDir the default views directory +// ViewsDir the default views directory, multi use "," split ViewsDir string // ExtNames allowed template extensions. eg {"tpl", "html"} ExtNames []string diff --git a/README.zh-CN.md b/README.zh-CN.md index 0fae177..293a6f0 100644 --- a/README.zh-CN.md +++ b/README.zh-CN.md @@ -5,7 +5,7 @@ [![Go Report Card](https://goreportcard.com/badge/github.com/gookit/easytpl)](https://goreportcard.com/report/github.com/gookit/easytpl) [![Unit-Tests](https://github.com/gookit/easytpl/workflows/Unit-Tests/badge.svg)](https://github.com/gookit/easytpl/actions) -一个简单的视图渲染器,基于golang `html/template` 封装,但更加简单易用。 +一个简单的模板渲染器,基于Golang `html/template` 封装,但更加简单易用。 > **[EN README](README.md)** @@ -15,6 +15,7 @@ - 支持布局文件渲染 - eg `{{ include "header" }} {{ yield }} {{ include "footer" }}` - 支持引入其他模板 eg `{{ include "other" }}` +- 支持使用 `extends` 继承基础模板. eg `{{ extends "base.tpl" }}` - 内置一些常用的模板方法 `row`, `lower`, `upper`, `join` ... ## GoDoc @@ -81,7 +82,7 @@ func main() { > 跟多API请参考 [GoDoc](https://pkg.go.dev/github.com/gookit/easytpl) -## 布局示例 +## layout 布局示例 - `include` 包含其他模板 @@ -137,10 +138,10 @@ templates/Lorem ipsum dolor sit amet, consectetur adipisicing elit.
``` -### 使用 +### 使用示例 ```go -v := easytpl.NewInitialized(func(r *easytpl.Renderer) { +v := easytpl.NewInited(func(r *easytpl.Renderer) { // setting default layout r.Layout = "layouts/default" // equals to "layouts/default.tpl" // templates dir. will auto load on init. @@ -150,16 +151,80 @@ v := easytpl.NewInitialized(func(r *easytpl.Renderer) { http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { v.Render(w, "home", easytpl.M{"Name": "tom"}) }) -log.Println("Listening port: 9100") + +slog.Println("Listening port: 9100") http.ListenAndServe(":9100", nil) ``` +## `extends` 继承示例 + +可以使用 `extends` 语句继承基础模板. eg `{{ extends "base.tpl" }}` + +> 注意: `extends` 语句必须在模板文件的第一行 + +```text +templates/ + |_ base.tpl + |_ home.tpl + |_ about.tpl +``` + +`templates/base.tpl` 基础模板文件: + +```gotemplate title="base.tpl" + + +Lorem ipsum dolor sit amet, consectetur adipisicing elit.
+{{ end }} +``` + +### 使用示例 + +```go +package main + +import ( + "net/http" + + "github.com/gookit/easytpl" + "github.com/gookit/slog" +) + +func main() { + v := easytpl.NewExtends(easytpl.WithTplDirs("templates")) + + http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + v.Render(w, "home", easytpl.M{"Name": "tom"}) + }) + + slog.Info("Listening port: 9100") + http.ListenAndServe(":9100", nil) +} +``` + ## 可用选项 ```go // 开启调试 Debug bool -// 默认的视图模板目录 +// 默认的视图模板目录,可以是多个目录,使用逗号分隔 ViewsDir string // 布局模板名称 Layout string @@ -199,7 +264,7 @@ r.MustInit() - 方法 3 (简单快捷) ```go -r := easytpl.NewInitialized(func (r *Renderer) { +r := easytpl.NewInited(func (r *Renderer) { r.Layout = "layouts/default" // ... ... })