Forum: Ruby on Rails finding out template's filename in application layout file code?

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.
(Guest)
on 2009-03-10 18:35
(Received via mailing list)
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
(Guest)
on 2009-03-10 19:04
(Received via mailing list)
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"
(Guest)
on 2009-03-11 12:23
(Received via mailing list)
(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 -%>
This topic is locked and can not be replied to.