Tutorial
Installation
OteraEngine can be installed using the Julia package manager. From the Julia REPL, type ] to enter the Pkg REPL mode and run.
pkg > add OteraEngine
Usage
Acutually, this package has only two structure and function, but these are very powerful because of Metaprogramming function of Julia.
Main.OteraEngine.Template
— TypeTemplate(html::String; path::Bool=true, config_path::String="",
config::Dict{String, T} = Dict(
"code_block_start"=>"```",
"code_block_stop"=>"```"
)
)
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
.config_path
is path to config file. The suffix of config file must betoml
.config
is configuration of Template. It is type ofDict
, and now there are two settings bellow.code_block_start
: The string at the start of code blocks.code_block_stop
: The string at the end of code blocks.
Config File
A config file must be written in TOML format. like this:
code_block_start = "{{"
code_block_stop = "}}"
The item is the same as the argiment config
.
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 ``(This can be changed by
config` variable).
For exmaple, this HTML work:
<html>
<head><title>OteraEngine 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::Dict{String, T}) where T <: Any
variables are initialized by init
(init
is the parameter for Function-like Object). init
must be Dict
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 = Dict("usr"=>"OteraEngine")
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 OteraEngine
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.