Hello,
I have a suggestion for a new view renderrer.
Why? Because there are people for whom DRB is not the easiest <%=
way_of_doing %>
I don''t think DRB renderrer is bad; just for me with 5 dioptries is
less
readable :-)
Moreover:
You usually get a code with a css, not 100% what you expected. You''re
absolutely not able to
use rxml which, of course, would be nice. So you do your work somehow and
when you''re almost
done, your boss say: Client doesn''t want this, he wants that. This is
repeating
and after that the code becomes full of hacks (because the project MUST
be done as soon as possible, which today means till tomorrow (and
recursively :->) ).
My solution is "CodePieces".
The view consists of two files: One file is a driver script written in
ruby and the second contains pieces of (html) code
in yaml file. Pros?
a) Because the driver script contains view-logic and the YAML file is a
code "repository" only,
you don''t need to rewrite whole template anymore. Just replace
the
yaml file and let the
driver script untouched. Moreover the driver script is reusable.
(I make a difference between application logic and view logic,
controller does the application logic,
view logic are foreachs and ifs)
b) YAML file can do only two types of calls: recursive call to another
piece of code or
helper call and is not chatty as DRB.
d) You don''t need many partials files, the yaml file consists all
partials.
e) Every html coder can easily edit yaml file and understands it.
f) It''s simple and powerful. (You can see at http://eeasy.1984.cz
what
I think of XML GML TML BML stuff :-> )
The best way how to explain my ideas are examples. This is the way how it
should work,
today I do it using regular expressions which is limitting. I''m going
to
write a real parser
and store parsed templates in a cache (it should be as fast as DRB or
faster).
But I want to read your opinions first. What do you think about it?
jan molic
1. Example - minimum. Usually used at the beginning when you want to create
"working" prototype of a site.
----------- YAML -------------
body: |
<html>
<body>
This is body.
</body>
</html>
----------- RUBY -------------
show( ''body'' )
2. Example - using bracket tags. Bracket tags are usually used for
printing values which are passed through the driver script.
No, there''s no way how to print view object''s variables
directly ( i.e.
{ @var } ). You can use helper call instead (see below).
----------- YAML -------------
message:
<div><p class=''{ p-class }''> Name: { name },
Date: { date } </p>
<p>{ text }</p>
</div>
list-item: <li>{ text }</li>
body: |
<html>
<body>
<ul>{ list-items }</ul>
<div class=''messages''>
{ messages }
</div>
</body>
</html>
----------- RUBY -------------
hsh = {}
hsh[''messages''] = get_each( ''message'',
@messages )
hsh[''list-items''] = get_each(
''list-item'', @list )
show( ''body'', hsh )
3. Example - simple helper calls. You can use simple (simple!) helper call
inside brackets.
You aren''t expected to use many helpers in yaml files, but there are
some
cases when passing
all values through driver script wouldn''t be comfortable - for instance
passing all localized
strings or URLs. You can use helper call to get view object''s variable
directly
but it''s not recommended. The syntax is " { helper: argument list
} ".
----------- YAML -------------
message:
<div>{ loc: name }: { name } <br />
{ loc: date }: { date }
<a href=''{ url: index, list }''>{ loc:
listing }</a>
This variable is passed directly from the view object: { var:
@text }
</div>
4. Example - brackets inside brackets. In this example
we pass only ''actual_page'' variable from the driver script.
Body class
will be set depending on it automatically.
----------- YAML -------------
# define classes of body tags
body_class-homepage: normal
body_class-news: normal
body_class-services: threecols
body:
<html>
<body class=''{ body_class-{ actual_page } }''>
This is body.
</body>
</div>
----------- RUBY -------------
show( ''body'', { ''actual_page'' =>
@actual_page } )
5. Example - pieces of code recursive calls.
In daily use: This is really powerful.
----------- YAML -------------
item: <li>{ text }</li>
# define default list styles
list-style-1: <ul>{ items }</ul>
list-style-2: <ul><p class=''news''> { items }
</p></ul>
# define list styles for pages
list-style-homepage: { list-style-1 }
list-style-news: { list-style-2 }
list-style-services: { list-style-2 }
list-style-products: { list-style-2 }
body:
<html>
<body>
{ list-style-{ actual_page } }
</body>
</html>
----------- RUBY -------------
hsh = {}
hsh[''items''] = get_each( ''item'', @list_items
)
hsh[''actual_page''] = @actual_page
show( ''body'', hsh )
6. Example - using yaml not only as string repository.
Sometimes variables are not connected to controller
but only to a view and therefore should be placed in view.
For example menu items order.
----------- YAML -------------
# define the order of items in the menu
menu-items:
- it: homepage
- it: services
- it: news
menu-item: <li>{ it }</li>
menu:
<div class=''menu''>
<ul>{ menu-items }</ul>
</div>
body:
<html>
<body>{ menu }</body>
</html>
----------- RUBY -------------
items = get( ''menu-items'' )
menu_items = get_each( ''menu-item'', items )
render( ''body'', { ''menu'' => get(
''menu'', menu_items ) } )