mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
tutorials: vweb: add an article
This commit is contained in:
@ -4,6 +4,7 @@ import (
|
||||
vweb
|
||||
time
|
||||
pg
|
||||
json
|
||||
)
|
||||
|
||||
struct App {
|
||||
@ -42,6 +43,32 @@ pub fn (app mut App) init() {
|
||||
app.db = db
|
||||
}
|
||||
|
||||
pub fn (app mut App) new() {
|
||||
$vweb.html()
|
||||
}
|
||||
|
||||
pub fn (app mut App) new_article() {
|
||||
title := app.vweb.form['title']
|
||||
text := app.vweb.form['text']
|
||||
if title == '' || text == '' {
|
||||
app.vweb.text('Empty text/titile')
|
||||
return
|
||||
}
|
||||
article := Article{
|
||||
title: title
|
||||
text: text
|
||||
}
|
||||
println(article)
|
||||
db := app.db
|
||||
db.insert(article)
|
||||
app.vweb.redirect('/article/')
|
||||
}
|
||||
|
||||
pub fn (app mut App) articles() {
|
||||
articles := app.find_all_articles()
|
||||
app.vweb.json(json.encode(articles))
|
||||
}
|
||||
|
||||
fn (app mut App) time() {
|
||||
app.vweb.text(time.now().format())
|
||||
}
|
||||
|
@ -9,6 +9,8 @@
|
||||
@article.text
|
||||
</div>
|
||||
@end
|
||||
<br>
|
||||
<a href='/new'>New article</a>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
|
13
tutorials/code/blog/new.html
Normal file
13
tutorials/code/blog/new.html
Normal file
@ -0,0 +1,13 @@
|
||||
<html>
|
||||
<header>
|
||||
<title>V Blog</title>
|
||||
</header>
|
||||
<body>
|
||||
<form action='/new_article' method='post'>
|
||||
<input type='text' placeholder='Title' name='title'> <br>
|
||||
<textarea placeholder='Text' name='text'></textarea>
|
||||
<input type='submit'>
|
||||
</form>
|
||||
</body>
|
||||
</html>
|
||||
|
Reference in New Issue
Block a user