From: Jano Svitok Date: 2007-03-24T01:28:02+09:00 Subject: Re: Newbie templating question: how to combine multiple file On 3/23/07, Frank Reiff wrote: > Jano Svitok wrote: > > On 3/23/07, Frank Reiff wrote: > >> > >> HTML editor, something along these lines: > >> > >> combining multiple HTML pages into a single one? > >> > >> Any help would be appreciated. > >> > >> Best regards, > >> > >> Frank > >> > > > > Hi Frank, > > > > have a look at rails source how it is done. It's quite easy. The > > following code I almost copied from there (I do template caching in a > > member attribute in addition). > > > > I include this code in the object that > > renders the templates, but with a little modifications, you can use it > > from > > top level scope as well (e.g. after you get rid of @'s) > > > > > > def render(template, local_assigns = {}) > > b = evaluate_locals(local_assigns) > > rhtml = @templates[template] > > if rhtml.nil? > > template_filename = File.join(@template_dir, > > template) > > rhtml = ERB.new(File.read(template_filename), > > nil, "%<>") > > @templates[template] = rhtml > > end > > rhtml.result(b) > > end > > > > def evaluate_locals(local_assigns = {}) > > b = binding > > local_assigns.each { |key, value| eval "#{key} = > > local_assigns[\"#{key}\"]", b } > > b > > end > > Thanks. One question though. The File.join presumably simply copies the > contents of the linked file, so won't this insert the enclosing > tag into the output? > > > > <%=render("header.html"%> >

Hello World

> > > > becomes > > > >

My Header

>

Hello World

> > File.join is used for joining pathnames. See the docs. The output depends on the content of the included template. If there's then it'll be in the output and vice versa. so if header.html contains:

My Header

then you'll have the output you wrote, but if there's only:

My Header

then you'll probably get what you want. Jano