Forum: Ruby on Rails Creating a new template handler via plugin

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
D0cd6b10e01bacb976b3b815a9c660bc?d=identicon&s=25 Alex Wayne (squeegy)
on 2006-12-21 23:32
I am trying to add a template type to render FlexImage::Model objects.
I seem to be missing some understanding on how rails handles some of the
details around this.

The template is pure ruby code, it just has some special handling around
the template to further process the returned value of the template.

The biggest issue is that I can't figure out how to get the view aware
of the helpers and instance variables from the controller.  Currently, I
am simply doing something like this:

  result = action_view.controller.instance_eval(@flexi_template)

This allows me access to the instance variable from the controllers, but
not the helpers.

So what is proper way to implement a template handler so that it gets
all the treatments that a view should.  Meaning it gets the helpers and
instance variables that one would expect from the rhtml views.

I have studied some other plugins like markaby and the rails code itself
and can't seem to get my head around this.  Also the rails docs
regarding this are quite brief.

The full code for my Template handler is here:
http://pastie.caboo.se/29106

Any insight would be appreciated.
D0cd6b10e01bacb976b3b815a9c660bc?d=identicon&s=25 Alex Wayne (squeegy)
on 2006-12-21 23:36
Alex Wayne wrote:
> Any insight would be appreciated.

While I am at it, if anyone has any idea why

  ActionController::Base.exempt_from_layout :foo

doesn't work, it would help to get rid of the render statement from the
controller:

  def image
    @image = MyImage.find(1)
    render :layout => false # required cause exempt_from_layout is
broken
  end
D0cd6b10e01bacb976b3b815a9c660bc?d=identicon&s=25 Alex Wayne (squeegy)
on 2006-12-22 00:42
Alex Wayne wrote:
> Alex Wayne wrote:
>> Any insight would be appreciated.
>
> While I am at it, if anyone has any idea why
>
>   ActionController::Base.exempt_from_layout :foo
>
> doesn't work, it would help to get rid of the render statement from the
> controller:
>
>   def image
>     @image = MyImage.find(1)
>     render :layout => false # required cause exempt_from_layout is
> broken
>   end

Don't you love when you struggle with something for a long time, then
you get frustrated and voice a question about it to others.  Then, as if
by magic, the universe answers your public outcry for assistance and
grants the knowledge you were looking for.

I stumbled upon the MarkDownOnRails plugin
http://rubygreenblue.com/blog/article/markdown_tem...,
which has exactly the simple and easy to understand template handling
that I needed to figure this out.  Turns out that rails passes you a
view object which *mostly* has what you need.

Here is the template handler, rewritten, and seems to work properly, for
all that are curious:

  http://pastie.caboo.se/29137
This topic is locked and can not be replied to.