From 26a107142726755a2e2f80462d14bf2654a05129 Mon Sep 17 00:00:00 2001 From: "Gerasimos (Makis) Maropoulos" Date: Thu, 4 Jul 2019 19:19:24 +0300 Subject: [PATCH] add the View engine and move the DI chapter outside of the Routing one --- Home.md | 5 +- MVC.md | 0 View.md | 148 ++++++++++++++++++ _Sidebar.md | 4 +- ...cy-injection.md => dependency-injection.md | 0 5 files changed, 155 insertions(+), 2 deletions(-) create mode 100644 MVC.md create mode 100644 View.md rename Routing-dependency-injection.md => dependency-injection.md (100%) diff --git a/Home.md b/Home.md index 135520b..e9158b8 100644 --- a/Home.md +++ b/Home.md @@ -18,11 +18,14 @@ This wiki is the main source of documentation for **developers** working with (o * [[Subdomains|Routing-subdomains]] * [[Wrap the Router|Routing-wrap-the-router]] * [[Override Context|Routing-override-context]] - * [[Dependency Injection|Routing-dependency-injection]] * [[API Versioning]] * [[File Server]] +* [[View]] +* [[Dependency Injection|dependency-injection]] +* [[MVC]] **TODO chapter** * [[Cookies]] + > This new wiki is a `Work In Progress` in an effort to centralize the documentation and concepts explanation into a single place. ## Runnable Examples diff --git a/MVC.md b/MVC.md new file mode 100644 index 0000000..e69de29 diff --git a/View.md b/View.md new file mode 100644 index 0000000..eb9ea3c --- /dev/null +++ b/View.md @@ -0,0 +1,148 @@ +Iris offers support for **6 template parsers out of the box** through its universal [View Engine](https://godoc.org/github.com/kataras/iris/view#Engine). Of course developers can stil use various go template parsers as the `Context.ResponseWriter()` completes the `http.ResponseWriter` and `io.Writer`. + +Iris puts some common rules and features that their original parsers don't support by default. For example we have support for `yield`, `render`, `render_r`, `current`, `urlpath` template funcs and `Layouts` and `binding` accross middlewares and **embedded template files** for all Engines. + +To use the unique features of each template you have to read and learn by reading their documentation. Choose what fits your app's needs the most. + +Let's see the list of the built-in view engines: + +| Engine | Declaration | Underline Template Parser +| -----------|-------------|-------------| +| std template/html | `iris.HTML(...)` | [html/template](https://golang.org/pkg/html/template/) package | +| django | `iris.Django(...)` | [flosch/pongo2](https://github.com/flosch/pongo2) package | +| handlebars | `iris.Handlebars(...)` | [Joker/jade](https://github.com/Joker/jade) package | +| amber | `iris.Amber(...)` | [aymerick/raymond](https://github.com/aymerick/raymond) package | +| pug(jade) | `iris.Pug(...)` | [eknkc/amber](https://github.com/eknkc/amber) package | +| jet | `iris.Jet(...)` | [CloudyKit/jet](https://github.com/CloudyKit/jet) package | + +One or more view engines can be registered in the same Application. To **register** a view engine use the `RegisterView(ViewEngine)` method. + +Load all templates from the "./views" folder +where extension is ".html" and parse them +using the standard `html/template` package. + +```go +// [app := iris.New...] +tmpl := iris.HTML("./views", ".html") +app.RegisterView(tmpl) +``` + +To **render or execute** a view use the `Context.View` method inside the main route's handler. + +```go +ctx.View("hi.html") +``` + +To **bind** Go values with key-value pattern inside a view through middleware or main handler use the `Context.ViewData` method before the `Context.View` one. + +Bind: `{{.message}}` with `"Hello world!"`. + +```go +ctx.ViewData("message", "Hello world!") +``` + +To bind a Go **model** to a view you have two options: + +- `ctx.ViewData("user", User{})` - variable binding as `{{.user.Name}}` for example +- `ctx.View("hello.html", User{})` - root binding as `{{.Name}}` for example. + +To **add a template function** use the `AddFunc` method of the preferred view engine. + +```go +// func name, input arguments, render value +tmpl.AddFunc("greet", func(s string) string { + return "Greetings " + s + "!" +}) +``` + +To **reload on local file changes** call the view enginne's `Reload` method. + +```go +tmpl.Reload(true) +``` + +To use **embedded** files and not depend on local file system use the [go-bindata](https://github.com/go-bindata/go-bindata) external tool and pass its `Asset` and `AssetNames` functions to the `Binary` method of the preferred view engine. + +```go +tmpl.Binary(Asset, AssetNames) +``` + +Example Code: + +Please read the _comments_ too. + +```go +// file: main.go +package main + +import "github.com/kataras/iris" + +func main() { + app := iris.New() + + // Parse all templates from the "./views" folder + // where extension is ".html" and parse them + // using the standard `html/template` package. + tmpl := iris.HTML("./views", ".html") + + // Enable re-build on local template files changes. + tmpl.Reload(true) + + // Default template funcs are: + // + // - {{ urlpath "myNamedRoute" "pathParameter_ifNeeded" }} + // - {{ render "header.html" }} + // and partial relative path to current page: + // - {{ render_r "header.html" }} + // - {{ yield }} + // - {{ current }} + // Register a custom template func: + tmpl.AddFunc("greet", func(s string) string { + return "Greetings " + s + "!" + }) + + // Register the view engine to the views, + // this will load the templates. + app.RegisterView(tmpl) + + // Method: GET + // Resource: http://localhost:8080 + app.Get("/", func(ctx iris.Context) { + // Bind: {{.message}} with "Hello world!" + ctx.ViewData("message", "Hello world!") + // Render template file: ./views/hi.html + ctx.View("hi.html") + }) + + app.Run(iris.Addr(":8080")) +} +``` + +```html + + + + Hi Page + + +

{{.message}}

+ {{greet "to you"}} + + +``` + +Open a browser tab at http://localhost:8080. + +The **rendered result** will look like this: + +```html + + + Hi Page + + +

Hello world!

+ Greetings to you! + + +``` diff --git a/_Sidebar.md b/_Sidebar.md index 9f15200..313a1ec 100644 --- a/_Sidebar.md +++ b/_Sidebar.md @@ -13,7 +13,9 @@ * [[Subdomains|Routing-subdomains]] * [[Wrap the Router|Routing-wrap-the-router]] * [[Override Context|Routing-override-context]] - * [[Dependency Injection|Routing-dependency-injection]] * [[API Versioning]] * [[File Server]] +* [[View]] +* [[Dependency Injection|dependency-injection]] +* [[MVC]] **TODO chapter** * [[Cookies]] diff --git a/Routing-dependency-injection.md b/dependency-injection.md similarity index 100% rename from Routing-dependency-injection.md rename to dependency-injection.md