Erlang/OTP Forums

Author Message

<  Erlyweb mailing list  ~  Subcontainers

ketralnis
Posted: Sat Sep 15, 2007 1:51 am Reply with quote
User Joined: 20 Jul 2007 Posts: 151 Location: San Francisco, CA
I've re-done this a few times without luck, and I've given up and am
asking for group for help Smile

I have a sidebar that needs to display on all pages, (except those
that end in the regex "/.json\?.*", but I'll get to that part later,
I just mention it in case I'm unwittingly doing something that will
prevent that in the future). That seems easy enough, I just have a
myapp_app_controller:hook/1 that looks like:

hook(A) ->
% render /post/index for /
A1=case yaws_arg:appmoddata(A) of
"/" ->
yaws_arg:appmoddata(A, "/post/index");
_ ->
A
end,

Ewc = erlyweb:get_initial_ewc({ewc, A1}),
case Ewc of
{page,_}=Page ->
Page; % regular pages like /myapp.js should be able to just
return
_ ->
{phased, {ewc,
html_container,
[A1,
{ewc,
sidebar_container,
[A1,
[Ewc]]}]},
fun(_Ewc,Data) ->
{data,Data}
end}
end.

myapp_html_container:index/2 looks like:

index(_A, Ewc) ->
Ewc.

And myapp_sidebar_container looks like:

index(A,Ewc) ->
[{ewc,sidebar,[A]},
Ewc].

Just in case it matters, sidebar_controller:index/1 looks like:

index(A) ->
SideComponent=case my_app_util:require_login(A) of
{no_session,_} -> {ewc,sidebar,not_logged_in,
['_']};
User -> {ewc,sidebar,logged_in,[User]}
end,
[{data,[0]},
SideComponent].

(There are two functions in this module, not_logged_in and logged_in,
that return {data,Args} tuples.)

Currently, this works. If it's not the best way to do it, let me
know, but it does work. A request for /post/popular renders with the
sidebar and appropriate controller and view, and all is happiness and
light.

However, I need regular controllers to be able to return headers (in
particular, they need to be able to send redirects and set cookies,
preferably at the same time). At present, this breaks with:

{invalid_response,
[{header,
{set_cookie,
["myapp_session",61,"1",
59,32,112,97,116,104,61,"/"]}},
{redirect_local,[[],47,"users/show/121"]}],
"Response values other than 'data' and 'ewc' tuples must
be enclosed a 'response' tuple. In addition, subcomponents may only
return 'data' and/or 'ewc' tuples."


It's not currently the case that if a controller returns a header
that it always returns one without a body, but I suppose that I could
arrange that.

I'm not necessarily attached to the setup of
html_container -> sidebar_container -> [sidebar,regular_controller]
(which is what I have now) I'd be just as happy with the setup of
html_container -> [sidebar,regular_controller]
with no sidebar_container at all, at least for now.

I do need dynamic information in my sidebar, but I don't need it to
be able to return headers. I need access to the Request ("A" in the
above functions) from the sidebar and from the regular controllers.

I think that this is what the {phased, {ewc...}} magic is all about,
but I'm not positive that I'm using it correctly above, and I can't
figure out exactly how to craft it. The documentation isn't very
specific.


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "erlyweb" group.
To post to this group, send email to erlyweb@googlegroups.com
To unsubscribe from this group, send email to erlyweb-unsubscribe@googlegroups.com
For more options, visit this group at http://groups.google.com/group/erlyweb?hl=en
-~----------~----~----~----~------~----~------~--~---

Post recived from mailinglist
View user's profile Send private message AIM Address
Guest
Posted: Sun Sep 16, 2007 5:47 pm Reply with quote
Guest
Try changing your 'phased' expression to this:

{phased, Ewc,
fun(_Ewc, Data) ->
{ewc, html_container,
[A1, {ewc, sidebar_container, [A1, {data, Data}]}]}
end}

To clarify what the documentation says, when you return {phased, Ewc,
Fun}, the 'Ewc' component may return headers, but the component
returned by Fun may not. Basically, you're telling ErlyWeb, "Render
the requested component and send to the browser any headers it may
return. Also, if it returns a body, do this further processing on the
body before sending it to the browser. This additional processing may
not produce any headers, because only the requested Ewc may return
headers."

I hope this helps.

Yariv





On 9/14/07, David King <dking@ketralnis.com> wrote:
>
> I've re-done this a few times without luck, and I've given up and am
> asking for group for help Smile
>
> I have a sidebar that needs to display on all pages, (except those
> that end in the regex "/.json\?.*", but I'll get to that part later,
> I just mention it in case I'm unwittingly doing something that will
> prevent that in the future). That seems easy enough, I just have a
> myapp_app_controller:hook/1 that looks like:
>
> hook(A) ->
> % render /post/index for /
> A1=case yaws_arg:appmoddata(A) of
> "/" ->
> yaws_arg:appmoddata(A, "/post/index");
> _ ->
> A
> end,
>
> Ewc = erlyweb:get_initial_ewc({ewc, A1}),
> case Ewc of
> {page,_}=Page ->
> Page; % regular pages like /myapp.js should be able to just
> return
> _ ->
> {phased, {ewc,
> html_container,
> [A1,
> {ewc,
> sidebar_container,
> [A1,
> [Ewc]]}]},
> fun(_Ewc,Data) ->
> {data,Data}
> end}
> end.
>
> myapp_html_container:index/2 looks like:
>
> index(_A, Ewc) ->
> Ewc.
>
> And myapp_sidebar_container looks like:
>
> index(A,Ewc) ->
> [{ewc,sidebar,[A]},
> Ewc].
>
> Just in case it matters, sidebar_controller:index/1 looks like:
>
> index(A) ->
> SideComponent=case my_app_util:require_login(A) of
> {no_session,_} -> {ewc,sidebar,not_logged_in,
> ['_']};
> User -> {ewc,sidebar,logged_in,[User]}
> end,
> [{data,[0]},
> SideComponent].
>
> (There are two functions in this module, not_logged_in and logged_in,
> that return {data,Args} tuples.)
>
> Currently, this works. If it's not the best way to do it, let me
> know, but it does work. A request for /post/popular renders with the
> sidebar and appropriate controller and view, and all is happiness and
> light.
>
> However, I need regular controllers to be able to return headers (in
> particular, they need to be able to send redirects and set cookies,
> preferably at the same time). At present, this breaks with:
>
> {invalid_response,
> [{header,
> {set_cookie,
> ["myapp_session",61,"1",
> 59,32,112,97,116,104,61,"/"]}},
> {redirect_local,[[],47,"users/show/121"]}],
> "Response values other than 'data' and 'ewc' tuples must
> be enclosed a 'response' tuple. In addition, subcomponents may only
> return 'data' and/or 'ewc' tuples."
>
>
> It's not currently the case that if a controller returns a header
> that it always returns one without a body, but I suppose that I could
> arrange that.
>
> I'm not necessarily attached to the setup of
> html_container -> sidebar_container -> [sidebar,regular_controller]
> (which is what I have now) I'd be just as happy with the setup of
> html_container -> [sidebar,regular_controller]
> with no sidebar_container at all, at least for now.
>
> I do need dynamic information in my sidebar, but I don't need it to
> be able to return headers. I need access to the Request ("A" in the
> above functions) from the sidebar and from the regular controllers.
>
> I think that this is what the {phased, {ewc...}} magic is all about,
> but I'm not positive that I'm using it correctly above, and I can't
> figure out exactly how to craft it. The documentation isn't very
> specific.
>
>
> >
>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "erlyweb" group.
To post to this group, send email to erlyweb@googlegroups.com
To unsubscribe from this group, send email to erlyweb-unsubscribe@googlegroups.com
For more options, visit this group at http://groups.google.com/group/erlyweb?hl=en
-~----------~----~----~----~------~----~------~--~---

Post recived from mailinglist

Display posts from previous:  

All times are GMT
Page 1 of 1
This forum is locked: you cannot post, reply to, or edit topics.

Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum