@@ -99,6 +99,7 @@ Vweb often uses convention over configuration and adding a new action requires
99
99
no routing rules either:
100
100
101
101
``` v oksyntax
102
+ // blog.v
102
103
import vweb
103
104
import time
104
105
@@ -138,6 +139,7 @@ Let's return an HTML view instead. Create `index.html` in the same directory:
138
139
and update our ` index() ` action so that it returns the HTML view we just created:
139
140
140
141
``` v ignore
142
+ // blog.v
141
143
pub fn (mut app App) index() vweb.Result {
142
144
message := 'Hello, world from Vweb!'
143
145
return $vweb.html()
@@ -185,6 +187,7 @@ We'll be using V's builtin ORM and a SQLite database.
185
187
186
188
Create a SQLite file with the schema:
187
189
``` sql
190
+ -- blog.sqlite
188
191
drop table if exists Article;
189
192
190
193
create table Article (
@@ -210,6 +213,7 @@ Run the file with `sqlite3 blog.db < blog.sqlite`.
210
213
Add a SQLite handle to ` App ` :
211
214
212
215
``` v oksyntax
216
+ // blog.v
213
217
import sqlite
214
218
import vweb
215
219
225
229
Add the ` init_server() ` method where we'll connect to a database:
226
230
227
231
``` v oksyntax
232
+ // blog.v
228
233
pub fn (mut app App) init_server() {
229
234
db := sqlite.connect(':memory:') or { panic(err) }
230
235
db.exec('create table `Article` (id integer primary key, title text default "", text text default "")')
@@ -260,6 +265,7 @@ pub fn (app &App) find_all_articles() []Article {
260
265
Let's fetch the articles in the ` index() ` action:
261
266
262
267
``` v ignore
268
+ // blog.v
263
269
pub fn (app &App) index() vweb.Result {
264
270
articles := app.find_all_articles()
265
271
return $vweb.html()
@@ -292,6 +298,8 @@ The built-in V ORM uses a syntax very similar to SQL. The queries are built with
292
298
For example, if we only wanted to find articles with ids between 100 and 200, we'd do:
293
299
294
300
``` v oksyntax
301
+ // article.v
302
+
295
303
return sql app.db {
296
304
select from Article where id >= 100 && id <= 200
297
305
}
@@ -300,6 +308,7 @@ return sql app.db {
300
308
Retrieving a single article is very simple:
301
309
302
310
``` v oksyntax
311
+ // article.v
303
312
pub fn (app &App) retrieve_article() ?Article {
304
313
return sql app.db {
305
314
select from Article limit 1
@@ -311,6 +320,7 @@ V ORM uses V's optionals for single values, which is very useful, since
311
320
bad queries will always be handled by the developer:
312
321
313
322
``` v oksyntax
323
+ // article.v
314
324
article := app.retrieve_article(10) or {
315
325
app.text('Article not found')
316
326
return
@@ -338,6 +348,7 @@ Create `new.html`:
338
348
```
339
349
340
350
``` v oksyntax
351
+ // article.v
341
352
import vweb
342
353
343
354
pub fn (mut app App) new_article() vweb.Result {
@@ -376,6 +387,7 @@ to render everything on the client or need an API, creating JSON endpoints
376
387
in V is very simple:
377
388
378
389
``` v oksyntax
390
+ // article.v
379
391
import vweb
380
392
import json
381
393
0 commit comments