tutorials: update vweb
parent
2533c706ae
commit
fa00f157ad
|
@ -1,5 +1,6 @@
|
||||||
## V 0.2.2
|
## V 0.2.2
|
||||||
*Not yet released*
|
*Not yet released*
|
||||||
|
- `vweb` now uses struct embedding: `app.vweb.text('hello') => app.text('hello')`.
|
||||||
- Consts can now be declared outside of `const()` blocks: `const x = 0`.
|
- Consts can now be declared outside of `const()` blocks: `const x = 0`.
|
||||||
|
|
||||||
## V 0.2.1
|
## V 0.2.1
|
||||||
|
|
|
@ -62,8 +62,7 @@ module main
|
||||||
import vweb
|
import vweb
|
||||||
|
|
||||||
struct App {
|
struct App {
|
||||||
pub mut:
|
vweb.Context
|
||||||
vweb vweb.Context
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
@ -71,7 +70,7 @@ fn main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (mut app App) index() vweb.Result {
|
pub fn (mut app App) index() vweb.Result {
|
||||||
app.vweb.text('Hello, world from vweb!')
|
app.text('Hello, world from vweb!')
|
||||||
return vweb.Result{}
|
return vweb.Result{}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -98,7 +97,9 @@ Vweb helpfully provided a link, open http://localhost:8081/ in your browser:
|
||||||
|
|
||||||
The `App` struct is an entry point of our web application. If you have experience
|
The `App` struct is an entry point of our web application. If you have experience
|
||||||
with an MVC web framework, you can think of it as a controller. (Vweb is
|
with an MVC web framework, you can think of it as a controller. (Vweb is
|
||||||
not an MVC framework however.)
|
not an MVC framework however.) It embeds the vweb Context object, that's why we get access
|
||||||
|
to methods like `.text()`.
|
||||||
|
|
||||||
|
|
||||||
As you can see, there are no routing rules. The `index()` action handles the `/` request by default.
|
As you can see, there are no routing rules. The `index()` action handles the `/` request by default.
|
||||||
Vweb often uses convention over configuration and adding a new action requires
|
Vweb often uses convention over configuration and adding a new action requires
|
||||||
|
@ -109,7 +110,7 @@ import vweb
|
||||||
import time
|
import time
|
||||||
|
|
||||||
fn (mut app App) time() vweb.Result {
|
fn (mut app App) time() vweb.Result {
|
||||||
app.vweb.text(time.now().format())
|
app.text(time.now().format())
|
||||||
return vweb.Result{}
|
return vweb.Result{}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
@ -319,7 +320,7 @@ bad queries will always be handled by the developer:
|
||||||
|
|
||||||
```v oksyntax
|
```v oksyntax
|
||||||
article := app.retrieve_article(10) or {
|
article := app.retrieve_article(10) or {
|
||||||
app.vweb.text('Article not found')
|
app.text('Article not found')
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
@ -348,10 +349,10 @@ Create `new.html`:
|
||||||
import vweb
|
import vweb
|
||||||
|
|
||||||
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{
|
||||||
|
@ -362,7 +363,7 @@ pub fn (mut app App) new_article() vweb.Result {
|
||||||
sql app.db {
|
sql app.db {
|
||||||
insert article into Article
|
insert article into Article
|
||||||
}
|
}
|
||||||
app.vweb.redirect('/')
|
app.redirect('/')
|
||||||
return vweb.Result{}
|
return vweb.Result{}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
@ -390,7 +391,7 @@ import json
|
||||||
|
|
||||||
pub fn (mut app App) articles() vweb.Result {
|
pub fn (mut app App) articles() vweb.Result {
|
||||||
articles := app.find_all_articles()
|
articles := app.find_all_articles()
|
||||||
app.vweb.json(json.encode(articles))
|
app.json(json.encode(articles))
|
||||||
return vweb.Result{}
|
return vweb.Result{}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
Loading…
Reference in New Issue