Skip to content

Commit

Permalink
📝 chore: update README docs, add new github action script
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Feb 28, 2024
1 parent a2980b3 commit 8eb9140
Show file tree
Hide file tree
Showing 3 changed files with 217 additions and 13 deletions.
72 changes: 72 additions & 0 deletions .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
@@ -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
79 changes: 73 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)**
Expand All @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -140,7 +143,7 @@ templates/

- `templates/home.tpl`

```html
```gotemplate title="home.tpl"
<h1>Hello, {{ .Name | upper }}</h1>
<h2>At template {{ current_tpl }}</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>
Expand All @@ -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.
Expand All @@ -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"
<html lang="en">
<head>
<title>layout example</title>
</head>
<body>
{{ block "content" . }}
<h1>Hello, at base template</h1>
{{ end }}
</body>
</html>
```

`templates/home.tpl` contents:

```gotemplate title="home.tpl"
{{ extends "base" }}
{{ define "content" }}
<h1>Hello, {{ .Name | upper }}</h1>
<h2>At template {{ current_tpl }}</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>
{{ 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
Expand All @@ -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
Expand Down
79 changes: 72 additions & 7 deletions README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)**
Expand All @@ -15,6 +15,7 @@
- 支持布局文件渲染
- eg `{{ include "header" }} {{ yield }} {{ include "footer" }}`
- 支持引入其他模板 eg `{{ include "other" }}`
- 支持使用 `extends` 继承基础模板. eg `{{ extends "base.tpl" }}`
- 内置一些常用的模板方法 `row`, `lower`, `upper`, `join` ...

## GoDoc
Expand Down Expand Up @@ -81,7 +82,7 @@ func main() {

> 跟多API请参考 [GoDoc](https://pkg.go.dev/github.com/gookit/easytpl)
## 布局示例
## layout 布局示例

- `include` 包含其他模板

Expand Down Expand Up @@ -137,10 +138,10 @@ templates/
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>
```

### 使用
### 使用示例

```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.
Expand All @@ -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"
<html lang="en">
<head>
<title>layout example</title>
</head>
<body>
{{ block "content" . }}
<h1>Hello, at base template</h1>
{{ end }}
</body>
</html>
```

`templates/home.tpl, templates/about.tpl` 模板文件:

```gotemplate title="home.tpl"
{{ extends "base" }}
{{ define "content" }}
<h1>Hello, {{ .Name | upper }}</h1>
<h2>At template {{ current_tpl }}</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>
{{ 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
Expand Down Expand Up @@ -199,7 +264,7 @@ r.MustInit()
- 方法 3 (简单快捷)

```go
r := easytpl.NewInitialized(func (r *Renderer) {
r := easytpl.NewInited(func (r *Renderer) {
r.Layout = "layouts/default"
// ... ...
})
Expand Down

0 comments on commit 8eb9140

Please sign in to comment.