# Extracting the name of the template I am yielding

**URL:** <https://www.ruby-forum.com/t/extracting-the-name-of-the-template-i-am-yielding/161434>\
**Category:** Rails\
**Created:** [February 26, 2009, 1:54pm UTC](https://www.ruby-forum.com/t/extracting-the-name-of-the-template-i-am-yielding/161434 "2009-02-26T13:54:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/838e76/32.png) [@rob](https://www.ruby-forum.com/u/rob)\
**Post date:** [February 26, 2009, 1:54pm UTC](https://www.ruby-forum.com/t/extracting-the-name-of-the-template-i-am-yielding/161434/1 "2009-02-26T13:54:24Z")

</div>

Hey guys,

I’m trying to extract the name of the template I am currently to be  
yielding from my layout, but with no success. Does anyone know how to do  
that?

Suppose I am calling the MainController#create action. On failure to  
create my erm…Main object it re-renders the ‘new’ template. I’d like  
to know in the layout which template I am going to render so that I can  
access a help system which can dynamically include help information  
depending on which template I am rendering at the time. Then no matter  
what controller or what action I am calling at the time, as long as I  
have the template name I can display the same information.

Any ideas?

RobL

> **[Posts](https://www.robl.me/)**
>
> I write thems codes

---

<div class="post-metadata">

**Author:** ![rob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/838e76/32.png) [@rob](https://www.ruby-forum.com/u/rob)\
**Post date:** [February 26, 2009, 3:17pm UTC](https://www.ruby-forum.com/t/extracting-the-name-of-the-template-i-am-yielding/161434/2 "2009-02-26T15:17:07Z")

</div>

Hey Rob -  
I think I understand the question

Is this what you’re looking for:

template will return the full name of the current template

if you stick it in your layout it’ll just render the name of the  
layout.

If you stick it in each template (a little more work) then it’ll  
return the name of the template.

Id stick this in an application\_helper like so:

def this\_temp  
template.to\_s.split(".")[0]  
end

This hacks of the filetype extention (html.erb) leaving just the name

Calling this in the view will give you  
\<%= this\_temp %\> =\> mains/new

Hope that helps?

---

<div class="post-metadata">

**Author:** ![rob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/838e76/32.png) [@rob](https://www.ruby-forum.com/u/rob)\
**Post date:** [February 26, 2009, 5:19pm UTC](https://www.ruby-forum.com/t/extracting-the-name-of-the-template-i-am-yielding/161434/3 "2009-02-26T17:19:43Z")

</div>

Thanks for the help Gavin, it wasn’t quite what I was looking for. Its a  
bit of a weird one. Given a particular layout I want to know the name of  
the template that is used in the

\<%= yield %\>

Which is a bit weird, I know. I would imagine that the results of the  
yield are just a big string, by which time its a bit late to work out  
what the name of the template I just rendered was.

Hmmzzz. Thanks all the same 🙂

RobL

> **[Posts](https://www.robl.me/)**
>
> I write thems codes

---

<div class="post-metadata">

**Author:** ![rob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/838e76/32.png) [@rob](https://www.ruby-forum.com/u/rob)\
**Post date:** [February 26, 2009, 5:25pm UTC](https://www.ruby-forum.com/t/extracting-the-name-of-the-template-i-am-yielding/161434/4 "2009-02-26T17:25:34Z")

</div>

Could you explain the problem in a little more depth?

Maybe there’s another way to achieve what you’re trying to do, aside  
from finding the template name?

Gav
