Skip to content

Commit

Permalink
Add example of the powerful 'templ' generated templates
Browse files Browse the repository at this point in the history
  • Loading branch information
kataras committed Dec 13, 2023
1 parent 7ffe401 commit 9279ca5
Show file tree
Hide file tree
Showing 9 changed files with 359 additions and 8 deletions.
1 change: 1 addition & 0 deletions _examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@
* Third-Parties
* [Render `valyala/quicktemplate` templates](view/quicktemplate)
* [Render `shiyanhui/hero` templates](view/herotemplate)
* [Render `a-h/templ` templates](view/templ) **NEW**
* [Request ID](https://github.com/kataras/iris/blob/main/middleware/requestid/requestid_test.go)
* [Request Rate Limit](request-ratelimit/main.go)
* [Request Referrer](request-referrer/main.go)
Expand Down
55 changes: 55 additions & 0 deletions _examples/view/templ/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
module github.com/kataras/iris/v12/_examples/view/templ

go 1.21

replace github.com/kataras/iris/v12 => ../../../

require (
github.com/a-h/templ v0.2.476
github.com/kataras/iris/v12 v12.2.9-0.20231212220110-7ffe4010b957
)

require (
github.com/BurntSushi/toml v1.3.2 // indirect
github.com/CloudyKit/fastprinter v0.0.0-20200109182630-33d98a066a53 // indirect
github.com/CloudyKit/jet/v6 v6.2.0 // indirect
github.com/Joker/jade v1.1.3 // indirect
github.com/Shopify/goreferrer v0.0.0-20220729165902-8cddb4f5de06 // indirect
github.com/andybalholm/brotli v1.0.6 // indirect
github.com/aymerick/douceur v0.2.0 // indirect
github.com/fatih/structs v1.1.0 // indirect
github.com/flosch/pongo2/v4 v4.0.2 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/gomarkdown/markdown v0.0.0-20230922112808-5421fefb8386 // indirect
github.com/google/uuid v1.4.0 // indirect
github.com/gorilla/css v1.0.0 // indirect
github.com/iris-contrib/schema v0.0.6 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/kataras/blocks v0.0.8 // indirect
github.com/kataras/golog v0.1.11 // indirect
github.com/kataras/pio v0.0.13 // indirect
github.com/kataras/sitemap v0.0.6 // indirect
github.com/kataras/tunnel v0.0.4 // indirect
github.com/klauspost/compress v1.17.4 // indirect
github.com/mailgun/raymond/v2 v2.0.48 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/microcosm-cc/bluemonday v1.0.26 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/schollz/closestmatch v2.1.0+incompatible // indirect
github.com/sirupsen/logrus v1.8.1 // indirect
github.com/tdewolff/minify/v2 v2.20.9 // indirect
github.com/tdewolff/parse/v2 v2.7.6 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/vmihailenco/msgpack/v5 v5.4.1 // indirect
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
github.com/yosssi/ace v0.0.5 // indirect
golang.org/x/crypto v0.16.0 // indirect
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
golang.org/x/net v0.19.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/time v0.5.0 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
181 changes: 181 additions & 0 deletions _examples/view/templ/go.sum

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions _examples/view/templ/hello.templ
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package main

templ hello(name string) {
<div>Hello, <strong>{ name }</strong></div>
}
53 changes: 53 additions & 0 deletions _examples/view/templ/hello_templ.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions _examples/view/templ/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package main

import (
"github.com/kataras/iris/v12"
)

// 1.
// $ go install github.com/a-h/templ/cmd/templ@latest
// $ templ generate
// $ go run .
func main() {
component := hello("Makis")

app := iris.New()
app.Get("/", iris.Component(component))

app.Listen(":8080")
}
8 changes: 8 additions & 0 deletions aliases.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,14 @@ type (
FallbackViewLayout = context.FallbackViewLayout
)

// Component returns a new Handler which can be registered as a main handler for a route.
// It's a shortcut handler that renders the given component as HTML through Context.RenderComponent.
func Component(component context.Component) Handler {
return func(ctx Context) {
ctx.RenderComponent(component)
}
}

// PrefixDir returns a new FileSystem that opens files
// by adding the given "prefix" to the directory tree of "fs".
//
Expand Down
42 changes: 34 additions & 8 deletions context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package context

import (
"bytes"
stdContext "context"
"context"
"encoding/json"
"encoding/xml"
"errors"
Expand Down Expand Up @@ -78,7 +78,7 @@ type (
// BodyDecoderWithContext same as BodyDecoder but it can accept a standard context,
// which is binded to the HTTP request's context.
BodyDecoderWithContext interface {
DecodeContext(ctx stdContext.Context, data []byte) error
DecodeContext(ctx context.Context, data []byte) error
}

// Unmarshaler is the interface implemented by types that can unmarshal any raw data.
Expand Down Expand Up @@ -275,8 +275,8 @@ func IsErrCanceled(err error) bool {

var netErr net.Error
return (errors.As(err, &netErr) && netErr.Timeout()) ||
errors.Is(err, stdContext.Canceled) ||
errors.Is(err, stdContext.DeadlineExceeded) ||
errors.Is(err, context.Canceled) ||
errors.Is(err, context.DeadlineExceeded) ||
errors.Is(err, http.ErrHandlerTimeout) ||
err.Error() == "closed pool"
}
Expand Down Expand Up @@ -437,7 +437,7 @@ var acquireGoroutines = func() interface{} {
return &goroutines{wg: new(sync.WaitGroup)}
}
func (ctx *Context) Go(fn func(cancelCtx stdContext.Context)) (running int) {
func (ctx *Context) Go(fn func(cancelCtx context.Context)) (running int) {
g := ctx.values.GetOrSet(goroutinesContextKey, acquireGoroutines).(*goroutines)
if fn != nil {
g.wg.Add(1)
Expand All @@ -448,7 +448,7 @@ func (ctx *Context) Go(fn func(cancelCtx stdContext.Context)) (running int) {
ctx.waitFunc = g.wg.Wait
go func(reqCtx stdContext.Context) {
go func(reqCtx context.Context) {
fn(reqCtx)
g.wg.Done()
Expand Down Expand Up @@ -4259,6 +4259,10 @@ func (h ErrorHandlerFunc) HandleContextError(ctx *Context, err error) {
}

func (ctx *Context) handleContextError(err error) {
if err == nil {
return
}

if errHandler := ctx.app.GetContextErrorHandler(); errHandler != nil {
errHandler.HandleContextError(ctx, err)
} else {
Expand Down Expand Up @@ -4291,6 +4295,28 @@ func (ctx *Context) Render(statusCode int, r interface {
}
}

// Component is the interface which all components must implement.
// A component is a struct which can be rendered to a writer.
// It's being used by the `Context.RenderComponent` method.
// An example of compatible Component is a templ.Component.
type Component interface {
Render(context.Context, io.Writer) error
}

// RenderComponent renders a component to the client.
// It sets the "Content-Type" header to "text/html; charset=utf-8".
// It reports any component render errors back to the caller.
// Look the Application.SetContextErrorHandler to override the
// default status code 500 with a custom error response.
func (ctx *Context) RenderComponent(component Component) error {
ctx.ContentType("text/html; charset=utf-8")
err := component.Render(ctx.Request().Context(), ctx.ResponseWriter())
if err != nil {
ctx.handleContextError(err)
}
return err
}

// JSON marshals the given "v" value to JSON and writes the response to the client.
// Look the Configuration.EnableProtoJSON and EnableEasyJSON too.
//
Expand Down Expand Up @@ -5387,7 +5413,7 @@ func (ctx *Context) ServeContent(content io.ReadSeeker, filename string, modtime
// represents one byte. See "golang.org/x/time/rate" package.
type rateReadSeeker struct {
io.ReadSeeker
ctx stdContext.Context
ctx context.Context
limiter *rate.Limiter
}

Expand Down Expand Up @@ -6460,7 +6486,7 @@ func (ctx *Context) User() User {
}

// Ensure Iris Context implements the standard Context package, build-time.
var _ stdContext.Context = (*Context)(nil)
var _ context.Context = (*Context)(nil)

// Deadline returns the time when work done on behalf of this context
// should be canceled. Deadline returns ok==false when no deadline is
Expand Down
4 changes: 4 additions & 0 deletions core/router/api_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -1563,6 +1563,10 @@ type (
//
// app.HandleServer("/api/identity", otherApp)
func (api *APIBuilder) HandleServer(path string, server ServerHandler) {
if server == nil {
return
}

if app, ok := server.(serverBuilder); ok {
// Do an extra check for Build() error at any case
// the end-developer didn't call Build before.
Expand Down

0 comments on commit 9279ca5

Please sign in to comment.