# Creating a new template handler via plugin

**URL:** <https://www.ruby-forum.com/t/creating-a-new-template-handler-via-plugin/80180>\
**Category:** Rails\
**Created:** [December 21, 2006, 11:32pm UTC](https://www.ruby-forum.com/t/creating-a-new-template-handler-via-plugin/80180 "2006-12-21T23:32:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![squeegy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dec6dc/32.png) [@squeegy](https://www.ruby-forum.com/u/squeegy)\
**Post date:** [December 21, 2006, 11:32pm UTC](https://www.ruby-forum.com/t/creating-a-new-template-handler-via-plugin/80180/1 "2006-12-21T23:32:00Z")

</div>

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](http://pastie.caboo.se/29106)

Any insight would be appreciated.

---

<div class="post-metadata">

**Author:** ![squeegy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dec6dc/32.png) [@squeegy](https://www.ruby-forum.com/u/squeegy)\
**Post date:** [December 21, 2006, 11:36pm UTC](https://www.ruby-forum.com/t/creating-a-new-template-handler-via-plugin/80180/2 "2006-12-21T23:36:43Z")

</div>

Alex W. 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

---

<div class="post-metadata">

**Author:** ![squeegy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dec6dc/32.png) [@squeegy](https://www.ruby-forum.com/u/squeegy)\
**Post date:** [December 22, 2006, 12:42am UTC](https://www.ruby-forum.com/t/creating-a-new-template-handler-via-plugin/80180/3 "2006-12-22T00:42:44Z")

</div>

Alex W. wrote:

> Alex W. 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\_template\_handler\_for\_rails](http://rubygreenblue.com/blog/article/markdown_template_handler_for_rails),  
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:

[Parked at Loopia](http://pastie.caboo.se/29137)
