RejectedSoftware Forums

Sign up

Html in Diet templates question

Hello,

I can not figure out the following , maybe i miss something very obvious..

How can i insert html code in a diet file as a variable?

I have :

string[] errors ~= "<li>This is one error</li>";
errors ~= "<li>This is another error</li>";

Then:

string errorstring;
	foreach(er; errors){
		errorstring ~= er;
	}
	req.params["error"] = urlEncode(errorstring);
	showPage(req,res);

And then at showPage function:

if (auto er = "error" in req.params) info.errors = *er;

(Passing the string with in an info struct).

Then in my diet template i have:

- if(info.errors != "")
	ul
|#{vibe.textfilter.urlencode.urlDecode(info.errors)}

but it gives the string as text and not html.
i tried also: ul

|!= #{info.errors} or |!= "#{info.errors}"

but it gives <ul>#{info.errors}</ul>

Any ideas?

And by the way does anybody have any advice about the best way to pass errors or notifications to different HTTP functions?
Hardcode them where they are displayed and pass error codes in queries?
Pass the error strings as parameters (but this doesn't work with res.redirect) or as query parameters in the url?

Thank you!

Re: Html in Diet templates question

On Thu, 12 Jun 2014 13:09:29 GMT, Yiannis Tsirikoglou wrote:

Hello,

I can not figure out the following , maybe i miss something very obvious..

How can i insert html code in a diet file as a variable?

I have :

string[] errors ~= "<li>This is one error</li>";
errors ~= "<li>This is another error</li>";

Then:

string errorstring;
	foreach(er; errors){
		errorstring ~= er;
	}
	req.params["error"] = urlEncode(errorstring);
	showPage(req,res);

And then at showPage function:

if (auto er = "error" in req.params) info.errors = *er;

(Passing the string with in an info struct).

Then in my diet template i have:

- if(info.errors != "")
	ul
|#{vibe.textfilter.urlencode.urlDecode(info.errors)}

but it gives the string as text and not html.
i tried also: ul

|!= #{info.errors} or |!= "#{info.errors}"

but it gives <ul>#{info.errors}</ul>

Any ideas?

This should work:

ul!= info.errors
// or
u1
    != info.errors

or this:

ul
    |!{info.errors}

I would expect |!= #{info.errors} to result in a compiler error. I'll look into that.

And by the way does anybody have any advice about the best way to pass errors or notifications to different HTTP functions?
Hardcode them where they are displayed and pass error codes in queries?
Pass the error strings as parameters (but this doesn't work with res.redirect) or as query parameters in the url?

I usually pass them by parameter, but that indeed has the issue of not working well with redirects. The good thing is that this is supported by vibe.web.web, though, saving some boilerplate code:

@path("/")
void getIndex(string _error = null)
{
	render!("index.dt", _error);
}

@errorDisplay!getIndex
void postLogin(string username, string password)
{
	enforceHTTP(username.length > 0, HTTPStatus.forbidden,
		"User name must not be empty.");
	enforceHTTP(password == "secret", HTTPStatus.forbidden,
		"Invalid password.");
	m_loginUser = username;
	redirect("/profile");
}

Any exception will automatically cause getIndex to be called for rendering the error message. Later it's also planned to add direct parameter validation support that will call the error display function without any exceptions involved (for efficiency).

Re: Html in Diet templates question

On Thu, 12 Jun 2014 15:43:16 GMT, Sönke Ludwig wrote:

This should work:

ul!= info.errors
// or
u1
    != info.errors

or this:

ul
    |!{info.errors}

I would expect |!= #{info.errors} to result in a compiler error. I'll look into that.

Yes it works now thank you. I got a bit confused with diet or d code after != .

And by the way does anybody have any advice about the best way to pass errors or notifications to different HTTP functions?
Hardcode them where they are displayed and pass error codes in queries?
Pass the error strings as parameters (but this doesn't work with res.redirect) or as query parameters in the url?

I usually pass them by parameter, but that indeed has the issue of not working well with redirects. The good thing is that this is supported by vibe.web.web, though, saving some boilerplate code:

@path("/")
void getIndex(string _error = null)
{
	render!("index.dt", _error);
}

@errorDisplay!getIndex
void postLogin(string username, string password)
{
	enforceHTTP(username.length > 0, HTTPStatus.forbidden,
		"User name must not be empty.");
	enforceHTTP(password == "secret", HTTPStatus.forbidden,
		"Invalid password.");
	m_loginUser = username;
	redirect("/profile");
}

Any exception will automatically cause getIndex to be called for rendering the error message. Later it's also planned to add direct parameter validation support that will call the error display function without any exceptions involved (for efficiency).

I have my own system for validation errors and notifications related to user input without using exceptions and i use vibe.d error handling for all the rest.
In your example the errorDisplay! property is assigned to the postLogin function but i guess i can later assign other functions' errors to different pages like:

  @path("/")
 void getIndex(string _error = null)
 {
 	render!("index.dt", _error);
 }
 
 @errorDisplay!getIndex
 void postLogin(string username, string password)
 {
 	enforceHTTP(username.length > 0, HTTPStatus.forbidden,
 		"User name must not be empty.");
 	enforceHTTP(password == "secret", HTTPStatus.forbidden,
 		"Invalid password.");
 	m_loginUser = username;
 	redirect("/profile");
 }
@path("/another")
 void getAnother(string _error = null)
 {
 	render!("other.dt", _error);
 }
@errorDisplay!getAnother
void postSomething()
{
...
}
The web interface generator seems nice i should take some time and change my code.