|
|
Home » Developing U++ » U++ Developers corner » Web framework - Skylark - first taste
Web framework - Skylark - first taste [message #34937] |
Sun, 18 December 2011 20:26 |
|
mirek
Messages: 14105 Registered: November 2005
|
Ultimate Member |
|
|
I have got to the point where I can produce very simple application using (normal) forms, sqlite3 and witz templates (in the end, I am using "witz" both as the name of template language and as extension).
The point of application is to somewhat resemble examples/AddressBook. I have added pictures to test multipart posts...
Ugly boilerplate code excluded, application consists of following code and templates:
C++ code:
URL_VIEW(Picture, "person/*/picture")
{
http.Content("image/jpeg", LoadFile(ConfigFile(http[0] + ".data")));
}
URL_VIEW(HomePage, "index.html")
{
ValueArray person;
Sql sql;
sql * Select(ID, FIRSTNAME, LASTNAME, EMAIL, SEX)
.From(PERSON)
.OrderBy(LASTNAME);
while(sql.Fetch()) {
ValueMap vm;
for(int i = 0; i < sql.GetColumns(); i++)
vm.Add(ToUpper(sql.GetColumnInfo(i).name), sql[i]);
vm.Add("HASPICTURE", FileExists(ConfigFile(AsString(sql[ID]) + ".data")));
person.Add(vm);
}
http
("PERSON", person)
.Render("AdrBook/Index.witz");
}
URL_VIEW(NotFound, "**")
{
http.Redirect("/index.html");
}
URL_VIEW(SubmitNew, "submit")
{
SQL * Insert(PERSON)
(FIRSTNAME, http["firstname"])
(LASTNAME, http["lastname"])
(EMAIL, http["email"])
(SEX, http["sex"])
;
SaveFile(ConfigFile(AsString(SQL.GetInsertedId()) + ".data"), http["file"]);
http.Redirect("/index.html");
}
URL_VIEW(New, "new")
{
http("EDIT", 0).Render("AdrBook/Dialog.witz");
}
URL_VIEW(SubmitEdit, "submit/edit/*")
{
int id = atoi(http[0]);
SQL * Update(PERSON)
(FIRSTNAME, http["firstname"])
(LASTNAME, http["lastname"])
(EMAIL, http["email"])
(SEX, http["sex"])
.Where(ID == atoi(http[0]));
;
String file = http["file"];
if(file.GetCount())
SaveFile(ConfigFile(AsString(id) + ".data"), file);
http.Redirect("/index.html");
}
URL_VIEW(Edit, "person/*/edit")
{
int id = atoi(http[0]);
Sql sql;
sql * Select(FIRSTNAME, LASTNAME, EMAIL, SEX)
.From(PERSON)
.Where(ID == id);
if(!sql.Fetch()) {
http.Redirect("/index.html");
return;
}
http
("ID", id)
("FIRSTNAME", sql[FIRSTNAME])
("LASTNAME", sql[LASTNAME])
("EMAIL", sql[EMAIL])
("SEX", sql[SEX])
("EDIT", 1)
.Render("AdrBook/Dialog.witz");
}
URL_VIEW(Delete, "person/*/delete")
{
SQL * Delete(PERSON).Where(ID == atoi(http[0]));
DeleteFile(ConfigFile(atoi(http[0]) + ".data"));
http.Redirect("/index.html");
}
And witz templates:
Style.witz
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="cs" lang="cs">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>#TITLE</title>
<html>
<body>
#BODY
</body>
</html>
#define TITLE This is an experimental Web AddressBook application
Dialog.witz
#include Style.witz
#define BODY
<FORM action=$(EDIT ? SubmitEdit(ID) : SubmitNew) method="post" accept-charset="utf-8" enctype="multipart/form-data">
<P>
First name: <INPUT type="text" name="firstname" value="$FIRSTNAME"><BR>
Last name: <INPUT type="text" name="lastname" value="$LASTNAME"><BR>
email: <INPUT type="text" name="email" value="$EMAIL"><BR>
<INPUT type="radio" name="sex" value="M" $(SEX != "F" ? "checked" : "")>Male<BR>
<INPUT type="radio" name="sex" value="F" $(SEX == "F" ? "checked" : "")>Female<BR>
<INPUT type="submit" value="Send" name="OK"><BR>
Picture upload: <INPUT type="file" name="file" id="file" />
</P>
</FORM>
#define TITLE Please enter a person!
Index.witz
#include Style.witz
#define BODY
<table border="1">
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
<th>Sex</th>
<th>Action</th>
</tr>
$for(i in PERSON)
<tr>
<td>$i.FIRSTNAME</td>
<td>$i.LASTNAME</td>
<td>$i.EMAIL</td>
<td>$(i.SEX == "M" ? "Male" : "Female")</td>
<td><a href=$Edit(i.ID)>Edit</a> <a href=$Delete(i.ID)>Delete</a>
$if(i.HASPICTURE) <a href=$Picture(i.ID)>Show picture</a> $endif
</td>
</tr>
$endfor
</table>
<p/>
<a href=$New>Insert new person</a>
C++ part contains lots of SQL<->http translations which can be easily automated. That excluded, I guess we are heading in the right direction...
Note that this is just hard bottom of framework. Nice things are yet to come
Mirek
[Updated on: Sun, 18 December 2011 20:29] Report message to a moderator
|
|
|
|
|
|
Re: Web framework - Skylark - first taste [message #34958 is a reply to message #34937] |
Tue, 20 December 2011 14:43 |
|
Seems nice and relatively easy to use
Just a question though: Will there be some support for internationalization? Translations on the template level are IMHO quite important, because nobody wants to maintain N almost identical templates that differ only by column names in table or something similarly unimportant And supplying all the texts via U++ code is not good idea, for multiple reasons (it would affect performance, readability, designer/coder separation, etc...). In other words, it would be great if it would be possible to use t_() in templates
Honza
|
|
|
|
|
|
|
|
|
|
|
Re: Web framework - Skylark - first taste [message #34999 is a reply to message #34998] |
Sun, 25 December 2011 17:16 |
|
mirek
Messages: 14105 Registered: November 2005
|
Ultimate Member |
|
|
cbpporter wrote on Sun, 25 December 2011 04:56 | Well the witz files are a form of "html based template system". Let's say you define a XML namespace u:"www.ultimatepp.org". Then, you can do the include statement like:
<u:include page="style.witz">
|
I guess it should be:
<u:include page="style.witz"/>
right?
Quote: |
This way it is compatible with XHTML. It also has the advantage that there are a lot of WYSIWYG web page editors out there that are capable of editing partial HTML pages, the one that are commonly used in web page design. The u:include tag would be ignored by these editors letting you continue edit the page. But finding some other syntax might confuse it. The best case scenario is that it interprets the statement as text. The worst case scenario is that it fails to parse the xhtml and refuses it as ill formated.
|
I now understand what you mean, however, '#' is OK in XML and there is nothing worse than that in preprocessor, so it would be interpreted as text in any case.
In fact, I would have little problem providing 'tag' alternative for preprocessor, but I think that much worse issue is that expressions of witz (phase 5) mimick javascript and thus allow < > and & characters. I guess it is solved by using CDATA for JavaScript, so the same solution should work for us too, but perhaps it would not hurt to add "and lt gt lte gte" alternative keywords to the language...
Mirek
[Updated on: Sun, 25 December 2011 17:17] Report message to a moderator
|
|
|
|
Goto Forum:
Current Time: Fri Nov 01 01:15:58 CET 2024
Total time taken to generate the page: 0.02105 seconds
|
|
|