1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

vweb: more updates

This commit is contained in:
Alexander Medvednikov 2020-12-31 17:47:20 +01:00
parent fa00f157ad
commit 2bc9ee4d88
4 changed files with 17 additions and 23 deletions

View File

@ -8,8 +8,8 @@ const (
struct App { struct App {
vweb.Context vweb.Context
pub mut: mut:
cnt int cnt int
} }
fn main() { fn main() {
@ -31,7 +31,7 @@ pub fn (mut app App) json_endpoint() vweb.Result {
pub fn (mut app App) index() vweb.Result { pub fn (mut app App) index() vweb.Result {
app.cnt++ app.cnt++
show := true show := true
// app.vweb.text('Hello world from vweb') // app.text('Hello world from vweb')
hello := 'Hello world from vweb' hello := 'Hello world from vweb'
numbers := [1, 2, 3] numbers := [1, 2, 3]
return $vweb.html() return $vweb.html()
@ -42,9 +42,6 @@ pub fn (mut app App) show_text() vweb.Result {
} }
pub fn (mut app App) cookie() vweb.Result { pub fn (mut app App) cookie() vweb.Result {
app.set_cookie({ app.set_cookie(name: 'cookie', value: 'test')
name: 'cookie'
value: 'test'
})
return app.text('Headers: $app.headers') return app.text('Headers: $app.headers')
} }

View File

@ -6,9 +6,8 @@ import sqlite
import json import json
struct App { struct App {
pub mut: vweb.Context
vweb vweb.Context db sqlite.DB
db sqlite.DB
} }
fn main() { fn main() {
@ -32,9 +31,7 @@ pub fn (app &App) index() vweb.Result {
} }
pub fn (mut app App) init_once() { pub fn (mut app App) init_once() {
db := sqlite.connect('blog.db') or { db := sqlite.connect('blog.db') or { panic(err) }
panic(err)
}
app.db = db app.db = db
} }
@ -46,10 +43,10 @@ pub fn (mut app App) new() vweb.Result {
} }
pub fn (mut app App) new_article() vweb.Result { pub fn (mut app App) new_article() vweb.Result {
title := app.vweb.form['title'] title := app.form['title']
text := app.vweb.form['text'] text := app.form['text']
if title == '' || text == '' { if title == '' || text == '' {
app.vweb.text('Empty text/title') app.text('Empty text/title')
return vweb.Result{} return vweb.Result{}
} }
article := Article{ article := Article{
@ -60,14 +57,14 @@ pub fn (mut app App) new_article() vweb.Result {
sql app.db { sql app.db {
insert article into Article insert article into Article
} }
return app.vweb.redirect('/') return app.redirect('/')
} }
pub fn (mut app App) articles() { pub fn (mut app App) articles() {
articles := app.find_all_articles() articles := app.find_all_articles()
app.vweb.json(json.encode(articles)) app.json(json.encode(articles))
} }
fn (mut app App) time() { fn (mut app App) time() {
app.vweb.text(time.now().format()) app.text(time.now().format())
} }

View File

@ -3,12 +3,12 @@ vlib/v/checker/tests/vweb_routing_checks.vv:22:1: error: mismatched parameters c
21 | ['/bar'] 21 | ['/bar']
22 | pub fn (mut app App) bar(a string) vweb.Result { 22 | pub fn (mut app App) bar(a string) vweb.Result {
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
23 | app.vweb.html('works') 23 | app.html('works')
24 | return vweb.Result{} 24 | return vweb.Result{}
vlib/v/checker/tests/vweb_routing_checks.vv:29:1: error: mismatched parameters count between vweb method `App.cow` (0) and route attribute ['/cow/:low'] (1) vlib/v/checker/tests/vweb_routing_checks.vv:29:1: error: mismatched parameters count between vweb method `App.cow` (0) and route attribute ['/cow/:low'] (1)
27 | // no segfault, but it shouldnt compile 27 | // no segfault, but it shouldnt compile
28 | ['/cow/:low'] 28 | ['/cow/:low']
29 | pub fn (mut app App) cow() vweb.Result { 29 | pub fn (mut app App) cow() vweb.Result {
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
30 | app.vweb.html('works') 30 | app.html('works')
31 | return vweb.Result{} 31 | return vweb.Result{}

View File

@ -29,7 +29,7 @@ pub fn (app mut App) index() {
pub fn (app App) post() { pub fn (app App) post() {
id := app.get_post_id() id := app.get_post_id()
post := app.retrieve_post(id) or { post := app.retrieve_post(id) or {
app.vweb.redirect('/') app.redirect('/')
return return
} }
comments := app.find_comments(id) comments := app.find_comments(id)
@ -52,7 +52,7 @@ pub fn (app App) post() {
@end @end
``` ```
`$vweb.html()` compiles an HTML template into V during compilation, `$vweb.html()` compiles an HTML template into V during compilation,
and embeds the resulting code in current action. and embeds the resulting code in current action.
That means that the template automatically has access to that action's entire environment. That means that the template automatically has access to that action's entire environment.