Saturday, January 31, 2009

Using page.xhtml

So far, I think you'd have to argue that we haven't really done anything that wouldn't be just as easy in raw HTML. In this post we'll take a look at something that really will simplify things and will lead to a Don't Repeat Yourself (DRY) moment. Let's start with out multiple pages with links from here. Next we're going to add a single line (OK, technically a line and a comment):

    # Use page.xhtml in the view directory for layout
layout :page



This tells Ramaze to use page.xhtml in the view directory (the default) for layout. Here's what the code looks like with this line in:

require 'rubygems'
require 'ramaze'

# This example is based on the previous "multiple pages" example.
# It has the same three methods (index, page1, and page2) but adds
# links on each page to the other two pages. For linking it uses
# the Ramaze Rs helper for linking within the same controller. The
# other item that is different is that instead of the content
# being in double quotes, we use the %{} form which is the same
# as the double quotes or as %Q{}. They just make it easier to
# include quotes in the string.
#
# Additionally, this adds the use of a layout using
# page.xhtml from the view directory.
class MainController < Ramaze::Controller

# Use page.xhtml in the view directory for layout
layout :page

# You can access it now with http://localhost:7000/
# This should output
# Hello, World!
# in your browser and provide links to the other two pages
def index
%{
Hello, World! <br/>
<a href="#{Rs(:page1)}">Page 1</a>
<a href="#{Rs(:page2)}">Page 2</a>
}
end

# You can access it now with http://localhost:7000/page1
# This should output
# "Page 1!"
# in your browser and provide links to the other two pages
def page1
%{
Page 1 <br/>
<a href="#{Rs(:index)}">Hello world</a>
<a href="#{Rs(:page2)}">Page 2</a>
}
end

# You can access it now with http://localhost:7000/page2
# This should output
# "Page 2!"
# in your browser and provide links to the other two pages
def page2
%{
Page 2 <br/>
<a href="#{Rs(:index)}">Hello world</a>
<a href="#{Rs(:page1)}">Page 1</a>
}
end
end

Ramaze.start



Now we need to add a page.xhtml. First create a "view" directory underneath where you're currently working. For example create a directory, "UsePageXHTML" and copy the above code into in in usepagexhtml.rb. The create a directory underneath that called "view". In the "view" directory copy the following text into "page.xhmtl"
<html>
<head> <title>Use page.xhtml</title> </head>
<body>
#@content
<h5> Powered by Ramaze </h5>
</body>
</html>



Most of this looks like pretty standard HTML except for the following line:
        #@content



This line, when processed takes the return value from the controller and substitutes it into this spot. In our case this is the text from the three MainController methods including the links. Note that each of the pages, index, page1, and page2 has the title "Use page.xhtml" and at the bottom of the page a "Powered by Ramaze" line. You can run this by going to your "UsePageXHTML" directory and typing:

ruby usepagexhtml.rb

As always, use the comments for any questions or comments. For those of you who have asked about syntax highlighting for the code, I'm looking at it and trying to figure out what the best/easiest way to do it would be. I could have worked on that this morning, but decided I'd rather post.

2 comments:

  1. hi. what's interesting is that, as far as i know, ezamar default view extention is .xhtml ... if i decide to use .html it will not work, so if i really need html files i have to use .xhtml extentions, but change the doctype to html, which is odd i think ...

    ReplyDelete
  2. I think that html files are seen as "static" files if you put them in the public directory. But you're right, it is odd to make the doctype html in an xhtml file. I guess it's because we're going to convert it to html.

    ReplyDelete