mirror of
https://github.com/kataras/iris.git
synced 2025-12-31 16:57:04 +00:00
add an app.View example for parsing and writing templates outside of the HTTP scope(context.View)
Former-commit-id: e65d8ece521c778dedf45cf2f522383c26b9901b
This commit is contained in:
@@ -294,7 +294,7 @@ Follow the examples below,
|
||||
- [The `url` tmpl func](view/template_html_4/main.go)
|
||||
- [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)
|
||||
|
||||
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.
|
||||
|
||||
|
||||
43
_examples/view/write-to/main.go
Normal file
43
_examples/view/write-to/main.go
Normal file
@@ -0,0 +1,43 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"github.com/kataras/iris"
|
||||
)
|
||||
|
||||
type mailData struct {
|
||||
Title string
|
||||
Body string
|
||||
RefTitle string
|
||||
RefLink string
|
||||
}
|
||||
|
||||
func main() {
|
||||
app := iris.New()
|
||||
app.Logger().SetLevel("debug")
|
||||
app.RegisterView(iris.HTML("./views", ".html"))
|
||||
|
||||
// you need to call `app.Build` manually before using the `app.View` func,
|
||||
// so templates are built in that state.
|
||||
app.Build()
|
||||
|
||||
// Or a string-buffered writer to use its body to send an e-mail
|
||||
// for sending e-mails you can use the https://github.com/kataras/go-mailer
|
||||
// or any other third-party package you like.
|
||||
//
|
||||
// The template's parsed result will be written to that writer.
|
||||
writer := os.Stdout
|
||||
err := app.View(writer, "email/simple.html", "shared/email.html", mailData{
|
||||
Title: "This is my e-mail title",
|
||||
Body: "This is my e-mail body",
|
||||
RefTitle: "Iris web framework",
|
||||
RefLink: "https://iris-go.com",
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
app.Logger().Errorf("error from app.View: %v", err)
|
||||
}
|
||||
|
||||
app.Run(iris.Addr(":8080"))
|
||||
}
|
||||
1
_examples/view/write-to/views/email/simple.html
Normal file
1
_examples/view/write-to/views/email/simple.html
Normal file
@@ -0,0 +1 @@
|
||||
{{.Body}}
|
||||
6
_examples/view/write-to/views/shared/email.html
Normal file
6
_examples/view/write-to/views/shared/email.html
Normal file
@@ -0,0 +1,6 @@
|
||||
<h1>{{.Title}}</h1>
|
||||
<p class="body">
|
||||
{{yield}}
|
||||
</p>
|
||||
|
||||
<a href="{{.RefLink}}" target="_new">{{.RefTitle}}</a>
|
||||
Reference in New Issue
Block a user