Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE REQUEST] extend View with rendering from String/Bytes #1617

Closed
Dexus opened this issue Sep 7, 2020 · 1 comment
Closed

[FEATURE REQUEST] extend View with rendering from String/Bytes #1617

Dexus opened this issue Sep 7, 2020 · 1 comment

Comments

@Dexus
Copy link

Dexus commented Sep 7, 2020

I need the possible to render some data from a string to a varaible. But I wound like to reinitialize the complete View Engine with all the functions that need in both.

Would it be possible to make the Iris View Engine injectable like call direct ViewEngine functions or at last make if available the View Engine StringRenderer of each supporting Engine available.

kataras added a commit that referenced this issue Sep 7, 2020
relative to: #1617

Wait for an answer from the issuer and if that's the expected behavior, do the same for the rest of the view engines
@kataras
Copy link
Owner

kataras commented Sep 7, 2020

OK @Dexus I have implemented that on the HTML parser. Below you will find an example, if that's the expected behavior then I will do the same for the rest of the parsers too.

UPDATE: All view engines (except the Blocks for now) have the ParseTemplate method now. Examples can be found at: https://github.com/kataras/iris/tree/master/_examples/view/parse-template. Should we tag it as completed, is that you're looking for?

./main.go

// Package main shows how to parse a template through custom byte slice content.
package main

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

func main() {
	app := iris.New()
	// To not load any templates from files or embedded data,
	// pass nil or empty string on the first argument:
	// view := iris.HTML(nil, ".html")

	view := iris.HTML("./views", ".html")
	view.ParseTemplate("program.html", []byte(`<h1>{{greet .Name}}</h1>`), iris.Map{
		"greet": func(name string) string {
			return "Hello, " + name + "!"
		},
	})

	app.RegisterView(view)
	app.Get("/", index)
	app.Get("/layout", layout)

	app.Listen(":8080")
}

func index(ctx iris.Context) {
	ctx.View("program.html", iris.Map{
		"Name": "Gerasimos",
	})
}

func layout(ctx iris.Context) {
	ctx.ViewLayout("layouts/main.html")
	index(ctx)
}

./views/layout.html

<html>
<head>
<title>My Layout</title>

</head>
<body>
	<h1>[layout] Body content is below...</h1>
	<!-- Render the current template here -->
	{{ yield }}
</body>
</html>

http://localhost:8080

<h1>Hello, Gerasimos!</h1>

http://localhost:8080/layout

<html>
  <head>
    <title>My Layout</title>
  </head>
  <body>
    <h1>[layout] Body content is below...</h1>
    <h1>Hello, Gerasimos!</h1>
  </body>
</html>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants