# Finding out template's filename in application layout file code?

**URL:** <https://www.ruby-forum.com/t/finding-out-templates-filename-in-application-layout-file-code/162508>\
**Category:** Rails\
**Created:** [March 10, 2009, 5:35pm UTC](https://www.ruby-forum.com/t/finding-out-templates-filename-in-application-layout-file-code/162508 "2009-03-10T17:35:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 10, 2009, 5:35pm UTC](https://www.ruby-forum.com/t/finding-out-templates-filename-in-application-layout-file-code/162508/1 "2009-03-10T17:35:33Z")

</div>

Hi,

I would like to access the filename of the (main) template used for a  
request, and I would like to do so in the (main application) layout  
template. I know about @template, but @template.template.filename  
yields the filename of the application template file, not of the view  
template file. On the other hand, the filename is in @template  
somewhere - I can see it when I look at @template.inspect. I’m just  
too stupid to find out where exactly…

And this is what I would like to accomplish, and by the way, I  
consider this a very basic function of a web framework:

- I don’t use any Javascript generated by Rails. I don’t like  
Prototype, and I sure don’t like inline Javascript

- I believe in separation of HTML and JS.

- I load any JS files AT THE END of the HTML (bottom of application  
layout file)

- I load jquery (or YUI, that’s the two JS-libs I like - jquery for  
smaller jobs, YUI for real applications rather than just some add-on  
code for a page), and then I add some page specific code. I have NO  
code inline, and I sure won’t let Rails insert any inline code, so as  
I said, I don’t use rails JS helpers or .rjs files. Ever. JS code  
remains 100% separate.

- For page specific code I would like to use a file with the exact  
same name as the .html.erb view file, in the same directory. I don’t  
want to have the JS code inline in the view file. That’s bad not just  
because I want it all logically separated, but also because editors  
have a very hard time handling mixed HTML/CSS/JS code. Especially if  
it’s more than a few lines of code that means trouble. It is much  
better to have JS code in a separate file.

- I would like my application layout template to look for .js files  
for the current view template file, and if present, insert them into  
the page (at the very bottom into a script section). The issue I have  
and ask about right here would be the same if I inserted a link to  
that JS file instead, but in order to avoid an additional HTTP  
download I prefer to insert the code. It is guarded by an onDOMReady  
event fired by the jquery or YUI library, which is loaded before that  
page specific code.

What still prevents me from doing this is that I don’t know how to  
generically find out the name of the currently used view template  
inside the ruby code of the application template layout file, and I  
sure don’t want to have to set a variable in the controller for this  
purpose - I’m sure Rails has that info _somewhere_. Please help me  
locate it 🙂

Thanks!

Michael

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 10, 2009, 6:04pm UTC](https://www.ruby-forum.com/t/finding-out-templates-filename-in-application-layout-file-code/162508/2 "2009-03-10T18:04:56Z")

</div>

Well, it seems @_first\_render.filename does the trick. Now, I’m just a  
little unsure about the "_" in front - usually that denotes the  
variable as “internal - don’t use”. Is there a “more official” way to  
access this information?

On Mar 10, 5:34 pm, “removed\_email\_address@domain.invalid”

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 11, 2009, 11:23am UTC](https://www.ruby-forum.com/t/finding-out-templates-filename-in-application-layout-file-code/162508/3 "2009-03-11T11:23:20Z")

</div>

(FYI - I consider this a vary basic setup for anyone using _proper_  
Javascript, i.e. not inline code, something Rails generates  
unfortunately)

Okay… so in Rails 2.3.1 (RC2) I cannot do it in the layout file  
alone any more. I have to remember the template name in (each) view in  
an instance variable, and then I can use that in the layout file.  
Details:

somewhere in the view for which there exists an extra JS file:

\<%- @view\_name=@\_render\_stack[0].template\_path -%\>

Since this is again an “underscore variable” and undocumented onne  
cannot rely on it existing in the next Rails release, but I’m sure it  
will be fairly easy to find something similar then.

In the layout file (the JS files have to be named “\_viewname.js”):

\<%- unless @view\_name.blank? -%\>  
\<%= render @view\_name.gsub(/.html.erb\s\*$/,’’)+’.js’ %\>  
\<%- end -%\>
