Thread: [Erlangweb-users] Problem with DTL and password field in forms
                
                Brought to you by:
                
                    etcerlangweb,
                    
                
                    paulgray
                    
                
            
            
        
        
        
    | 
     
      
      
      From: Gilbert R. <erl...@la...> - 2010-02-18 17:49:35
       
   | 
Hello,
I am working on a website in erlang-web, and here are two problems I stumbled upon that
required modification or erlang-web source code.
First, password fields in form with confirmation would alway return an error saying 
the password didn't match. It was caused by wtype_password:validate who would
feed ["password", "password"] to utf8_api:ustring. The result is one string: 
"passwordpassword" instead of ["password", "password"].
Here is the change I made to correct this:
--- a/lib/wparts-1.4/src/wtype_password.erl     Tue Jan 19 10:58:02 2010 +0100
+++ b/lib/wparts-1.4/src/wtype_password.erl     Thu Feb 18 18:09:11 2010 +0100
@@ -45,8 +45,13 @@
             end
     end;
+validate({Types, [RawString1, RawString2]}) when is_list(RawString1) and is_list(RawString2) ->
+  validate2({Types, [utf8_api:ustring(RawString1), utf8_api:ustring(RawString2)]});
+
 validate({Types, RawString}) when is_list(RawString) ->
-    String = utf8_api:ustring(RawString),
+  validate2({Types, utf8_api:ustring(RawString)}).
+
+validate2({Types, String}) when is_list(String) ->
     case wpart_valid:is_private(Types) of
        true ->
            {ok, String};
My second problem was with DTL templates. While wpart template's parent include 
mecanism works well with templates sepparated in subdirectories, DTL caused problems.
Here is an exemple of the directory structure that cause problem:
- templates
  - base
    - base.html
    - half.html
    - frontpage.html
  - article
    - article.html
while an extends "half.html" works well in frontpage.html , it does not in article.html.
And if you do an extends "base/base.html" in it, erlydtl tries to load article/base/base.html
Trying to do extends "../base/half.html" doesn't fix the problem as it then tries to load
article/base.html inside half.html
In order to make DTL have the same behavior, I give DTL the template directory 
throught its doc_root option.
--- a/lib/eptic-1.4/src/e_cache_ets.erl Tue Jan 19 10:58:02 2010 +0100
+++ b/lib/eptic-1.4/src/e_cache_ets.erl Thu Feb 18 18:09:11 2010 +0100
@@ -93,7 +93,7 @@
     XML;
 cache(File, erlydtl_expander) ->
     Mod = list_to_atom(string:join(string:tokens(File, "/.-"), "")),
-    case erlydtl:compile(File, Mod) of
+    case erlydtl:compile(File, Mod, [{doc_root, e_conf:template_root()}]) of
         ok ->
             ets:insert(?MODULE, {File, {date(), time()}, Mod}),
             Mod;
Erlang-web is otherwise quite nice to work with. Thanks for the good work.
 | 
| 
     
      
      
      From: Michal P. <mic...@er...> - 2010-02-19 08:45:06
       
   | 
Hi,
We are very happy with the patches you provided: 
good job! Both of them have been applied on the 
Erlang Web's sources (changeset 4a2530c5e23d) and 
will be included in the next release.
Thank you for your contribution!
----- "Gilbert Roulot" <erl...@la...> wrote:
> Hello,
> 
> I am working on a website in erlang-web, and here are two problems I
> stumbled upon that
> required modification or erlang-web source code.
> 
> First, password fields in form with confirmation would alway return an
> error saying 
> the password didn't match. It was caused by wtype_password:validate
> who would
> feed ["password", "password"] to utf8_api:ustring. The result is one
> string: 
> "passwordpassword" instead of ["password", "password"].
> 
> Here is the change I made to correct this:
> --- a/lib/wparts-1.4/src/wtype_password.erl     Tue Jan 19 10:58:02
> 2010 +0100
> +++ b/lib/wparts-1.4/src/wtype_password.erl     Thu Feb 18 18:09:11
> 2010 +0100
> @@ -45,8 +45,13 @@
>              end
>      end;
> 
> +validate({Types, [RawString1, RawString2]}) when is_list(RawString1)
> and is_list(RawString2) ->
> +  validate2({Types, [utf8_api:ustring(RawString1),
> utf8_api:ustring(RawString2)]});
> +
>  validate({Types, RawString}) when is_list(RawString) ->
> -    String = utf8_api:ustring(RawString),
> +  validate2({Types, utf8_api:ustring(RawString)}).
> +
> +validate2({Types, String}) when is_list(String) ->
>      case wpart_valid:is_private(Types) of
>         true ->
>             {ok, String};
> 
> My second problem was with DTL templates. While wpart template's
> parent include 
> mecanism works well with templates sepparated in subdirectories, DTL
> caused problems.
> 
> Here is an exemple of the directory structure that cause problem:
> - templates
>   - base
>     - base.html
>     - half.html
>     - frontpage.html
>   - article
>     - article.html
> 
> while an extends "half.html" works well in frontpage.html , it does
> not in article.html.
> And if you do an extends "base/base.html" in it, erlydtl tries to load
> article/base/base.html
> Trying to do extends "../base/half.html" doesn't fix the problem as it
> then tries to load
> article/base.html inside half.html
> 
> In order to make DTL have the same behavior, I give DTL the template
> directory 
> throught its doc_root option.
> 
> --- a/lib/eptic-1.4/src/e_cache_ets.erl Tue Jan 19 10:58:02 2010
> +0100
> +++ b/lib/eptic-1.4/src/e_cache_ets.erl Thu Feb 18 18:09:11 2010
> +0100
> @@ -93,7 +93,7 @@
>      XML;
>  cache(File, erlydtl_expander) ->
>      Mod = list_to_atom(string:join(string:tokens(File, "/.-"), "")),
> -    case erlydtl:compile(File, Mod) of
> +    case erlydtl:compile(File, Mod, [{doc_root,
> e_conf:template_root()}]) of
>          ok ->
>              ets:insert(?MODULE, {File, {date(), time()}, Mod}),
>              Mod;
> 
> 
> 
> Erlang-web is otherwise quite nice to work with. Thanks for the good
> work.
> 
> ------------------------------------------------------------------------------
> Download Intel® Parallel Studio Eval
> Try the new software tools for yourself. Speed compiling, find bugs
> proactively, and fine-tune applications for parallel performance.
> See why Intel Parallel Studio got high marks during beta.
> http://p.sf.net/sfu/intel-sw-dev
> _______________________________________________
> Erlangweb-users mailing list
> Erl...@li...
> https://lists.sourceforge.net/lists/listinfo/erlangweb-users
> http://www.erlang-web.org/
Best regards,
Michal
---------------------------------------------------
---------------------------------------------------
WE'VE CHANGED NAMES!
Since January 1st 2010 Erlang Training and Consulting Ltd. has become ERLANG SOLUTIONS LTD.
www.erlang-solutions.com
 | 
| 
     
      
      
      From: Gilbert R. <erl...@la...> - 2010-02-19 17:54:51
       
   | 
On Fri, Feb 19, 2010 at 08:44:50AM +0000, Michal Ptaszek wrote:
> Hi,
> 
> We are very happy with the patches you provided: 
> good job! Both of them have been applied on the 
> Erlang Web's sources (changeset 4a2530c5e23d) and 
> will be included in the next release.
> 
> Thank you for your contribution!
>
You're welcome.
I found another issue with DTL. When I use {{ variable }} in a template,
and set the value with wpart:fset("variable", "Hello"). Erlydtl doesn't find
it.
What happens is erlydtl_runtime:fetch_value is looking for the atom viariable in
the dictionary, but the dictionnary has string keys. wpart:fset(variable, "Hello") 
doesn't work.
In order to fix this, I modified the dictionnary passed to erlydtl_runtime to turn 
its keys from strings into atoms:
--- a/lib/wpart-1.4/src/erlydtl_expander.erl    Tue Jan 19 10:58:02 2010 +0100
+++ b/lib/wpart-1.4/src/erlydtl_expander.erl    Fri Feb 19 18:32:39 2010 +0100
@@ -24,7 +24,10 @@
 
 process_xml(Mod) ->
     case e_dict:get_state() of
-        {ok, Dict} ->
+        {ok, Dict1} ->
+            % turn strings in Dict1 into atoms. Because erldtl
+            % looks for atoms in it
+            Dict = lists:map(fun ({Key, Value}) -> {list_to_atom(Key), Value} end, Dict1),
             case Mod:render(Dict) of
                 {ok, Html} ->
                     [
I'll contribute patches as I go if more are needed. I guess DTL support doesn't see lots 
of use along erlang-web users  :) 
Regards
 
 | 
| 
     
      
      
      From: Michal P. <mic...@er...> - 2010-02-23 08:23:56
       
   | 
Hi Gilbert,
----- "Gilbert Roulot" <erl...@la...> wrote:
> On Fri, Feb 19, 2010 at 08:44:50AM +0000, Michal Ptaszek wrote:
> > Hi,
> > 
> > We are very happy with the patches you provided: 
> > good job! Both of them have been applied on the 
> > Erlang Web's sources (changeset 4a2530c5e23d) and 
> > will be included in the next release.
> > 
> > Thank you for your contribution!
> >
> 
> You're welcome.
> 
> I found another issue with DTL. When I use {{ variable }} in a
> template,
> and set the value with wpart:fset("variable", "Hello"). Erlydtl
> doesn't find
> it.
> 
> What happens is erlydtl_runtime:fetch_value is looking for the atom
> viariable in
> the dictionary, but the dictionnary has string keys.
> wpart:fset(variable, "Hello") 
> doesn't work.
> In order to fix this, I modified the dictionnary passed to
> erlydtl_runtime to turn 
> its keys from strings into atoms:
> 
> --- a/lib/wpart-1.4/src/erlydtl_expander.erl    Tue Jan 19 10:58:02
> 2010 +0100
> +++ b/lib/wpart-1.4/src/erlydtl_expander.erl    Fri Feb 19 18:32:39
> 2010 +0100
> @@ -24,7 +24,10 @@
>  
>  process_xml(Mod) ->
>      case e_dict:get_state() of
> -        {ok, Dict} ->
> +        {ok, Dict1} ->
> +            % turn strings in Dict1 into atoms. Because erldtl
> +            % looks for atoms in it
> +            Dict = lists:map(fun ({Key, Value}) ->
> {list_to_atom(Key), Value} end, Dict1),
>              case Mod:render(Dict) of
>                  {ok, Html} ->
>                      [
> 
Which version of erlydtl are you using?
The one that is bundled with standard Erlang Web contains a patch
for erlydtl_runtime: when accessing the request dictionary, the
key will be first transformed into a string, an if the value is not
found, the original atom will be used (erlydtl_runtime:18).
> 
> I'll contribute patches as I go if more are needed. I guess DTL
> support doesn't see lots 
> of use along erlang-web users  :) 
> 
> 
> Regards
Thank you!
Michal Ptaszek
---------------------------------------------------
---------------------------------------------------
WE'VE CHANGED NAMES!
Since January 1st 2010 Erlang Training and Consulting Ltd. has become ERLANG SOLUTIONS LTD.
www.erlang-solutions.com
 | 
| 
     
      
      
      From: Gilbert R. <erl...@la...> - 2010-02-24 17:02:59
       
   | 
On Tue, Feb 23, 2010 at 08:23:42AM +0000, Michal Ptaszek wrote:
> > You're welcome.
> > 
> > I found another issue with DTL. When I use {{ variable }} in a
> > template,
> > and set the value with wpart:fset("variable", "Hello"). Erlydtl
> > doesn't find
> > it.
> > 
...
> 
> Which version of erlydtl are you using?
> 
> The one that is bundled with standard Erlang Web contains a patch
> for erlydtl_runtime: when accessing the request dictionary, the
> key will be first transformed into a string, an if the value is not
> found, the original atom will be used (erlydtl_runtime:18).
> 
Hello,
I am using erlydtl 0.5.3. I do see the code around line 18, but in my
case find_value at line 5 is used or fetch_value at line 38. Both fail.
fetch_value is called by the use of {{ hello }}
     fetch_value(Key, Data) ->
         case find_value(Key, Data) of
             undefined ->
                 throw({undefined_variable, Key});
             Val ->
                 Val
         end.
Key: hello
Data: [{"get",[]},
            {"__https",false},
            {"__ip",{194,79,152,115}},
            {"__cookies",
             [{"eptic_cookie","nonode@nohost-9933714052825178113"}]},
            {"session",[]},
            {"__path",[]},
            {"__cookie_key","nonode@nohost-9933714052825178113"},
            {"__controller",front},
            {"hello","Hello world!"}]
find_value at line 5 is called when using {% if hello %} Hello {% endif %}
     find_value(Key, L) when is_list(L) ->
         proplists:get_value(Key, L);
Key: hello
L: [{"get",[]},
            {"__https",false},
            {"__ip",{194,79,152,115}},
            {"__cookies",
             [{"eptic_cookie","nonode@nohost-9933714052825178113"}]},
            {"session",[]},
            {"__path",[]},
            {"__cookie_key","nonode@nohost-9933714052825178113"},
            {"__controller",front},
            {"hello","Hello world!"}]
Here's the relevant controller code:
index(_Args) ->
    wpart:fset("hello", "Hello world!"),
    {template, "test/index.html"}.
And the template
<html>
<head>
</head>
<body>
<h1> {% if hello %} Hello ! {% endif %}</h1>
</body>
</html>
This is on a fresh install, downloaded with 
hg clone https://bitbucket.org/etc/erlang-web/
Regards
 |