Tutorial
Installation
Jinja can be installed using the Julia package manager. From the Julia REPL, type ] to enter the Pkg REPL mode and run.
pkg > add Jinja
Usage
Acutually, this package has only two structure and function, but these are very powerful because of Metaprogramming function of Julia.
Main.Jinja.Template
— TypeTemplate(html::String; path::Bool=true)
This is the only structure and function of this package. This structure has 2 parameter,
html
is the path to the HTML file or HTML of String type.path
determines whether the parameterhtml
represents the file path. The default value istrue
.
HTML
You can write the code of Template in JuliaLang, and just write the variables you want to output to a HTML at the end of the code. The code needs to be enclosed by ```.
For exmaple, this HTML work:
<html>
<head><title>Jinja Test</title></head>
<body>
Hello, `usr`!
</body>
</html>
Rendering
After you create a Template, you just have to execute the codes! For this, you use the Function-like Object of Template structure. tmp(; init::Expr) variables are initialized by init
(init
is the parameter for Function-like Object). init
must be Expr
type. If you don't pass the init
, the initialization won't be done. Please see the example below.
Example
tmp = Template("./test1.html") #The last HTML code
init = quote
usr="Julia"
end
result = tmp(init)
println(result)
Specifically, you can also do this.
#HTML Template File
<html>
</head><title>MyPage</title><head>
<body>
The current time is <strong>
`
using Dates
now()
`
</strong>
</body>
</html>
#Julia code
using Jinja
tmp = Template("./time.html") #The last HTML
println(tmp())
#The current time comes in the last HTML code intead of the Julia code and returns it.