-
-
Notifications
You must be signed in to change notification settings - Fork 277
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for rendering raw HTML, and bi-directional support …
- Loading branch information
Showing
18 changed files
with
540 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
0.2.499 | ||
0.2.500 |
89 changes: 89 additions & 0 deletions
89
docs/docs/03-syntax-and-usage/14-using-with-go-templates.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
# Using with `html/template` | ||
|
||
Templ components can be used with the Go standard library [`html/template`](https://pkg.go.dev/html/template) package. | ||
|
||
## Using `html/template` in a templ component | ||
|
||
To use an existing `html/template` in a templ component, use the `templ.FromGoHTML` function. | ||
|
||
```templ title="component.templ" | ||
package testgotemplates | ||
import "html/template" | ||
var goTemplate = template.Must(template.New("example").Parse("<div>{{ . }}</div>")) | ||
templ Example() { | ||
<!DOCTYPE html> | ||
<html> | ||
<body> | ||
@templ.FromGoHTML(goTemplate, "Hello, World!") | ||
</body> | ||
</html> | ||
} | ||
``` | ||
|
||
```go title="main.go" | ||
func main() { | ||
Example.Render(context.Background(), os.Stdout) | ||
} | ||
``` | ||
|
||
```html title="Output" | ||
<!DOCTYPE html> | ||
<html> | ||
<body> | ||
<div>Hello, World!</div> | ||
</body> | ||
</html> | ||
``` | ||
|
||
## Using a templ component with `html/template` | ||
|
||
To use a templ component within a `html/template`, use the `templ.ToGoHTML` function to render the component into a `template.HTML value`. | ||
|
||
```templ title="component.html" | ||
package testgotemplates | ||
import "html/template" | ||
var example = template.Must(template.New("example").Parse(`<!DOCTYPE html> | ||
<html> | ||
<body> | ||
{{ . }} | ||
</body> | ||
</html> | ||
`)) | ||
templ greeting() { | ||
<div>Hello, World!</div> | ||
} | ||
``` | ||
|
||
```go title="main.go" | ||
func main() { | ||
// Create the templ component. | ||
templComponent := greeting() | ||
|
||
// Render the templ component to a `template.HTML` value. | ||
html, err := templ.ToGoHTML(context.Background(), templComponent) | ||
if err != nil { | ||
t.Fatalf("failed to convert to html: %v", err) | ||
} | ||
|
||
// Use the `template.HTML` value within the text/html template. | ||
err = example.Execute(os.Stdout, html) | ||
if err != nil { | ||
t.Fatalf("failed to execute template: %v", err) | ||
} | ||
} | ||
``` | ||
|
||
```html title="Output" | ||
<!DOCTYPE html> | ||
<html> | ||
<body> | ||
<div>Hello, World!</div> | ||
</body> | ||
</html> | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# Rendering raw HTML | ||
|
||
To render HTML that has come from a trusted source, bypassing all HTML escaping and security mechanisms that templ includes, use the `templ.Raw` function. | ||
|
||
:::info | ||
Only include HTML that comes from a trusted source. | ||
::: | ||
|
||
:::warning | ||
Use of this function may introduce security vulnerabilities to your program. | ||
::: | ||
|
||
```templ title="component.templ" | ||
templ Example() { | ||
<!DOCTYPE html> | ||
<html> | ||
<body> | ||
@templ.Raw("<div>Hello, World!</div>") | ||
</body> | ||
</html> | ||
} | ||
``` | ||
|
||
```html title="Output" | ||
<!DOCTYPE html> | ||
<html> | ||
<body> | ||
<div>Hello, World!</div> | ||
</body> | ||
</html> | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<body> | ||
<div>Hello, World!</div> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package testgotemplates | ||
|
||
import ( | ||
_ "embed" | ||
"testing" | ||
|
||
"github.com/a-h/templ/generator/htmldiff" | ||
) | ||
|
||
//go:embed expected.html | ||
var expected string | ||
|
||
func TestExample(t *testing.T) { | ||
component := Example() | ||
diff, err := htmldiff.Diff(component, expected) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if diff != "" { | ||
t.Error(diff) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package testgotemplates | ||
|
||
import "html/template" | ||
|
||
var goTemplate = template.Must(template.New("example").Parse("<div>{{ . }}</div>")) | ||
|
||
templ Example() { | ||
<!DOCTYPE html> | ||
<html> | ||
<body> | ||
@templ.FromGoHTML(goTemplate, "Hello, World!") | ||
</body> | ||
</html> | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,5 +14,6 @@ | |
} | ||
</script> | ||
<h1>Hello</h1> | ||
<div>World</div> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ templ Example() { | |
} | ||
</script> | ||
<h1>Hello</h1> | ||
@templ.Raw("<div>World</div>") | ||
</body> | ||
</html> | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<body> | ||
<div>Hello, World!</div> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package testgotemplates | ||
|
||
import ( | ||
"context" | ||
_ "embed" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/a-h/templ" | ||
"github.com/a-h/templ/generator/htmldiff" | ||
) | ||
|
||
//go:embed expected.html | ||
var expected string | ||
|
||
func TestExample(t *testing.T) { | ||
// Create the templ component. | ||
templComponent := greeting() | ||
html, err := templ.ToGoHTML(context.Background(), templComponent) | ||
if err != nil { | ||
t.Fatalf("failed to convert to html: %v", err) | ||
} | ||
|
||
// Use it within the text/html template. | ||
b := new(strings.Builder) | ||
err = example.Execute(b, html) | ||
if err != nil { | ||
t.Fatalf("failed to execute template: %v", err) | ||
} | ||
|
||
// Compare the output with the expected. | ||
diff, err := htmldiff.DiffStrings(expected, b.String()) | ||
if err != nil { | ||
t.Fatalf("failed to diff strings: %v", err) | ||
} | ||
if diff != "" { | ||
t.Error(diff) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package testgotemplates | ||
|
||
import "html/template" | ||
|
||
var example = template.Must(template.New("example").Parse(`<!DOCTYPE html> | ||
<html> | ||
<body> | ||
{{ . }} | ||
</body> | ||
</html> | ||
`)) | ||
|
||
templ greeting() { | ||
<div>Hello, World!</div> | ||
} |
Oops, something went wrong.