Table of Contents
There are 4 ways to render HTML in Basolato. Although each library has it's own benefits and drawbacks, every library can be used.
Basolato use nim-templates
as a default template engin. It can be used by importing basolato/view
.
Library | Benefits | Drawbacks |
---|---|---|
htmlgen |
|
|
SCF |
|
|
Karax |
|
|
nim-templates |
|
|
Views file should be in resources
dir.
To send POST request from form
, you have to set csrf token
. You can use helper function from basolato/view
import basolato/view
proc index*():string = tmpli html"""
<form>
$(csrfToken())
<input type="text", name="name">
</form>
"""
import htmlgen
import basolato/view
proc index*():string =
form(
csrfToken(),
input(type="text", name="name")
)
#? stdtmpl | standard
#import basolato/view
#proc index*():string =
<form>
${csrfToken()}
<input type="text", name="name">
</form>
import basolato/view
import karax / [karaxdsl, vdom]
proc index*():string =
var vnode = buildView(form):
csrfTokenKarax()
input(type="text", name="name")
return $vnode
Controller and result is same for each example.
controller
proc index*(): Response =
let message = "Basolato"
return render(indexView(message))
result
<html>
<head>
<title>Basolato</title>
</head>
<body>
<p>Basolato</p>
</body>
</html>
import basolato/view
proc baseImpl(content:string): string = tmpli html"""
<html>
<heade>
<title>Basolato</title>
</head>
<body>
$(content)
</body>
</html>
"""
proc indexImpl(message:string): string = tmpli html"""
<p>$message</p>
"""
proc indexView*(message:string): string =
baseImpl(indexImpl(message))
import htmlgen
proc baseImpl(content:string): string =
html(
head(
title("Basolato")
),
body(content)
)
proc indexImpl(message:string): string =
p(message)
proc indexView*(message:string): string =
baseImpl(indexImpl(message))
SCF should divide procs for each file
baseImpl.nim
#? stdtmpl | standard
#proc baseImpl*(content:string): string =
<html>
<heade>
<title>Basolato</title>
</head>
<body>
$content
</body>
</html>
indexImpl.nim
#? stdtmpl | standard
#proc indexImpl*(message:string): string =
<p>$message</p>
index_view.nim
#? stdtmpl | standard
#import baseImpl
#import indexImpl
#proc indexView*(message:string): string =
${baseImpl(indexImpl(message))}
This usage is Server Side HTML Rendering
import karax / [karasdsl, vdom]
proc baseImpl(content:string): string =
var vnode = buildView(html):
head:
title: text("Basolato")
body: text(content)
return $vnode
proc indexImpl(message:string): string =
var vnode = buildView(p):
text(message)
return $vnode
proc indexView*(message:string): string =
baseImpl(indexImpl(message))
If the user's input value is invalid and you want to back the input page and display the previously entered value, you can use old
helper function.
controller
# get access
proc signinPage*(this:LoginController):Response =
return render(this.view.signinView())
# post access
proc signin(this:LoginController):Response =
let params = this.request.params()
let email = params["email"]
try
...
except:
return render(Http422, this.view.signinView(%params))
view
proc impl(params=newJObject()):string = tmpli html"""
<input type="text" name="email" value="$(old(params, "email"))">
<input type="text" name="password">
"""
proc signinView*(this:View, params=newJObject()):string =
let title = "SignIn"
return this.applicationView(title, impl(params))
It display value if params
has key email
, otherwise display empty string.
You can access auth
in view like bellow.
controller
proc home*(this:StaticPageController):Response =
return render(this.view.homeView())
view
import basolato/view
proc headerView*(auth:Auth):string = tmpli html"""
<header>
<ul>
$if auth.isLogin(){
<li>$(auth.get("id"))</li>
}
$else{
<li><a href="/login">Log In</a></li>
}
</ul>
</header>
proc applicationView*(this:View, title:string, body:string, flash=newJObject()):string = tmpli html"""
<!DOCTYPE html>
<html>
<head>
</head>
<body>
$(headerView(this.auth))
...
</body>
</html>
proc homeView*(this:View):string =
this.applicationView("Title", impl())
"""