Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

added support for Rails like flash messages. See example blog

  • Loading branch information...
commit 3757f3c2cb4cf36cf8c0fa4665de687c67271386 1 parent a5ab400
Dave Bryson authored
17 example/blog/src/blog_example_web.erl
View
@@ -34,13 +34,13 @@ loop(Req) ->
%% {error,_}
case beepbeep:dispatch(Env) of
{render,View,Data} ->
- {ok,Content} = render_template(View,Data),
+ {ok,Content} = render_template(View,Data,Env),
Req:respond({200,
[{"Content-Type","text/html"}|[get_cookie(Env)]],
Content});
{render,View,Data,Options} ->
{Status,ContentType,Headers} = extract_options(Options),
- {ok,Content} = render_template(View,Data),
+ {ok,Content} = render_template(View,Data,Env),
Req:respond({Status,
[{"Content-Type",ContentType}|[get_cookie(Env)|Headers]],
Content});
@@ -60,9 +60,11 @@ loop(Req) ->
Req:respond({500,[],"Server Error"})
end.
-render_template(ViewFile,Data) ->
+render_template(ViewFile,Data,Env) ->
+ %% Copy flash into Data and clear from Session
+ Data1 = set_and_clear_flash(Data,Env),
FullPathToFile = filename:join([blog_example_deps:local_path(["views"]),ViewFile]),
- beepbeep:render_template(FullPathToFile,ViewFile,Data).
+ beepbeep:render_template(FullPathToFile,ViewFile,Data1).
extract_options(Options) ->
{proplists:get_value(status,Options,200),
@@ -72,6 +74,13 @@ extract_options(Options) ->
get_cookie(Env) ->
mochiweb_cookies:cookie(?BEEPBEEP_SID,beepbeep_args:get_session_id(Env),[{path, "/"}]).
+set_and_clear_flash(Data,Env) ->
+ case beepbeep_args:get_flash(Env) of
+ none -> Data;
+ Flash ->
+ [{flash,Flash}|Data]
+ end.
+
setup_session(Req,Env) ->
SessionKey = beepbeep_session_server:new_session(Req:get_cookie_value(?BEEPBEEP_SID)),
beepbeep_args:set_session_id(SessionKey,Env).
16 example/blog/src/home_controller.erl
View
@@ -20,8 +20,20 @@ handle_request("new",[]) ->
handle_request("create",[]) ->
Title = beepbeep_args:get_param("post_title",Env),
Body = beepbeep_args:get_param("post_body",Env),
- blog_database:insert(Title,Body),
- {redirect,"/"}.
+
+ %% Example of validation. Require a Title
+ if
+ Title =:= undefined orelse length(Title) =:= 0 ->
+ %% Blank
+ %% Set flash message for the template
+ beepbeep_args:flash({notice,"Title Required!"},Env),
+ {redirect,"/home/new"};
+ true ->
+ %% Set flash message for the template
+ beepbeep_args:flash({notice,"Post Created!"},Env),
+ blog_database:insert(Title,Body),
+ {redirect,"/"}
+ end.
%% Shows how to filter on certain actions in this controller
before_filter() ->
5 example/blog/views/flash.html
View
@@ -0,0 +1,5 @@
+{% if flash %}
+ {% for k,message in flash %}
+ <p>{{ message }}</p>
+ {% endfor %}
+{% endif %}
4 example/blog/views/home/index.html
View
@@ -1,6 +1,8 @@
{% extends "../base.html" %}
{% block content %}
-
+
+{% include "flash.html" %}
+
{% if data %}
{% for i in data %}
<h2>{{ i.title }}</h2>
2  example/blog/views/home/new.html
View
@@ -1,6 +1,8 @@
{% extends "../base.html" %}
{% block content %}
+{% include "flash.html" %}
+
<h2>Add a Post</h2>
<form action="/home/create" method="post">
Title:<br/>
3  priv/skel/src/home_controller.erl
View
@@ -11,6 +11,9 @@ handle_request("index",[]) ->
handle_request("show",[Year]) ->
Sid = beepbeep_args:get_session_id(Env),
Name = beepbeep_args:get_param("name",Env),
+
+ beepbeep_args:flash({notice,"Example of a flash message between actions. See flash.html!"},Env),
+
{render,"home/show.html",[{year,Year},{sid,Sid},{name,Name}]}.
17 priv/skel/src/skel_web.erl
View
@@ -34,13 +34,13 @@ loop(Req) ->
%% {error,_}
case beepbeep:dispatch(Env) of
{render,View,Data} ->
- {ok,Content} = render_template(View,Data),
+ {ok,Content} = render_template(View,Data,Env),
Req:respond({200,
[{"Content-Type","text/html"}|[get_cookie(Env)]],
Content});
{render,View,Data,Options} ->
{Status,ContentType,Headers} = extract_options(Options),
- {ok,Content} = render_template(View,Data),
+ {ok,Content} = render_template(View,Data,Env),
Req:respond({Status,
[{"Content-Type",ContentType}|[get_cookie(Env)|Headers]],
Content});
@@ -60,9 +60,11 @@ loop(Req) ->
Req:respond({500,[],"Server Error"})
end.
-render_template(ViewFile,Data) ->
+render_template(ViewFile,Data,Env) ->
+ %% Copy flash into Data and clear from Session
+ Data1 = set_and_clear_flash(Data,Env),
FullPathToFile = filename:join([skel_deps:local_path(["views"]),ViewFile]),
- beepbeep:render_template(FullPathToFile,ViewFile,Data).
+ beepbeep:render_template(FullPathToFile,ViewFile,Data1).
extract_options(Options) ->
{proplists:get_value(status,Options,200),
@@ -72,6 +74,13 @@ extract_options(Options) ->
get_cookie(Env) ->
mochiweb_cookies:cookie(?BEEPBEEP_SID,beepbeep_args:get_session_id(Env),[{path, "/"}]).
+set_and_clear_flash(Data,Env) ->
+ case beepbeep_args:get_flash(Env) of
+ none -> Data;
+ Flash ->
+ [{flash,Flash}|Data]
+ end.
+
setup_session(Req,Env) ->
SessionKey = beepbeep_session_server:new_session(Req:get_cookie_value(?BEEPBEEP_SID)),
beepbeep_args:set_session_id(SessionKey,Env).
5 priv/skel/views/flash.html
View
@@ -0,0 +1,5 @@
+{% if flash %}
+ {% for k,message in flash %}
+ <p>{{ message }}</p>
+ {% endfor %}
+{% endif %}
3  priv/skel/views/home/index.html
View
@@ -1,4 +1,7 @@
{% extends "../base.html" %}
{% block content %}
Message is: {{ data }}
+ <br/>
+ <br/>
+ <a href="/home/show/2009?name=dave">Show more...</a>
{% endblock %}
3  priv/skel/views/home/show.html
View
@@ -1,5 +1,8 @@
{% extends "../base.html" %}
{% block content %}
+
+{% include "../flash.html" %}
+
You choose year: {{ year }}
<p>
Session id: {{ sid }}
33 src/beepbeep_args.erl
View
@@ -23,7 +23,10 @@
get_session_data/2,
get_all_session_data/1,
get_action/1,
- set_action/2]).
+ set_action/2,
+ flash/2,
+ get_flash/1]).
+
-author('Dave Bryson <http://weblog.miceda.org>').
%%
@@ -102,7 +105,33 @@ get_all_headers(Env) ->
%%
get_param(Key,Env) ->
Params = proplists:get_value("beepbeep.data",Env),
- proplists:get_value(Key,Params,"?").
+ proplists:get_value(Key,Params).
+
+%%
+%% @doc Set a 'flash' message for use in your template. All flash message are wrapped in a List
+%%
+flash(Term,Env) ->
+ Flash = case get_session_data(beepbeep_flash,Env) of
+ undefined ->
+ [Term];
+ ExistingFlash ->
+ [Term|ExistingFlash]
+ end,
+ set_session_data(beepbeep_flash,Flash,Env).
+
+
+%% Get and clear the flash
+get_flash(Env) ->
+ Sid = get_session_id(Env),
+ case get_session_data(beepbeep_flash,Env) of
+ undefined ->
+ %% No flash data
+ none;
+ Data ->
+ beepbeep_session_server:remove_session_data(Sid,beepbeep_flash),
+ Data
+ end.
+
%%
%% @doc Get the current session id
23 src/beepbeep_session_server.erl
View
@@ -13,7 +13,7 @@
-behaviour(gen_server).
--export([start/0,new_session/1,get_session_data/1,set_session_data/3,delete_session/1]).
+-export([start/0,new_session/1,get_session_data/1,set_session_data/3,delete_session/1,remove_session_data/2]).
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
terminate/2, code_change/3]).
@@ -43,6 +43,9 @@ set_session_data(Sid,Key,Value) ->
delete_session(Sid) ->
gen_server:call(?MODULE,{delete_session,Sid}).
+remove_session_data(Sid,Key) ->
+ gen_server:call(?MODULE,{remove_session_data,Sid,Key}).
+
%%% Callbacks
handle_call({new_session,Cookie}, _From, _State) ->
@@ -87,6 +90,24 @@ handle_call({set_session_data,Sid,Key,Value},_From,_State) ->
handle_call({delete_session,Sid},_From,_State) ->
ets:delete(?MODULE,Sid),
+ {reply,ok,undefined};
+
+
+handle_call({remove_session_data,Sid,Key},_From,_State) ->
+ Data = case ets:lookup(?MODULE,Sid) of
+ [S] ->
+ S#beep_session.data;
+ [] -> []
+ end,
+ Data1 = case proplists:is_defined(Key,Data) of
+ true ->
+ proplists:delete(Key,Data);
+ false ->
+ Data
+ end,
+
+ ets:insert(?MODULE,#beep_session{sid=Sid,data=Data1,ttl=0}),
+
{reply,ok,undefined}.
Please sign in to comment.
Something went wrong with that request. Please try again.