mirror of
https://github.com/vlang/v.git
synced 2025-09-15 07:22:27 +03:00
vlib: add a new Dynamic Template Manager module in x.templating.dtm
and an example vweb server that uses it (#20468)
This commit is contained in:
parent
5f732aa1b2
commit
07016fb3d5
8 changed files with 1750 additions and 0 deletions
|
@ -0,0 +1,65 @@
|
|||
import vweb
|
||||
import time
|
||||
import x.templating.dtm
|
||||
import os
|
||||
|
||||
struct App {
|
||||
vweb.Context
|
||||
pub mut:
|
||||
dtm &dtm.DynamicTemplateManager = dtm.create_dtm() @[vweb_global]
|
||||
shared_data_string []string @[vweb_global]
|
||||
shared_data_int []int @[vweb_global]
|
||||
shared_switch int = 1 @[vweb_global]
|
||||
}
|
||||
|
||||
fn main() {
|
||||
mut app := &App{}
|
||||
|
||||
app.shared_data_string << 'vweb dtm'
|
||||
app.shared_data_string << 'VWEB DTM'
|
||||
app.shared_data_string << '<span>this <br> is <br> a <br> test <br> with <br> included <br> HTML!</span>'
|
||||
app.shared_data_string << '<span>THIS <br> NOT <br> A <br> TEST!</span>'
|
||||
app.shared_data_string << '<p>escaped html tags test</p>'
|
||||
app.shared_data_int << 123456
|
||||
app.shared_data_int << 7891011
|
||||
|
||||
dtm.initialize_dtm(mut app.dtm) or { eprintln(err) }
|
||||
/*
|
||||
app.initialize_dtm(mut &app.dtm,
|
||||
compress_html: false
|
||||
active_cache_server: false
|
||||
max_size_data_in_mem: 100) or { eprintln(err) }
|
||||
*/
|
||||
go app.update_data()
|
||||
|
||||
app.mount_static_folder_at(os.resource_abs_path('static'), '/')
|
||||
|
||||
vweb.run(app, 18081)
|
||||
}
|
||||
|
||||
@['/']
|
||||
pub fn (mut app App) index() vweb.Result {
|
||||
mut tmpl_var := map[string]dtm.DtmMultiTypeMap{}
|
||||
tmpl_var['title'] = app.shared_data_string[app.shared_switch]
|
||||
tmpl_var['non_string_type'] = app.shared_data_int[app.shared_switch]
|
||||
tmpl_var['string_type'] = app.shared_data_string[4]
|
||||
tmpl_var['html_#includehtml'] = app.shared_data_string[app.shared_switch + 2]
|
||||
|
||||
// You can also modify the HTML template file directly without having to recompile the application.
|
||||
html_content := app.dtm.serve_dynamic_template('index.html',
|
||||
placeholders: &tmpl_var
|
||||
cache_delay_expiration: dtm.cache_delay_expiration_at_min
|
||||
)
|
||||
return app.html(html_content)
|
||||
}
|
||||
|
||||
fn (mut app App) update_data() {
|
||||
for {
|
||||
if app.shared_switch == 1 {
|
||||
app.shared_switch = 0
|
||||
} else {
|
||||
app.shared_switch = 1
|
||||
}
|
||||
time.sleep(10 * time.second)
|
||||
}
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
body {
|
||||
font-family: Arial, sans-serif;
|
||||
background-color: #f8f8f8;
|
||||
color: #333;
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
}
|
||||
|
||||
#container {
|
||||
width: 80%;
|
||||
margin: 0 auto;
|
||||
background-color: #fff;
|
||||
padding: 20px;
|
||||
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
|
||||
}
|
||||
|
||||
h1 {
|
||||
color: #007bff;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
p {
|
||||
font-size: 16px;
|
||||
line-height: 1.6;
|
||||
}
|
||||
|
||||
span {
|
||||
color: #28a745;
|
||||
font-weight: bold;
|
||||
}
|
|
@ -0,0 +1,2 @@
|
|||
<p>This is from a file inclusion!</p>
|
||||
<span>DTM</span>
|
|
@ -0,0 +1,16 @@
|
|||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>@title</title>
|
||||
@css '/index.css'
|
||||
</head>
|
||||
<body>
|
||||
@include 'comment.html'
|
||||
<div id="container">
|
||||
<H1>@title</H1>
|
||||
<p>@non_string_type</p>
|
||||
<p>@string_type</p>
|
||||
@html
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
Loading…
Add table
Add a link
Reference in a new issue