Login
Username:

Password:


Lost Password?
Register now!
Page « 1 ... 5 6 7 (8) 9 »
Articles : Web Development Using the Ruby on Rails
on 2008/7/27 23:54:31 (1901 reads)
Articles


The '<%= yield %>' line is an example or ERb. The <% %> denotes that we want to execute a piece of Ruby code and <%= %> means we want to render the output of the embedded Ruby code on the html page. The 'yield' method returns the content of the currently rendered page. This very simple example layout dismisses the need to write the boilerplate html every page. A page may look like this:


< h1 >no need to write surrounding html tags< /h1>


The final rendered page would have an html source like this:

< head >
< /head>

< body>

< h1 >no need to write surrounding html tags< /h1 >
< /body>


How does Rails know which template to apply, if any? Again, it looks in certain directories and expects certain filenames. Continuing on the previous file layout, it could look like this:

/app

/controllers
employees_controller.rb

/models
employee.rb

/views

/employees
show.rhtml

/layouts
application.rhtml
employees.rhtml


The default directory for storing layouts is in views/layouts. When rendering a view, for example the employees/show.rhtml view, Rails will look in the layouts folder and search for a file with the same name as the controller that renders the view, 'employees.rhtml' in this case. If the employees.rhtml file contains a line <%= yield %>, it will be replaced with the contents of the employees/show.rhtml file.

You might also notice another (optional) file in the layouts directory, the 'application.rhtml' file. This is a special template, as it will be used as the default template for each controller unless explicitly overridden.

Page « 1 ... 5 6 7 (8) 9 »
Printer Friendly Page Send this Story to a Friend Create a PDF from the article
Share The Knowledge

Jobs