Here is a chunk of RDoc from the Rails API:
layout.rhtml
<html xmlns="http://www.w3.org/1999/xhtml"
xml:lang="en" lang="en">
<head>
<title>layout with js</title>
<script type="text/javascript">
<%= yield :script %>
</script>
</head>
<body>
<%= yield %>
</body>
</html>
view.rhtml
This page shows an alert box!
<% content_for("script") do %>
alert('hello world')
<% end %>
Normal view text
This will defiantly help you out if you're using layouts (which you probably are) in rails because it lets the view page change a portion of the layout if it needs to. I found this really useful when I needed to try and load up a swf file in one of my views and wanting to do some browser/flash detection when the view was loaded. I didn't want the detection to happen on all the pages that used the view, but I needed it to happen when the page was being loaded. Rails helper to the rescue!
layout.rhtml
<html xmlns="http://www.w3.org/1999/xhtml"
xml:lang="en" lang="en">
<head>
<title>layout</title>
</head>
<body <%= yield :script %>>
<%= yield %>
</body>
</html>
view.rhtml
This page detects flash!
<% content_for("script") do %>
"onLoad='detectFlash()'"
<% end %>
Normal view text
See that? You can even yield inside an open html tag (this is pseudo code, but should work). On all the other views that use layout.rhtml nothing happens, you just get a normal . When view.rhtml is shown "onLoad='detectFlash()'" is inserted into the body, and run, detecting the flash version before the page loads. This was something that I thought was going to be a serious problem to do, but rails makes it super easy.
There are all kinds of other goodies in the rails classes/ActionView/Helpers package, I would recommend checking them out because there is probably something there to help you out. Oh ya, if you actually need to detect the flash version of someones browser Dreamweaver spits out a nice script that will do it for you. If you need that let me know and I can post it.
No comments:
Post a Comment