Commit 8b777512 authored by Bat's avatar Bat
Browse files

Merge branch 'master' into 'master'

fix #41 + improvement in the for loop



See merge request !17
parents 914308d3 d7bbd6c3
Pipeline #9445 passed with stage
in 9 minutes and 36 seconds
using Valse;
/*
* A list of users.
*
* Because @{link Valse.Many} can't be used as models yet.
*/
public class UserList : Object {
/*
* The list
*/
public Many<User> users { get; set; }
}
using Valse;
/*
* Represents an user.
*
......@@ -30,4 +32,11 @@ public class User : Object {
* Used to test dates.
*/
public DateTime register_date { get; set; }
/*
* The post of this user.
*
* Used to test Many<G>.
*/
public Many<string> posts { get; set; default = new Many<string> (); }
}
......@@ -46,10 +46,10 @@ public class UserController : Controller {
UserList list = new UserList ();
list.users = usrs;
var model = new HashMap<string, Object> ();
model ["mod"] = list;
//var model = new HashMap<string, Object> ();
//model ["mod"] = list;
return page ("views/user/list.wala", model);
return page ("views/user/list.wala", model ("users", usrs));
}
/*
......@@ -99,7 +99,9 @@ public class UserController : Controller {
} else {
int id = int.parse (req.query_ids["id"]);
User usr = (User) db.get_id<User> (id);
print ("account created the : " + usr.register_date.to_string () + "\n");
usr.posts.add ("Hello, i'm #newhere.");
usr.posts.add ("What a fantastic website!");
usr.posts.add ("ping @Plop : do you want a banana?");
return page ("views/user/profile.wala", model ("mod", usr, "test", new TestModel ("Yet another link", "https://bat41.gitlab.io")));
}
}
......
......@@ -6,10 +6,10 @@ All users
{% zone body %}
<p>
There is {{ mod.users.count }} users.
There is {{ users.count }} users.
</p>
<ul>
{% for user, i in mod.users %}
{% for user, i in users %}
<li><a href="/user/profile/{{ i|inc }}">{{ user.pseudo }}</a></li>
{% endfor %}
</ul>
......
......@@ -11,5 +11,9 @@
{{ mod.sign|typofr }}
</blockquote>
<a href="{{ mod.website }}">{{ mod.pseudo }}'s website</a>
<h2>Latest posts</h2>
{% for post in mod.posts %}
<p>{{ post }}</p>
{% endfor %}
<a href="{{ test.url }}">{{ test.blob }}</a>
{% endzone %}
......@@ -154,7 +154,12 @@ namespace Valse {
* @return A {@link Gee.HashMap} usable in a view
*/
private HashMap<string, string> create_model (Object obj) {
// special behavior for Many
if (obj.get_type ().is_a (typeof (Many))) {
return ((Many) obj).to_hashmap ();
}
// for standard GObject objects
HashMap<string, string> result = new HashMap<string, string> ();
foreach (ParamSpec spec in obj.get_class ().list_properties ()) {
......@@ -164,17 +169,17 @@ namespace Valse {
obj.get_property (spec.get_nick (), ref val);
var values = ((Many) val.get_object ()).to_hashmap ().entries;
foreach (var entry in values) {
result [spec.get_nick () + entry.key] = entry.value;
result ["." + spec.get_nick () + entry.key] = entry.value;
}
} else if (spec.value_type == typeof (DateTime)) {
GLib.Value val = GLib.Value (typeof (DateTime));
string prop = spec.name.replace ("-", "_");
obj.get_property (spec.name, ref val);
result [prop] = ((DateTime) val).format ("%F %H-%M-%S.000");
result ["." + prop] = ((DateTime) val).format ("%F %H-%M-%S.000");
} else {
GLib.Value val = GLib.Value (typeof (string)); // to get variable's value
obj.get_property (spec.get_nick (), ref val); // we get this value
result [spec.get_nick ().replace ("-", "_")] = (string) val; //we set it in our model (used in the view)
result ["." + spec.get_nick ().replace ("-", "_")] = (string) val; //we set it in our model (used in the view)
}
}
......
......@@ -166,14 +166,17 @@ namespace Valse {
var mdl = model.value;
foreach (var mod_var in mdl.entries) {
model_dump += @"<p>$(model_keyword).$(mod_var.key) = $(mod_var.value)";
// dumping the new model
model_dump += @"<p>$(model_keyword)$(mod_var.key) = $(mod_var.value)";
if (mod_var.value == null) {
model_dump += "(null)";
}
model_dump += "</p>";
}
Regex for_re = new Regex ("{% *for (?P<item>[\\w_.]+?)(, *(?P<index>[\\w]+))? in " + model_keyword + "\\.(?P<coll>[[:graph:]]*?) *%}(?P<content>(.|\\R)+?){% *endfor *%}");
print (model_dump.replace ("</p>", "\n").replace("<p>", ""));
Regex for_re = new Regex ("{% *for (?P<item>[\\w_.]+?)(, *(?P<index>[\\w]+))? in (?P<coll>" + model_keyword + "(\\.([[:graph:]]*?))?) *%}(?P<content>(.|\\R)+?){% *endfor *%}");
MatchInfo for_info;
bool for_go = for_re.match (result, RegexMatchFlags.NEWLINE_ANY, out for_info);
int for_count = 0;
......@@ -181,6 +184,7 @@ namespace Valse {
while (for_go) {
string item = for_info.fetch_named ("item");
string coll = for_info.fetch_named ("coll");
print (@"coll : $coll\n");
string content = for_info.fetch_named ("content");
string? index_var = for_info.fetch_named ("index");
......@@ -188,25 +192,26 @@ namespace Valse {
// while we haven't see every element
int index = 0;
int coll_length = int.parse (mdl [coll + ".count"]);
int coll_length = int.parse (mdl [coll.replace (model_keyword, "") + ".count"]);
while (index < coll_length) {
// for each element of the collection
// we insert the pattern (= content of the for loop)
Regex item_re = new Regex ("{{ *%s(?P<prop>[[:graph:]]+) *}}".printf (item));
Regex item_re = new Regex ("{{ *%s(?P<prop>[[:graph:]]*) *}}".printf (item));
generated += item_re.replace_eval (content, content.length, 0, 0, (info, res) => {
string? elem_prop = info.fetch_named ("prop");
res.append ("{{ " + model_keyword + "." + coll + "[" + index.to_string () + "]" + elem_prop + " }}");
res.append ("{{ " + coll + "[" + index.to_string () + "]" + elem_prop + " }}");
return false;
});
if (index_var != null) {
if (index_var != null && index_var.length != 0) {
print (@"index var is '$index_var' $(index_var.length)\n");
// interpreting index variables
Regex index_re = new Regex ("{{ %s(?P<filters>.*?) }}".printf (index_var));
generated = index_re.replace_eval (generated, generated.length, 0, 0, (info, res) => {
// we don't put index directly to make it possible to apply filters
string mdl_index_var = "__for__%d__index__%d__".printf (for_count, index);
string mdl_index_var = ".__for__%d__index__%d__".printf (for_count, index);
mdl[mdl_index_var] = index.to_string ();
res.append ("{{ %s.%s%s }}".printf (model_keyword, mdl_index_var, info.fetch_named ("filters") == null ? "" : info.fetch_named ("filters")));
res.append ("{{ %s%s%s }}".printf (model_keyword, mdl_index_var, info.fetch_named ("filters") == null ? "" : info.fetch_named ("filters")));
return false;
});
}
......@@ -223,14 +228,19 @@ namespace Valse {
for_count++;
}
print (result);
// Model variables
Regex var_re = new Regex ("{{ *" + model_keyword + "\\.(?<prop>[A-Za-z0-9_.\\[\\]]*)(\\|(?<filter>[[:graph:]]*?)( (?<params>\".*?\"))?)? *}}");
Regex var_re = new Regex ("{{ *" + model_keyword + "(?<prop>[\\w.\\[\\]]*)(\\|(?<filter>[[:graph:]]*?)( (?<params>\".*?\"))?)? *}}");
MatchInfo var_info;
bool go_var = var_re.match (result, 0, out var_info);
while (go_var) {
string prop_name = var_info.fetch_named ("prop");
string prop_val = mdl [prop_name];
if (prop_val == null && DEBUG) {
prop_val = "[WARNING] %s%s is null.".printf (model_keyword, prop_name.has_prefix ("[") ? prop_name : prop_name);
}
string? var_filters = var_info.fetch_named ("filter");
string? params = var_info.fetch_named ("params");
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment