From beef97fd5da80d6d01227a2cab2d52c6650c75cf Mon Sep 17 00:00:00 2001 From: Gerasimos Maropoulos Date: Sat, 26 May 2018 22:49:48 +0300 Subject: [PATCH] Update vendor for Pug (Jade) Parser and add Iris + Pug examples in the _examples/view folder, relative to https://github.com/kataras/iris/issues/1003 Former-commit-id: e26a5701e00ec055f3bcf693c1980c7d22147310 --- _examples/README.md | 6 ++- _examples/README_ZH.md | 4 ++ _examples/view/template_pug_0/main.go | 29 +++++++++++++ .../view/template_pug_0/templates/index.pug | 25 +++++++++++ _examples/view/template_pug_1/main.go | 42 +++++++++++++++++++ .../view/template_pug_1/templates/index.pug | 20 +++++++++ _examples/view/template_pug_2/main.go | 28 +++++++++++++ .../view/template_pug_2/templates/footer.pug | 2 + .../view/template_pug_2/templates/header.pug | 4 ++ .../view/template_pug_2/templates/index.pug | 7 ++++ _examples/view/template_pug_3/main.go | 20 +++++++++ .../view/template_pug_3/templates/index.pug | 7 ++++ .../view/template_pug_3/templates/layout.pug | 7 ++++ view/pug.go | 6 +++ 14 files changed, 206 insertions(+), 1 deletion(-) create mode 100644 _examples/view/template_pug_0/main.go create mode 100644 _examples/view/template_pug_0/templates/index.pug create mode 100644 _examples/view/template_pug_1/main.go create mode 100644 _examples/view/template_pug_1/templates/index.pug create mode 100644 _examples/view/template_pug_2/main.go create mode 100644 _examples/view/template_pug_2/templates/footer.pug create mode 100644 _examples/view/template_pug_2/templates/header.pug create mode 100644 _examples/view/template_pug_2/templates/index.pug create mode 100644 _examples/view/template_pug_3/main.go create mode 100644 _examples/view/template_pug_3/templates/index.pug create mode 100644 _examples/view/template_pug_3/templates/layout.pug diff --git a/_examples/README.md b/_examples/README.md index 202f62ce..1dfa6953 100644 --- a/_examples/README.md +++ b/_examples/README.md @@ -243,7 +243,7 @@ func(c *ExampleController) Get() string | mvc.Result or (mvc.Result, error) ``` -where [mvc.Result](https://github.com/kataras/iris/blob/master/mvc/func_result.go) is an interface which contains only that function: `Dispatch(ctx iris.Context)`. +where [mvc.Result](https://github.com/kataras/iris/blob/master/mvc/go19.go#L10) is an [interface](https://github.com/kataras/iris/blob/master/hero/func_result.go#L18) which contains only that function: `Dispatch(ctx iris.Context)`. ## Using Iris MVC for code reuse @@ -295,6 +295,10 @@ Follow the examples below, - [Inject Data Between Handlers](view/context-view-data/main.go) - [Embedding Templates Into App Executable File](view/embedding-templates-into-app/main.go) - [Write to a custom `io.Writer`](view/write-to) +- [Greeting with Pug (Jade)`](view/template_pug_0) +- [Pug (Jade) Actions`](view/template_pug_1) +- [Pug (Jade) Includes`](view/template_pug_2) +- [Pug (Jade) Extends`](view/template_pug_3) You can serve [quicktemplate](https://github.com/valyala/quicktemplate) and [hero templates](https://github.com/shiyanhui/hero/hero) files too, simply by using the `context#ResponseWriter`, take a look at the [http_responsewriter/quicktemplate](http_responsewriter/quicktemplate) and [http_responsewriter/herotemplate](http_responsewriter/herotemplate) examples. diff --git a/_examples/README_ZH.md b/_examples/README_ZH.md index 02bee0c5..946f9a33 100644 --- a/_examples/README_ZH.md +++ b/_examples/README_ZH.md @@ -294,6 +294,10 @@ If you're new to back-end web development read about the MVC architectural patte - [Inject Data Between Handlers](view/context-view-data/main.go) - [Embedding Templates Into App Executable File](view/embedding-templates-into-app/main.go) - [Write to a custom `io.Writer`](view/write-to) +- [Greeting with Pug (Jade)`](view/template_pug_0) +- [Pug (Jade) Actions`](view/template_pug_1) +- [Pug (Jade) Includes`](view/template_pug_2) +- [Pug (Jade) Extends`](view/template_pug_3) You can serve [quicktemplate](https://github.com/valyala/quicktemplate) and [hero templates](https://github.com/shiyanhui/hero/hero) files too, simply by using the `context#ResponseWriter`, take a look at the [http_responsewriter/quicktemplate](http_responsewriter/quicktemplate) and [http_responsewriter/herotemplate](http_responsewriter/herotemplate) examples. diff --git a/_examples/view/template_pug_0/main.go b/_examples/view/template_pug_0/main.go new file mode 100644 index 00000000..1d5f35e6 --- /dev/null +++ b/_examples/view/template_pug_0/main.go @@ -0,0 +1,29 @@ +package main + +import "github.com/kataras/iris" + +func main() { + app := iris.New() + + tmpl := iris.Pug("./templates", ".pug") + tmpl.Reload(true) // reload templates on each request (development mode) + tmpl.AddFunc("greet", func(s string) string { // add your template func here. + return "Greetings " + s + "!" + }) + + app.RegisterView(tmpl) + + app.Get("/", index) + + // http://localhost:8080 + app.Run(iris.Addr(":8080")) +} + +func index(ctx iris.Context) { + ctx.ViewData("pageTitle", "My Index Page") + ctx.ViewData("youAreUsingJade", true) + // Q: why need extension .pug? + // A: Because you can register more than one view engine per Iris application. + ctx.View("index.pug") + +} diff --git a/_examples/view/template_pug_0/templates/index.pug b/_examples/view/template_pug_0/templates/index.pug new file mode 100644 index 00000000..e0f39238 --- /dev/null +++ b/_examples/view/template_pug_0/templates/index.pug @@ -0,0 +1,25 @@ +mixin withGo + | Generating Go html/template output. + +doctype html +html(lang="en") + head + title= .pageTitle + script(type='text/javascript'). + if (foo) { + bar(1 + 5) + } + body + h1 Jade - template engine + #container.col + if .youAreUsingJade + p {{ greet "iris user" }} + p You are amazing! + else + p Get on it! + p. + Jade is #[a(terse)] and simple + templating language with a + #[strong focus] on performance + and powerful features. + + withGo \ No newline at end of file diff --git a/_examples/view/template_pug_1/main.go b/_examples/view/template_pug_1/main.go new file mode 100644 index 00000000..7c266507 --- /dev/null +++ b/_examples/view/template_pug_1/main.go @@ -0,0 +1,42 @@ +// Package main shows an example of pug actions based on https://github.com/Joker/jade/tree/master/example/actions +package main + +import "github.com/kataras/iris" + +type Person struct { + Name string + Age int + Emails []string + Jobs []*Job +} + +type Job struct { + Employer string + Role string +} + +func main() { + app := iris.New() + + tmpl := iris.Pug("./templates", ".pug") + app.RegisterView(tmpl) + + app.Get("/", index) + + // http://localhost:8080 + app.Run(iris.Addr(":8080")) +} + +func index(ctx iris.Context) { + job1 := Job{Employer: "Monash B", Role: "Honorary"} + job2 := Job{Employer: "Box Hill", Role: "Head of HE"} + + person := Person{ + Name: "jan", + Age: 50, + Emails: []string{"jan@newmarch.name", "jan.newmarch@gmail.com"}, + Jobs: []*Job{&job1, &job2}, + } + + ctx.View("index.pug", person) +} diff --git a/_examples/view/template_pug_1/templates/index.pug b/_examples/view/template_pug_1/templates/index.pug new file mode 100644 index 00000000..c6b21de2 --- /dev/null +++ b/_examples/view/template_pug_1/templates/index.pug @@ -0,0 +1,20 @@ +doctype html +html(lang="en") + head + meta(charset="utf-8") + title Title + body + p ads + ul + li The name is {{.Name}}. + li The age is {{.Age}}. + + each _,_ in .Emails + div An email is {{.}} + + | {{ with .Jobs }} + each _,_ in . + div + An employer is {{.Employer}} + and the role is {{.Role}} + | {{ end }} \ No newline at end of file diff --git a/_examples/view/template_pug_2/main.go b/_examples/view/template_pug_2/main.go new file mode 100644 index 00000000..02e9571e --- /dev/null +++ b/_examples/view/template_pug_2/main.go @@ -0,0 +1,28 @@ +package main + +import ( + "html/template" + + "github.com/kataras/iris" +) + +func main() { + app := iris.New() + + tmpl := iris.Pug("./templates", ".pug") + tmpl.Reload(true) // reload templates on each request (development mode) + tmpl.AddFunc("bold", func(s string) (template.HTML, error) { // add your template func here. + return template.HTML("" + s + ""), nil + }) + + app.RegisterView(tmpl) + + app.Get("/", index) + + // http://localhost:8080 + app.Run(iris.Addr(":8080")) +} + +func index(ctx iris.Context) { + ctx.View("index.pug") +} diff --git a/_examples/view/template_pug_2/templates/footer.pug b/_examples/view/template_pug_2/templates/footer.pug new file mode 100644 index 00000000..b7f58893 --- /dev/null +++ b/_examples/view/template_pug_2/templates/footer.pug @@ -0,0 +1,2 @@ +#footer + p Copyright (c) foobar \ No newline at end of file diff --git a/_examples/view/template_pug_2/templates/header.pug b/_examples/view/template_pug_2/templates/header.pug new file mode 100644 index 00000000..a3f081ea --- /dev/null +++ b/_examples/view/template_pug_2/templates/header.pug @@ -0,0 +1,4 @@ +head + title My Site + \ No newline at end of file diff --git a/_examples/view/template_pug_2/templates/index.pug b/_examples/view/template_pug_2/templates/index.pug new file mode 100644 index 00000000..a006f865 --- /dev/null +++ b/_examples/view/template_pug_2/templates/index.pug @@ -0,0 +1,7 @@ +doctype html +html + include templates/header.pug + body + h1 My Site + p {{ bold "Welcome to my super lame site."}} + include templates/footer.pug \ No newline at end of file diff --git a/_examples/view/template_pug_3/main.go b/_examples/view/template_pug_3/main.go new file mode 100644 index 00000000..ff82bbf4 --- /dev/null +++ b/_examples/view/template_pug_3/main.go @@ -0,0 +1,20 @@ +package main + +import "github.com/kataras/iris" + +func main() { + app := iris.New() + + tmpl := iris.Pug("./templates", ".pug") + + app.RegisterView(tmpl) + + app.Get("/", index) + + // http://localhost:8080 + app.Run(iris.Addr(":8080")) +} + +func index(ctx iris.Context) { + ctx.View("index.pug") +} diff --git a/_examples/view/template_pug_3/templates/index.pug b/_examples/view/template_pug_3/templates/index.pug new file mode 100644 index 00000000..257f4a7c --- /dev/null +++ b/_examples/view/template_pug_3/templates/index.pug @@ -0,0 +1,7 @@ +extends templates/layout.pug + +block title + title Article Title + +block content + h1 My Article \ No newline at end of file diff --git a/_examples/view/template_pug_3/templates/layout.pug b/_examples/view/template_pug_3/templates/layout.pug new file mode 100644 index 00000000..ebd199f3 --- /dev/null +++ b/_examples/view/template_pug_3/templates/layout.pug @@ -0,0 +1,7 @@ +doctype html +html + head + block title + title Default title + body + block content \ No newline at end of file diff --git a/view/pug.go b/view/pug.go index ed72432a..5fd05f12 100644 --- a/view/pug.go +++ b/view/pug.go @@ -10,6 +10,12 @@ import ( // It has got some features and a lot of functions // which will make your life easier. // Read more about the Jade Go Template: https://github.com/Joker/jade +// +// Examples: +// https://github.com/kataras/iris/tree/master/_examples/view/template_pug_0 +// https://github.com/kataras/iris/tree/master/_examples/view/template_pug_1 +// https://github.com/kataras/iris/tree/master/_examples/view/template_pug_2 +// https://github.com/kataras/iris/tree/master/_examples/view/template_pug_3 func Pug(directory, extension string) *HTMLEngine { s := HTML(directory, extension) s.middleware = jade.Parse