So I played around with this to see what could be done within the existing
framework. Here's what I've come up with...
The haml code looks like:
- define_template :titlebar do |content|
%h1= content
- define_template :box do |title,content|
.box
.title= title
.content= content
%html
%body
= eval_template :titlebar,"My title"
= eval_template :box,"My Eval Template Title" do
My internal special content
= Time.now
- sleep 2
= eval_template :box,"Another box" do
This is another box
= Time.now
The supporting code is surprisingly simple....
module Haml
module Helpers
class TemplateStore
def self.save(name,&block)
@blocks ||= {}
@blocks[name] = *block
end
def self.get(name)
@blocks[name]
end
end
def define_template(name,&block)
TemplateStore.save(name,&block)
end
def eval_template(name,*args,&block)
if block_given?
content = capture_haml(&block)
args << content
end
capture_haml(*args,&(TemplateStore.get(name)))
end
end
end
It has room for improvement, but it's a start.
John
On 10/12/07, John Aughey <[EMAIL PROTECTED]> wrote:
>
> Ok, so the subject doesn't make total sense, but I have an idea of how to
> use haml to defer some of the layout decisions to an external template.
> Maybe this doesn't make sense either. Here's an example.
>
>
> I have a site that puts content inside pretty boxes styled with CSS.
> In the haml code, it looks something like...
>
> .box
> .title Title of the box
> .content
> Here's where all the content goes
>
>
> Which generates html
>
> <div class='box'>
> <div class='title'>"This is a test"</div>
> <div class='content'>
> All my content goes here
>
> </div>
> </div>
>
> This is all fine, but the haml code has too much layout logic for my taste.
> If I changed how a box were to be structured (maybe I want to use a *gasp*
> table), everywhere I put something inside this logical box would need to
>
> be modified.
>
> I'm proposing a new special character such as ! or whatever that passes the
> evaluated haml block to another template. The new haml code for the above
> example would look like...
>
> !box Title of the box
>
> Here's where all the content goes
>
> This really says what I mean. I want a titled box with the following
> content. The previous example put too much of the required layout of having
> several nested div sections in the code.
>
>
> The layout logic would go into another file or block that would be rendered
> inline with the same engine. In this example, there would be a file
> _box.haml that looks like:
>
> .box
> .title @args[0]
>
> .content
> @content
>
> So with this template defined of how a box is to be structured, I can then
> use this !box tag throughout my thousands of .haml files without worry. If
> I need to modified how the box is structured to use some new CSS style or
>
> presentation, I only need to modify the above template.
>
> Rather than using an external file to render the template, it might be
> defined inline with the main haml code, so a single file haml source for
> this might look like:
>
>
> ^define_template :box
> .box
> .title @args[0]
> .content
> @content
>
> !box "Title for the box"
> Content for the box
>
>
> I started to look at the haml source to make this modification, but the
>
> learning code was a little steep. The advantage this has over Rails
> partials is the inclusion of the content block that can be inserted wherever
> the template feels it should be put and wrapped with whatever layout is
>
> necessary.
>
> John
>
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Haml" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/haml?hl=en
-~----------~----~----~----~------~----~------~--~---