I really like this, because it allows me to separate the view-logic for things like contents and comments out from each other, which makes changing the behaviour of these quite simple, localised, and re- useable. (In fact, as I re-write portions of erlyweb for my purposes, the component system is something that I haven't touched at all because it works quite well.) However, it has an inherent limitation: none of those functions can return data to be rendered outside of their components. In my case, I'd like to be able to set <title> tags, which means that the top-level controller function (by top- level, I mean the one referenced in the URL; like in /post/show/12, the top-level controller function would be show/2) needs to somehow return information to be rendered outside its little sandbox.
I see that Vimagi doesn't set <title> tags (neither <http:// vimagi.com/p/0PXmMRe6hGV> nor <http://vimagi.com/users/feeling3_4> set one, at least, and those are where I'd expect to see them), but BeerRiot does (<http://beerriot.com/beer/view/1751>; BTW I do love the idea of having a controller called "beer_controller").
It isn't limited to <title>s (for instance, I may want to alert the browser that an RSS equivalent of this page exists, or what the caching TTL is for this page in the HTTP headers). Conceptually, how would one go about returning this extra information, while still using the component system (I now have a lot of code that uses it that I'd like to not re-write :) )?
- A while ago somebody asked a similar question and Bryan Fink suggested returning a tuple of the form {title, Title} from the view function, and then later picking up this tuple in the html_container_controller and using its value to set the <title> tag.
- Use the process dictionary to store any values that need to be used later outside the current component.
Let me know if it works for you.
Btw, just curious, what parts of ErlyWeb are you rewriting?
Yariv
On Nov 16, 2007 10:19 AM, David King <dk...@ketralnis.com> wrote:
> I really like this, because it allows me to separate the view-logic > for things like contents and comments out from each other, which > makes changing the behaviour of these quite simple, localised, and re- > useable. (In fact, as I re-write portions of erlyweb for my purposes, > the component system is something that I haven't touched at all > because it works quite well.) However, it has an inherent limitation: > none of those functions can return data to be rendered outside of > their components. In my case, I'd like to be able to set <title> > tags, which means that the top-level controller function (by top- > level, I mean the one referenced in the URL; like in /post/show/12, > the top-level controller function would be show/2) needs to somehow > return information to be rendered outside its little sandbox.
> I see that Vimagi doesn't set <title> tags (neither <http:// > vimagi.com/p/0PXmMRe6hGV> nor <http://vimagi.com/users/feeling3_4> > set one, at least, and those are where I'd expect to see them), but > BeerRiot does (<http://beerriot.com/beer/view/1751>; BTW I do love > the idea of having a controller called "beer_controller").
> It isn't limited to <title>s (for instance, I may want to alert the > browser that an RSS equivalent of this page exists, or what the > caching TTL is for this page in the HTTP headers). Conceptually, how > would one go about returning this extra information, while still > using the component system (I now have a lot of code that uses it > that I'd like to not re-write :) )?
> - A while ago somebody asked a similar question and Bryan Fink > suggested returning a tuple of the form {title, Title} from the view > function, and then later picking up this tuple in the > html_container_controller and using its value to set the <title> tag.
That does only take into account <title>s, and not any other metadata. And doesn't using erltl preclude returning something useful from a view function other than the data itself? And then the component receiving the rendered page would have to walk the resultant IOlist looking for it, rather than being able to pattern- match it. That just seems very brittle. I guess I could have one view function like
And then another module with the erltl itself, but that also seems rather contrived. Maybe it makes sense to have a list in the html_container of controllers:functions that require meta-data and know to call them and ask for the <title>, but that also seems brittle as the controllers:functions change.
> - Use the process dictionary to store any values that need to be used > later outside the current component.
Ech, that sounds terrible, and it relies on Erlyweb doing all of the work in one process, which I don't see being a permanently viable assumption.
Would it make sense to hack in a return value from the controller function? Something like:
(with a better name, since "return_with_meta" doesn't sound all that great to me). But then I'm not sure how one could extract that from the rendered view. Maybe pass only the second tuple-item to the view, and pass both the first item and the second to the controller. Many many questions. I'll have to think about it some more.
> Btw, just curious, what parts of ErlyWeb are you rewriting?
Mostly large swaths of erlydb and erlydb_mnesia (in ways that make it work better with mnesia, but that make it incompatible with other DBMSs, which of course the current incarnation avoids). I may end up having to revert this if mnesia can't handle millions of objects, with which it seems to be having trouble once they start to become larger objects than simple tuples. I've hacked in a few return and 'EXIT' handlers from controller functions for special circumstances, like when I want to return a 400- or 500-level HTTP response along with other information. I might try to hack in some WebDAV handling stuff, as I'm considering writing a desktop client for my app in the future and that seems the easiest way to get the data to and from the desktop client. I haven't decided what that's going to look like yet. It looks like I may be hacking in a return value to return extended information to parent controller functions, like <title> tags. but I want to decide how it should work, first. It sounds like it's just not a concern to the two biggest users of erlyweb
>> I really like this, because it allows me to separate the view-logic >> for things like contents and comments out from each other, which >> makes changing the behaviour of these quite simple, localised, and >> re- >> useable. (In fact, as I re-write portions of erlyweb for my purposes, >> the component system is something that I haven't touched at all >> because it works quite well.) However, it has an inherent limitation: >> none of those functions can return data to be rendered outside of >> their components. In my case, I'd like to be able to set <title> >> tags, which means that the top-level controller function (by top- >> level, I mean the one referenced in the URL; like in /post/show/12, >> the top-level controller function would be show/2) needs to somehow >> return information to be rendered outside its little sandbox.
>> I see that Vimagi doesn't set <title> tags (neither <http:// >> vimagi.com/p/0PXmMRe6hGV> nor <http://vimagi.com/users/feeling3_4> >> set one, at least, and those are where I'd expect to see them), but >> BeerRiot does (<http://beerriot.com/beer/view/1751>; BTW I do love >> the idea of having a controller called "beer_controller").
>> It isn't limited to <title>s (for instance, I may want to alert the >> browser that an RSS equivalent of this page exists, or what the >> caching TTL is for this page in the HTTP headers). Conceptually, how >> would one go about returning this extra information, while still >> using the component system (I now have a lot of code that uses it >> that I'd like to not re-write :) )?
> > - A while ago somebody asked a similar question and Bryan Fink > > suggested returning a tuple of the form {title, Title} from the view > > function, and then later picking up this tuple in the > > html_container_controller and using its value to set the <title> tag.
> That does only take into account <title>s, and not any other > metadata. And doesn't using erltl preclude returning something useful > from a view function other than the data itself? And then the > component receiving the rendered page would have to walk the > resultant IOlist looking for it, rather than being able to pattern- > match it. That just seems very brittle. I guess I could have one view > function like
> And then another module with the erltl itself, but that also seems > rather contrived. Maybe it makes sense to have a list in the > html_container of controllers:functions that require meta-data and > know to call them and ask for the <title>, but that also seems > brittle as the controllers:functions change.
> > - Use the process dictionary to store any values that need to be used > > later outside the current component.
> Ech, that sounds terrible, and it relies on Erlyweb doing all of the > work in one process, which I don't see being a permanently viable > assumption.
> Would it make sense to hack in a return value from the controller > function? Something like:
> (with a better name, since "return_with_meta" doesn't sound all that > great to me). But then I'm not sure how one could extract that from > the rendered view. Maybe pass only the second tuple-item to the view, > and pass both the first item and the second to the controller. Many > many questions. I'll have to think about it some more.
I think that's a very good idea. There may be issues if multiple subcomponents return the same metadata (or at least the same keys), but then I guess ErlyWeb will just pick the first one. There are also questions regarding how the metadata will be passed around (should it be passed to a components siblings and or only its parents?) -- I'll have to think about it and make sure all the bases are covered.
> > Btw, just curious, what parts of ErlyWeb are you rewriting?
> Mostly large swaths of erlydb and erlydb_mnesia (in ways that make it > work better with mnesia, but that make it incompatible with other > DBMSs, which of course the current incarnation avoids). I may end up > having to revert this if mnesia can't handle millions of objects, > with which it seems to be having trouble once they start to become > larger objects than simple tuples. I've hacked in a few return and > 'EXIT' handlers from controller functions for special circumstances, > like when I want to return a 400- or 500-level HTTP response along > with other information. I might try to hack in some WebDAV handling > stuff, as I'm considering writing a desktop client for my app in the > future and that seems the easiest way to get the data to and from the > desktop client. I haven't decided what that's going to look like yet. > It looks like I may be hacking in a return value to return extended > information to parent controller functions, like <title> tags. but I > want to decide how it should work, first. It sounds like it's just > not a concern to the two biggest users of erlyweb
If you want to contribute this feature (or any other feature that you think will be useful to other ErlyWeb users) I'll be happy to accept your code.
> >> I really like this, because it allows me to separate the view-logic > >> for things like contents and comments out from each other, which > >> makes changing the behaviour of these quite simple, localised, and > >> re- > >> useable. (In fact, as I re-write portions of erlyweb for my purposes, > >> the component system is something that I haven't touched at all > >> because it works quite well.) However, it has an inherent limitation: > >> none of those functions can return data to be rendered outside of > >> their components. In my case, I'd like to be able to set <title> > >> tags, which means that the top-level controller function (by top- > >> level, I mean the one referenced in the URL; like in /post/show/12, > >> the top-level controller function would be show/2) needs to somehow > >> return information to be rendered outside its little sandbox.
> >> I see that Vimagi doesn't set <title> tags (neither <http:// > >> vimagi.com/p/0PXmMRe6hGV> nor <http://vimagi.com/users/feeling3_4> > >> set one, at least, and those are where I'd expect to see them), but > >> BeerRiot does (<http://beerriot.com/beer/view/1751>; BTW I do love > >> the idea of having a controller called "beer_controller").
> >> It isn't limited to <title>s (for instance, I may want to alert the > >> browser that an RSS equivalent of this page exists, or what the > >> caching TTL is for this page in the HTTP headers). Conceptually, how > >> would one go about returning this extra information, while still > >> using the component system (I now have a lot of code that uses it > >> that I'd like to not re-write :) )?
>> Would it make sense to hack in a return value from the controller >> function? Something like: >> {return_with_meta,[{title,"Foo"},{link_rel,"next",link_to_page_2()}], >> [{data,"foo"}, >> {ewc,...}]} >> (with a better name, since "return_with_meta" doesn't sound all that >> great to me). But then I'm not sure how one could extract that from >> the rendered view. Maybe pass only the second tuple-item to the view, >> and pass both the first item and the second to the controller. Many >> many questions. I'll have to think about it some more. > I think that's a very good idea. There may be issues if multiple > subcomponents return the same metadata (or at least the same keys), > but then I guess ErlyWeb will just pick the first one. There are also > questions regarding how the metadata will be passed around (should it > be passed to a components siblings and or only its parents?) -- I'll > have to think about it and make sure all the bases are covered.
I really like the component system, so I'd definitely like to get your feedback on my ideas since it's already going in a good direction.
I wouldn't expect the siblings to be guaranteed to receive it (since clearly you can't guarantee that they all do, since some may have already been evaluated, or may be being evaluated at the same time), but I wouldn't expect them to be guaranteed to *not* receive it.
Since the yaws_arg is being re-vamped anyway, maybe this should be carried around on the yaws_arg. A simple plist is probably fine for it. Then as the yaws_arg is returned changed it can be passed through changed. Then make all subcomponents pass the yaws_arg implicitly: so instead of {ewc,controller,func,[A,Foo]} calling func(A,Foo), make {ewc,controller,func,[Foo]]} call fun(A,Foo). That way you're not passing a stale yaws_arg accidentally. This is of course a backwards- breaking change, but not a difficult one to fix, since all of the erlyweb code that I've seen and written passes it to the sub- components anyway (even if it's not necessarily used).
Otherwise, you end up all of the components doing something like this:
(once again, with better names than return_with_meta and add_meta) The worry here is that the subcomponents have to be called with NewA instead of A, which seems ripe for forgetting to me (especially if the yaws_arg some some validation before it passes it on; you could pass invalid data to sub-components this way).
I haven't looked at the contents of the yaws_arg, but if it's tagged with a type, then you could just do this:
Since erlyweb already knows to check the returned list for {ewc} and whatnot, it could just check for a new yaws_arg and start using that one. The problem there is that it forces {ewc}s to be processed serially (since what do you do when you get two conflicting yaws_args?), which my erlyweb does not (and given your blog post on a potential {concurrent,[{ewc,...}]}, I suspect this won't be a guarantee forever). Another idea is to just add another return value from controller functions:
(this was has my implicit-passing of yaws_args, but it's not necessary; not doing so does preclude children retrieving properties from parents, but if they need that they should pass it as an argument anyway). That doesn't carry the assumption that they are processed serially. It prevents parents from getting to children's properties without using {phased,Ewcs,...}, but that's probably not a problem, and isn't very avoidable. (I think all of these options do, if they want to do post-processing of any kind).
Personally, I like the last option, but it looks like it would require quite a few changes to the way that the erlyweb:ewc/N functions work
>>> Btw, just curious, what parts of ErlyWeb are you rewriting? >> [...] >> Mostly large swaths of erlydb and erlydb_mnesia (in ways that make it >> work better with mnesia, but that make it incompatible with other >> DBMSs, which of course the current incarnation avoids). I may end up >> having to revert this if mnesia can't handle millions of objects, >> with which it seems to be having trouble once they start to become >> larger objects than simple tuples. I've hacked in a few return and >> 'EXIT' handlers from controller functions for special circumstances, >> like when I want to return a 400- or 500-level HTTP response along >> with other information. I might try to hack in some WebDAV handling >> stuff, as I'm considering writing a desktop client for my app in the >> future and that seems the easiest way to get the data to and from the >> desktop client. I haven't decided what that's going to look like yet. >> It looks like I may be hacking in a return value to return extended >> information to parent controller functions, like <title> tags. but I >> want to decide how it should work, first. It sounds like it's just >> not a concern to the two biggest users of erlyweb
> If you want to contribute this feature (or any other feature that you > think will be useful to other ErlyWeb users) I'll be happy to accept > your code.
> Yariv
>>> Yariv
>>> On Nov 16, 2007 10:19 AM, David King <dk...@ketralnis.com> wrote:
>>>> erlyweb uses a component system for rendering pages. It makes >>>> most of >>>> my controller functions return lists like this:
>>>> I really like this, because it allows me to separate the view-logic >>>> for things like contents and comments out from each other, which >>>> makes changing the behaviour of these quite simple, localised, and >>>> re- >>>> useable. (In fact, as I re-write portions of erlyweb for my >>>> purposes, >>>> the component system is something that I haven't touched at all >>>> because it works quite well.) However, it has an inherent >>>> limitation: >>>> none of those functions can return data to be rendered outside of >>>> their components. In my case, I'd like to be able to set <title> >>>> tags, which means that the top-level controller function (by top- >>>> level, I mean the one referenced in the URL; like in /post/show/12, >>>> the top-level controller function would be show/2) needs to somehow >>>> return information to be rendered outside its little sandbox.
>>>> I see that Vimagi doesn't set <title> tags (neither <http:// >>>> vimagi.com/p/0PXmMRe6hGV> nor <http://vimagi.com/users/feeling3_4> >>>> set one, at least, and those are where I'd expect to see them), but >>>> BeerRiot does (<http://beerriot.com/beer/view/1751>; BTW I do love >>>> the idea of having a controller called "beer_controller").
>>>> It isn't limited to <title>s (for instance, I may want to alert the >>>> browser that an RSS equivalent of this page exists, or what the >>>> caching TTL is for this page in the HTTP headers). Conceptually, >>>> how >>>> would one go about returning this extra information, while still >>>> using the component system (I now have a lot of code that uses it >>>> that I'd like to not re-write :) )?
On Nov 16, 6:24 pm, David King <dk...@ketralnis.com> wrote:
> > - A while ago somebody asked a similar question and Bryan Fink
> > suggested returning a tuple of the form {title, Title} from the view
> > function, and then later picking up this tuple in the
> > html_container_controller and using its value to set the <title> tag.
> That does only take into account <title>s, and not any other
> metadata. And doesn't using erltl preclude returning something useful
> from a view function other than the data itself? And then the
> component receiving the rendered page would have to walk the
> resultant IOlist looking for it, rather than being able to pattern-
> match it. That just seems very brittle.
Yeah, it is a bit brittle. But, some tips:
1. If you need to return several things for the container to find,
just use more tuples. I have {title, X}, {head, X}, {onload, X}, etc.
2. ErlTl works just fine with this. The code looks like:
3. My container controller picks each of these piece out using two
rules:
a. all such tuples must be at the start of the rendered data
b. all such tuples must be in a defined order
So, the container code looks roughly like:
index(Data) ->
case Data of
[{title, Title} | TData] -> ok;
TData -> Title = "BeerRiot"
end,
case TData of
[{head, Head} | HData] -> ok;
HData -> Head = <<>>
end
...
{data, {Title, Head, OtherData}}.
I have to play a few more tricks, though, to account for newlines in
the rendered Data list. That could be fixed by moving the ErlTl
statements all to one line, but it didn't seem important enough to
worry about.
So, that restricts using these tuples to the "top level" component
view, but it has worked pretty well for me so far.
The idea of adding a {meta, X} tuple to the list of things a
controller can return sounds interesting, but since this stuff is more
on the view side, it also seems a little out of place. Not to say
that my hack isn't further out of place, of course. ;)
-Bryan
P.S. If you think the idea of a beer_controller is great, you'll love
that I also have a beer_container_controller, but only because I
couldn't decide among bottle_controller, can_controller, and
keg_controller. :)
Thanks, David. Interesting thoughts. After thinking about the problem more, I'm not sure that I would like to propagate changes to the arg up the chain. It feels like we'd be overloading the arg for purposes it wasn't designed to have. Plus, if we let subcomponents change the arg, it would make debugging more difficult. (Imagine that component A works if you test it independently, but then it breaks if you render component B before A.) This feels imperative to me -- like having global mutable state. I prefer subcomponents side-effect free. I also like thinking of the components relationships as a tree where the parents control the children and not vice versa.
I think that the best solution is to add a new return value, e.g. {meta, Val}, to controller functions. ErlyWeb will accumulate those values in a list and pass it to the 'phased' Fun as an extra parameter. So, you could write
This would simplify setting the Title, Header, etc from the controllers without using Bryan's clever ErlTL tricks (which may be less-than-intuitive for some people :) ), add too much magic to the framework, or even changing it much. It would also work trivially even when subcomponents are rendered in parallel because this is effectively a side-effect-free accumulator.
> I really like the component system, so I'd definitely like to get > your feedback on my ideas since it's already going in a good direction.
> I wouldn't expect the siblings to be guaranteed to receive it (since > clearly you can't guarantee that they all do, since some may have > already been evaluated, or may be being evaluated at the same time), > but I wouldn't expect them to be guaranteed to *not* receive it.
> Since the yaws_arg is being re-vamped anyway, maybe this should be > carried around on the yaws_arg. A simple plist is probably fine for > it. Then as the yaws_arg is returned changed it can be passed through > changed. Then make all subcomponents pass the yaws_arg implicitly: so > instead of {ewc,controller,func,[A,Foo]} calling func(A,Foo), make > {ewc,controller,func,[Foo]]} call fun(A,Foo). That way you're not > passing a stale yaws_arg accidentally. This is of course a backwards- > breaking change, but not a difficult one to fix, since all of the > erlyweb code that I've seen and written passes it to the sub- > components anyway (even if it's not necessarily used).
> Otherwise, you end up all of the components doing something like this:
> (once again, with better names than return_with_meta and add_meta) > The worry here is that the subcomponents have to be called with NewA > instead of A, which seems ripe for forgetting to me (especially if > the yaws_arg some some validation before it passes it on; you could > pass invalid data to sub-components this way).
> I haven't looked at the contents of the yaws_arg, but if it's tagged > with a type, then you could just do this:
> Since erlyweb already knows to check the returned list for {ewc} and > whatnot, it could just check for a new yaws_arg and start using that > one. The problem there is that it forces {ewc}s to be processed > serially (since what do you do when you get two conflicting > yaws_args?), which my erlyweb does not (and given your blog post on a > potential {concurrent,[{ewc,...}]}, I suspect this won't be a > guarantee forever). Another idea is to just add another return value > from controller functions:
> (this was has my implicit-passing of yaws_args, but it's not > necessary; not doing so does preclude children retrieving properties > from parents, but if they need that they should pass it as an > argument anyway). That doesn't carry the assumption that they are > processed serially. It prevents parents from getting to children's > properties without using {phased,Ewcs,...}, but that's probably not a > problem, and isn't very avoidable. (I think all of these options do, > if they want to do post-processing of any kind).
> Personally, I like the last option, but it looks like it would > require quite a few changes to the way that the erlyweb:ewc/N > functions work
> Any other ideas? How does that last one look?
> >>> Btw, just curious, what parts of ErlyWeb are you rewriting? > >> [...] > >> Mostly large swaths of erlydb and erlydb_mnesia (in ways that make it > >> work better with mnesia, but that make it incompatible with other > >> DBMSs, which of course the current incarnation avoids). I may end up > >> having to revert this if mnesia can't handle millions of objects, > >> with which it seems to be having trouble once they start to become > >> larger objects than simple tuples. I've hacked in a few return and > >> 'EXIT' handlers from controller functions for special circumstances, > >> like when I want to return a 400- or 500-level HTTP response along > >> with other information. I might try to hack in some WebDAV handling > >> stuff, as I'm considering writing a desktop client for my app in the > >> future and that seems the easiest way to get the data to and from the > >> desktop client. I haven't decided what that's going to look like yet. > >> It looks like I may be hacking in a return value to return extended > >> information to parent controller functions, like <title> tags. but I > >> want to decide how it should work, first. It sounds like it's just > >> not a concern to the two biggest users of erlyweb
> > If you want to contribute this feature (or any other feature that you > > think will be useful to other ErlyWeb users) I'll be happy to accept > > your code.
> > Yariv
> >>> Yariv
> >>> On Nov 16, 2007 10:19 AM, David King <dk...@ketralnis.com> wrote:
> >>>> erlyweb uses a component system for rendering pages. It makes > >>>> most of > >>>> my controller functions return lists like this:
> >>>> I really like this, because it allows me to separate the view-logic > >>>> for things like contents and comments out from each other, which > >>>> makes changing the behaviour of these quite simple, localised, and > >>>> re- > >>>> useable. (In fact, as I re-write portions of erlyweb for my > >>>> purposes, > >>>> the component system is something that I haven't touched at all > >>>> because it works quite well.) However, it has an inherent > >>>> limitation: > >>>> none of those functions can return data to be rendered outside of > >>>> their components. In my case, I'd like to be able to set <title> > >>>> tags, which means that the top-level controller function (by top- > >>>> level, I mean the one referenced in the URL; like in /post/show/12, > >>>> the top-level controller function would be show/2) needs to somehow > >>>> return information to be rendered outside its little sandbox.
> >>>> I see that Vimagi doesn't set <title> tags (neither <http:// > >>>> vimagi.com/p/0PXmMRe6hGV> nor <http://vimagi.com/users/feeling3_4> > >>>> set one, at least, and those are where I'd expect to see them), but > >>>> BeerRiot does (<http://beerriot.com/beer/view/1751>; BTW I do love > >>>> the idea of having a controller called "beer_controller").
> >>>> It isn't limited to <title>s (for instance, I may want to alert the > >>>> browser that an RSS equivalent of this page exists, or what the > >>>> caching TTL is for this page in the HTTP headers). Conceptually, > >>>> how > >>>> would one go about returning this extra information, while still > >>>> using the component system (I now have a lot of code that uses it > >>>> that I'd like to not re-write :) )?
> I think that the best solution is to add a new return value, e.g. > {meta, Val}, to controller functions. ErlyWeb will accumulate those > values in a list and pass it to the 'phased' Fun as an extra > parameter. So, you could write > {phased, {ewc, A}, > fun(ExpandedEwc, MetaVals, Rendered) -> > {ewc, html_container, [A, MetaVals, Rendered]} > end}. > This would simplify setting the Title, Header, etc from the > controllers without using Bryan's clever ErlTL tricks (which may be > less-than-intuitive for some people :) ), add too much magic to the > framework, or even changing it much. It would also work trivially even > when subcomponents are rendered in parallel because this is > effectively a side-effect-free accumulator.
That sounds good, and it avoids breaking backwards compatibility as well. I do agree about changes to yaws_arg being a bad idea (although it is currently possible for programs to change it before passing it to children, that would be to be deliberate).
The only problem I see is that if my controller function returns:
Then what does my view function look like? These could both make sense:
<%@ fun([_,Data]) %> <!-- pass it through -->
<%@ fun([Data]) %> <!-- pretend it didn't exist -->
I wouldn't mind passing it through, but that does seem a bit strange for children to be getting data intended for parents. Pretending it didn't exist seems even stranger.
>> I really like the component system, so I'd definitely like to get >> your feedback on my ideas since it's already going in a good >> direction.
>> I wouldn't expect the siblings to be guaranteed to receive it (since >> clearly you can't guarantee that they all do, since some may have >> already been evaluated, or may be being evaluated at the same time), >> but I wouldn't expect them to be guaranteed to *not* receive it.
>> Since the yaws_arg is being re-vamped anyway, maybe this should be >> carried around on the yaws_arg. A simple plist is probably fine for >> it. Then as the yaws_arg is returned changed it can be passed through >> changed. Then make all subcomponents pass the yaws_arg implicitly: so >> instead of {ewc,controller,func,[A,Foo]} calling func(A,Foo), make >> {ewc,controller,func,[Foo]]} call fun(A,Foo). That way you're not >> passing a stale yaws_arg accidentally. This is of course a backwards- >> breaking change, but not a difficult one to fix, since all of the >> erlyweb code that I've seen and written passes it to the sub- >> components anyway (even if it's not necessarily used).
>> Otherwise, you end up all of the components doing something like >> this:
>> (once again, with better names than return_with_meta and add_meta) >> The worry here is that the subcomponents have to be called with NewA >> instead of A, which seems ripe for forgetting to me (especially if >> the yaws_arg some some validation before it passes it on; you could >> pass invalid data to sub-components this way).
>> I haven't looked at the contents of the yaws_arg, but if it's tagged >> with a type, then you could just do this:
>> Since erlyweb already knows to check the returned list for {ewc} and >> whatnot, it could just check for a new yaws_arg and start using that >> one. The problem there is that it forces {ewc}s to be processed >> serially (since what do you do when you get two conflicting >> yaws_args?), which my erlyweb does not (and given your blog post on a >> potential {concurrent,[{ewc,...}]}, I suspect this won't be a >> guarantee forever). Another idea is to just add another return value >> from controller functions:
>> (this was has my implicit-passing of yaws_args, but it's not >> necessary; not doing so does preclude children retrieving properties >> from parents, but if they need that they should pass it as an >> argument anyway). That doesn't carry the assumption that they are >> processed serially. It prevents parents from getting to children's >> properties without using {phased,Ewcs,...}, but that's probably not a >> problem, and isn't very avoidable. (I think all of these options do, >> if they want to do post-processing of any kind).
>> Personally, I like the last option, but it looks like it would >> require quite a few changes to the way that the erlyweb:ewc/N >> functions work
>> Any other ideas? How does that last one look?
>>>>> Btw, just curious, what parts of ErlyWeb are you rewriting? >>>> [...] >>>> Mostly large swaths of erlydb and erlydb_mnesia (in ways that >>>> make it >>>> work better with mnesia, but that make it incompatible with other >>>> DBMSs, which of course the current incarnation avoids). I may >>>> end up >>>> having to revert this if mnesia can't handle millions of objects, >>>> with which it seems to be having trouble once they start to become >>>> larger objects than simple tuples. I've hacked in a few return and >>>> 'EXIT' handlers from controller functions for special >>>> circumstances, >>>> like when I want to return a 400- or 500-level HTTP response along >>>> with other information. I might try to hack in some WebDAV handling >>>> stuff, as I'm considering writing a desktop client for my app in >>>> the >>>> future and that seems the easiest way to get the data to and >>>> from the >>>> desktop client. I haven't decided what that's going to look like >>>> yet. >>>> It looks like I may be hacking in a return value to return extended >>>> information to parent controller functions, like <title> tags. >>>> but I >>>> want to decide how it should work, first. It sounds like it's just >>>> not a concern to the two biggest users of erlyweb
>>> If you want to contribute this feature (or any other feature that >>> you >>> think will be useful to other ErlyWeb users) I'll be happy to accept >>> your code.
>>> Yariv
>>>>> Yariv
>>>>> On Nov 16, 2007 10:19 AM, David King <dk...@ketralnis.com> wrote:
>>>>>> erlyweb uses a component system for rendering pages. It makes >>>>>> most of >>>>>> my controller functions return lists like this:
>>>>>> I really like this, because it allows me to separate the view- >>>>>> logic >>>>>> for things like contents and comments out from each other, which >>>>>> makes changing the behaviour of these quite simple, localised, >>>>>> and >>>>>> re- >>>>>> useable. (In fact, as I re-write portions of erlyweb for my >>>>>> purposes, >>>>>> the component system is something that I haven't touched at all >>>>>> because it works quite well.) However, it has an inherent >>>>>> limitation: >>>>>> none of those functions can return data to be rendered outside of >>>>>> their components. In my case, I'd like to be able to set <title> >>>>>> tags, which means that the top-level controller function (by top- >>>>>> level, I mean the one referenced in the URL; like in /post/ >>>>>> show/12, >>>>>> the top-level controller function would be show/2) needs to >>>>>> somehow >>>>>> return information to be rendered outside its little sandbox.
>>>>>> I see that Vimagi doesn't set <title> tags (neither <http:// >>>>>> vimagi.com/p/0PXmMRe6hGV> nor <http://vimagi.com/users/ >>>>>> feeling3_4> >>>>>> set one, at least, and those are where I'd expect to see >>>>>> them), but >>>>>> BeerRiot does (<http://beerriot.com/beer/view/1751>; BTW I do >>>>>> love >>>>>> the idea of having a controller called "beer_controller").
>>>>>> It isn't limited to <title>s (for instance, I may want to >>>>>> alert the >>>>>> browser that an RSS equivalent of this page exists, or what the >>>>>> caching TTL is for this page in the HTTP headers). Conceptually, >>>>>> how >>>>>> would one go about returning this extra information, while still >>>>>> using the component system (I now have a lot of code that uses it >>>>>> that I'd like to not re-write :) )?
> > I think that the best solution is to add a new return value, e.g. > > {meta, Val}, to controller functions. ErlyWeb will accumulate those > > values in a list and pass it to the 'phased' Fun as an extra > > parameter. So, you could write > > {phased, {ewc, A}, > > fun(ExpandedEwc, MetaVals, Rendered) -> > > {ewc, html_container, [A, MetaVals, Rendered]} > > end}. > > This would simplify setting the Title, Header, etc from the > > controllers without using Bryan's clever ErlTL tricks (which may be > > less-than-intuitive for some people :) ), add too much magic to the > > framework, or even changing it much. It would also work trivially even > > when subcomponents are rendered in parallel because this is > > effectively a side-effect-free accumulator.
> That sounds good, and it avoids breaking backwards compatibility as > well. I do agree about changes to yaws_arg being a bad idea (although > it is currently possible for programs to change it before passing it > to children, that would be to be deliberate).
> The only problem I see is that if my controller function returns:
> Then what does my view function look like? These could both make sense:
> <%@ fun([_,Data]) %> <!-- pass it through -->
> <%@ fun([Data]) %> <!-- pretend it didn't exist -->
> I wouldn't mind passing it through, but that does seem a bit strange > for children to be getting data intended for parents. Pretending it > didn't exist seems even stranger.
I don't think that the view function should receive the meta Vals. The purpose of the {meta, Val} feature is to pass values "up" to the 'phased' Fun. If you need to pass the same value to the view function (and I can't really think of a scenario where you would), you can add it again as a {data, Data} tuple. Maybe to make things clearer, instead of adding {meta, Val} values to the list you return from a controller function, you would return a tuple such as
The elements of the second tuple element would be accumulated and eventually passed to the 'phased' fun, and the third element would be treated as the response value is treated today.
> >> I really like the component system, so I'd definitely like to get > >> your feedback on my ideas since it's already going in a good > >> direction.
> >> I wouldn't expect the siblings to be guaranteed to receive it (since > >> clearly you can't guarantee that they all do, since some may have > >> already been evaluated, or may be being evaluated at the same time), > >> but I wouldn't expect them to be guaranteed to *not* receive it.
> >> Since the yaws_arg is being re-vamped anyway, maybe this should be > >> carried around on the yaws_arg. A simple plist is probably fine for > >> it. Then as the yaws_arg is returned changed it can be passed through > >> changed. Then make all subcomponents pass the yaws_arg implicitly: so > >> instead of {ewc,controller,func,[A,Foo]} calling func(A,Foo), make > >> {ewc,controller,func,[Foo]]} call fun(A,Foo). That way you're not > >> passing a stale yaws_arg accidentally. This is of course a backwards- > >> breaking change, but not a difficult one to fix, since all of the > >> erlyweb code that I've seen and written passes it to the sub- > >> components anyway (even if it's not necessarily used).
> >> Otherwise, you end up all of the components doing something like > >> this:
> >> (once again, with better names than return_with_meta and add_meta) > >> The worry here is that the subcomponents have to be called with NewA > >> instead of A, which seems ripe for forgetting to me (especially if > >> the yaws_arg some some validation before it passes it on; you could > >> pass invalid data to sub-components this way).
> >> I haven't looked at the contents of the yaws_arg, but if it's tagged > >> with a type, then you could just do this:
> >> Since erlyweb already knows to check the returned list for {ewc} and > >> whatnot, it could just check for a new yaws_arg and start using that > >> one. The problem there is that it forces {ewc}s to be processed > >> serially (since what do you do when you get two conflicting > >> yaws_args?), which my erlyweb does not (and given your blog post on a > >> potential {concurrent,[{ewc,...}]}, I suspect this won't be a > >> guarantee forever). Another idea is to just add another return value > >> from controller functions:
> >> (this was has my implicit-passing of yaws_args, but it's not > >> necessary; not doing so does preclude children retrieving properties > >> from parents, but if they need that they should pass it as an > >> argument anyway). That doesn't carry the assumption that they are > >> processed serially. It prevents parents from getting to children's > >> properties without using {phased,Ewcs,...}, but that's probably not a > >> problem, and isn't very avoidable. (I think all of these options do, > >> if they want to do post-processing of any kind).
> >> Personally, I like the last option, but it looks like it would > >> require quite a few changes to the way that the erlyweb:ewc/N > >> functions work
> >> Any other ideas? How does that last one look?
> >>>>> Btw, just curious, what parts of ErlyWeb are you rewriting? > >>>> [...] > >>>> Mostly large swaths of erlydb and erlydb_mnesia (in ways that > >>>> make it > >>>> work better with mnesia, but that make it incompatible with other > >>>> DBMSs, which of course the current incarnation avoids). I may > >>>> end up > >>>> having to revert this if mnesia can't handle millions of objects, > >>>> with which it seems to be having trouble once they start to become > >>>> larger objects than simple tuples. I've hacked in a few return and > >>>> 'EXIT' handlers from controller functions for special > >>>> circumstances, > >>>> like when I want to return a 400- or 500-level HTTP response along > >>>> with other information. I might try to hack in some WebDAV handling > >>>> stuff, as I'm considering writing a desktop client for my app in > >>>> the > >>>> future and that seems the easiest way to get the data to and > >>>> from the > >>>> desktop client. I haven't decided what that's going to look like > >>>> yet. > >>>> It looks like I may be hacking in a return value to return extended > >>>> information to parent controller functions, like <title> tags. > >>>> but I > >>>> want to decide how it should work, first. It sounds like it's just > >>>> not a concern to the two biggest users of erlyweb
> >>> If you want to contribute this feature (or any other feature that > >>> you > >>> think will be useful to other ErlyWeb users) I'll be happy to accept > >>> your code.
> >>> Yariv
> >>>>> Yariv
> >>>>> On Nov 16, 2007 10:19 AM, David King <dk...@ketralnis.com> wrote:
> >>>>>> erlyweb uses a component system for rendering pages. It makes > >>>>>> most of > >>>>>> my controller functions return lists like this:
> >>>>>> I really like this, because it allows me to separate the view- > >>>>>> logic > >>>>>> for things like contents and comments out from each other, which > >>>>>> makes changing the behaviour of these quite simple, localised, > >>>>>> and > >>>>>> re- > >>>>>> useable. (In fact, as I re-write portions of erlyweb for my > >>>>>> purposes, > >>>>>> the component system is something that I haven't touched at all > >>>>>> because it works quite well.) However, it has an inherent > >>>>>> limitation: > >>>>>> none of those functions can return data to be rendered outside of > >>>>>> their components. In my case, I'd like to be able to set <title> > >>>>>> tags, which means that the top-level controller function (by top- > >>>>>> level, I mean the one referenced in the URL; like in /post/ > >>>>>> show/12, > >>>>>> the top-level controller function would be show/2) needs to > >>>>>> somehow > >>>>>> return information to be rendered outside its little sandbox.
> >>>>>> I see that Vimagi doesn't set <title> tags (neither <http:// > >>>>>> vimagi.com/p/0PXmMRe6hGV> nor <http://vimagi.com/users/ > >>>>>> feeling3_4> > >>>>>> set one, at least, and those are where I'd expect to see > >>>>>> them), but > >>>>>> BeerRiot does (<http://beerriot.com/beer/view/1751>; BTW I do > >>>>>> love > >>>>>> the idea of having a controller called "beer_controller").
> >>>>>> It isn't limited to <title>s (for instance, I may want to > >>>>>> alert the > >>>>>> browser that an RSS equivalent of this page exists, or what the > >>>>>> caching TTL is for this page in the HTTP headers). Conceptually, > >>>>>> how > >>>>>> would one go about returning this extra information, while still > >>>>>> using the component system (I now have a lot of code that uses it > >>>>>> that I'd like to not re-write :) )?
>> Then what does my view function look like? [...] > I don't think that the view function should receive the meta Vals. The > purpose of the {meta, Val} feature is to pass values "up" to the > 'phased' Fun. If you need to pass the same value to the view function > (and I can't really think of a scenario where you would), you can add > it again as a {data, Data} tuple. Maybe to make things clearer, > instead of adding {meta, Val} values to the list you return from a > controller function, you would return a tuple such as > show(A) -> > ... > {meta, [Val1, Val2], [{data, Data}, {ewc, ..}... ]}.
I like it. But I'm starting to think that {meta} is a bad word for it. It's not very descriptive of what's actually happening. Of course, I don't have any better suggestions :) Maybe {with_properties} or {set_properties} or something?
> I like it. But I'm starting to think that {meta} is a bad word for > it. It's not very descriptive of what's actually happening. Of > course, I don't have any better suggestions :) Maybe > {with_properties} or {set_properties} or something?
Yeah, I agree. I think 'with_properties' and 'set_properties' sounds too generic, though. Maybe 'with_meta_vals' or 'with_phased_vals'?
>> This would simplify setting the Title, Header, etc from the >> controllers without using Bryan's clever ErlTL tricks (which may be >> less-than-intuitive for some people :) ), add too much magic to the >> framework, or even changing it much. It would also work trivially >> even >> when subcomponents are rendered in parallel because this is >> effectively a side-effect-free accumulator.
> That sounds good, and it avoids breaking backwards compatibility as > well. I do agree about changes to yaws_arg being a bad idea (although > it is currently possible for programs to change it before passing it > to children, that would be to be deliberate).
> The only problem I see is that if my controller function returns:
> Then what does my view function look like? These could both make > sense:
> <%@ fun([_,Data]) %> <!-- pass it through -->
> <%@ fun([Data]) %> <!-- pretend it didn't exist -->
> I wouldn't mind passing it through, but that does seem a bit strange > for children to be getting data intended for parents. Pretending it > didn't exist seems even stranger.
>>> I really like the component system, so I'd definitely like to get >>> your feedback on my ideas since it's already going in a good >>> direction.
>>> I wouldn't expect the siblings to be guaranteed to receive it (since >>> clearly you can't guarantee that they all do, since some may have >>> already been evaluated, or may be being evaluated at the same time), >>> but I wouldn't expect them to be guaranteed to *not* receive it.
>>> Since the yaws_arg is being re-vamped anyway, maybe this should be >>> carried around on the yaws_arg. A simple plist is probably fine for >>> it. Then as the yaws_arg is returned changed it can be passed >>> through >>> changed. Then make all subcomponents pass the yaws_arg >>> implicitly: so >>> instead of {ewc,controller,func,[A,Foo]} calling func(A,Foo), make >>> {ewc,controller,func,[Foo]]} call fun(A,Foo). That way you're not >>> passing a stale yaws_arg accidentally. This is of course a >>> backwards- >>> breaking change, but not a difficult one to fix, since all of the >>> erlyweb code that I've seen and written passes it to the sub- >>> components anyway (even if it's not necessarily used).
>>> Otherwise, you end up all of the components doing something like >>> this:
>>> (once again, with better names than return_with_meta and add_meta) >>> The worry here is that the subcomponents have to be called with NewA >>> instead of A, which seems ripe for forgetting to me (especially if >>> the yaws_arg some some validation before it passes it on; you could >>> pass invalid data to sub-components this way).
>>> I haven't looked at the contents of the yaws_arg, but if it's tagged >>> with a type, then you could just do this:
>>> Since erlyweb already knows to check the returned list for {ewc} and >>> whatnot, it could just check for a new yaws_arg and start using that >>> one. The problem there is that it forces {ewc}s to be processed >>> serially (since what do you do when you get two conflicting >>> yaws_args?), which my erlyweb does not (and given your blog post >>> on a >>> potential {concurrent,[{ewc,...}]}, I suspect this won't be a >>> guarantee forever). Another idea is to just add another return value >>> from controller functions:
>>> (this was has my implicit-passing of yaws_args, but it's not >>> necessary; not doing so does preclude children retrieving properties >>> from parents, but if they need that they should pass it as an >>> argument anyway). That doesn't carry the assumption that they are >>> processed serially. It prevents parents from getting to children's >>> properties without using {phased,Ewcs,...}, but that's probably >>> not a >>> problem, and isn't very avoidable. (I think all of these options do, >>> if they want to do post-processing of any kind).
>>> Personally, I like the last option, but it looks like it would >>> require quite a few changes to the way that the erlyweb:ewc/N >>> functions work
>>> Any other ideas? How does that last one look?
>>>>>> Btw, just curious, what parts of ErlyWeb are you rewriting? >>>>> [...] >>>>> Mostly large swaths of erlydb and erlydb_mnesia (in ways that >>>>> make it >>>>> work better with mnesia, but that make it incompatible with other >>>>> DBMSs, which of course the current incarnation avoids). I may >>>>> end up >>>>> having to revert this if mnesia can't handle millions of objects, >>>>> with which it seems to be having trouble once they start to become >>>>> larger objects than simple tuples. I've hacked in a few return and >>>>> 'EXIT' handlers from controller functions for special >>>>> circumstances, >>>>> like when I want to return a 400- or 500-level HTTP response along >>>>> with other information. I might try to hack in some WebDAV >>>>> handling >>>>> stuff, as I'm considering writing a desktop client for my app in >>>>> the >>>>> future and that seems the easiest way to get the data to and >>>>> from the >>>>> desktop client. I haven't decided what that's going to look like >>>>> yet. >>>>> It looks like I may be hacking in a return value to return >>>>> extended >>>>> information to parent controller functions, like <title> tags. >>>>> but I >>>>> want to decide how it should work, first. It sounds like it's just >>>>> not a concern to the two biggest users of erlyweb
>>>> If you want to contribute this feature (or any other feature that >>>> you >>>> think will be useful to other ErlyWeb users) I'll be happy to >>>> accept >>>> your code.
>>>> Yariv
>>>>>> Yariv
>>>>>> On Nov 16, 2007 10:19 AM, David King <dk...@ketralnis.com> wrote:
>>>>>>> erlyweb uses a component system for rendering pages. It makes >>>>>>> most of >>>>>>> my controller functions return lists like this:
>>>>>>> I really like this, because it allows me to separate the view- >>>>>>> logic >>>>>>> for things like contents and comments out from each other, which >>>>>>> makes changing the behaviour of these quite simple, localised, >>>>>>> and >>>>>>> re- >>>>>>> useable. (In fact, as I re-write portions of erlyweb for my >>>>>>> purposes, >>>>>>> the component system is something that I haven't touched at all >>>>>>> because it works quite well.) However, it has an inherent >>>>>>> limitation: >>>>>>> none of those functions can return data to be rendered >>>>>>> outside of >>>>>>> their components. In my case, I'd like to be able to set <title> >>>>>>> tags, which means that the top-level controller function (by >>>>>>> top- >>>>>>> level, I mean the one referenced in the URL; like in /post/ >>>>>>> show/12, >>>>>>> the top-level controller function would be show/2) needs to >>>>>>> somehow >>>>>>> return information to be rendered outside its little sandbox.
>>>>>>> I see that Vimagi doesn't set <title> tags (neither <http:// >>>>>>> vimagi.com/p/0PXmMRe6hGV> nor <http://vimagi.com/users/ >>>>>>> feeling3_4> >>>>>>> set one, at least, and those are where I'd expect to see >>>>>>> them), but >>>>>>> BeerRiot does (<http://beerriot.com/beer/view/1751>; BTW I do >>>>>>> love >>>>>>> the idea of having a controller called "beer_controller").
>>>>>>> It isn't limited to <title>s (for instance, I may want to >>>>>>> alert the >>>>>>> browser that an RSS equivalent of this page exists, or what the >>>>>>> caching TTL is for this page in the HTTP headers). Conceptually, >>>>>>> how >>>>>>> would one go about returning this extra information, while still >>>>>>> using the component system (I now have a lot of code that >>>>>>> uses it >>>>>>> that I'd like to not re-write :) )?
> Yoi probably want to change the order of those arguments. If you have
> fun(ExpandedEwc, Rendered)
> then you should also have:
> fun(ExpandedEwc, Rendered, MetaVals)
> That is, keep the first few arguments consistent for the various > forms, and when adding arguments, add them to the end, not the middle.
Makes sense. Let's add the parameter at the end. I'm not too worried about backwards compatibility, btw. Porting existing apps would require a 1 line change.
I took a few days off for Thanksgiving and now I'm back to working on ErlyWeb.
As I went back to working one the phased_vars feature, it occurred to me that it would probably be cleaner to include them as an element of the existing 'response' list rather than in a special return value. So, instead of returning
It looks good though; if I'm returning phased values like that, I'm probably setting headers and whatnot, since I'm probably the initial {ewc}. The only problem is that only initial {ewc}s can do it, not nested ones (unless there are many levels of {phased,ewc...}). That's fine for my purposes, but I don't know about most people.
> It looks good though; if I'm returning phased values like that, I'm > probably setting headers and whatnot, since I'm probably the initial > {ewc}. The only problem is that only initial {ewc}s can do it, not > nested ones (unless there are many levels of {phased,ewc...}). That's > fine for my purposes, but I don't know about most people.
This limitation seems resonable to me too. The main need for the phased_vars feature is being able to set title, headers, etc, and those are for the most part determined by the top level ewc, not its subcomponents. If at some point we realize this is insufficient we can do the work to remove this limitation, but for now I think it strikes the best balance between features and ease of implementation.