Saturday, January 31, 2009

Syntax Color Highlight Test

This is a test of syntax highlighting

require 'rubygems'
require 'ramaze'

# you can access it now with http://localhost:7000/
# This should output
# Hello, World!
# in your browser

class MainController < Ramaze::Controller
def index
"Hello, World!"
end
end

Ramaze.start
OK, as long as I've figured this out, I might as well document how I did it in case someone else needs it. On ^ manveru's advice, I took a look at MaRuKu. MaRuKu is a Markdown language. To use it, just
gem install maruku
To syntax highlight ruby source copy the ruby source to a new file, move the code in four (4) spaces or one tab, and add the following line at the end
http://maruku.rubyforge.org/maruku.html
in the example above I copied our hello.rb to hello.rb.md, indented the entire file, and finally added the line. Next run ramaku on the new file
ramaku hello.rb.md
and this should produce a new file hello.rb.html
From this just grab the items in the within the <pre> tags and then paste it into your blog (or wherever). The only other thing to do is put the CSS into your blog's HTML. I added the following
.ruby .normal {}
.ruby .comment { color: #005; font-style: italic; }
.ruby .keyword { color: #A00; font-weight: bold; }
.ruby .method { color: #077; }
.ruby .class { color: #074; }
.ruby .module { color: #050; }
.ruby .punct { color: #447; font-weight: bold; }
.ruby .symbol { color: #099; }
.ruby .string { color: #944; background: #FFE; }
.ruby .char { color: #F07; }
.ruby .ident { color: #004; }
.ruby .constant { color: #07F; }
.ruby .regex { color: #B66; background: #FEF; }
.ruby .number { color: #F99; }
.ruby .attribute { color: #7BB; }
.ruby .global { color: #7FB; }
.ruby .expr { color: #227; }
.ruby .escape { color: #277; }


as part of a style tag right before the closing of the head tag.

Let me know if this makes sense to everyone and if not, I'll try to explain it a bit better. To be honest, I wasn't all that interested in doing a lot of work for this so there's a bit of trial and error rather than true understanding of what's really going on here.

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.

Sunday, January 25, 2009

Multiple Controllers

Often, a web site will have two (or more) types of users. For instance this blog has someone writing the blog and a number of people (hopefully) reading it. An online store will have someone administering or managing the site and a number of people making purchases from the site. In these cases, there will typically be multiple controllers, one for each function. The code shown here (essentially the same as our last bit of code for linking) doesn't really need multiple controllers, but is for display purposes only.

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.
class MainController < Ramaze::Controller

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

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

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

class SecondController < Ramaze::Controller

# The second controller will be accessed using "second"
# as in
# http://localhost:7000/second/index.
map '/second'

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

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

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

Ramaze.start




The first thing to note is the second class, once again derived from Ramaze::Controller called cleverly enough SecondController. The second controller uses the "map" function to tell how to access it. In this case, you would go to http://localhost:7000/second to get to the second controllers index method. This displays a page with the line "Hello World from Second Controller!" as well as links to the SecondController's pages and links back to all of the FirstController's pages. Links back to the FirstController's pages are made using the R helper for links to other controllers rather than the Rs helper for links within the same controller. It has the form:
<a href="#{R(MainController, :index)}">Index MainController</a>




In this case all we're doing is adding the controller as well as the method to the link.

Not too much else going on here, use the comments to post any questions you might have on the code.

Monday, January 19, 2009

Ramaze Multiple Pages With Linking

Having users have to switch between pages using the URL isn't particularly user friendly, so we'll add links to the pages to switch between them. Here's the code:

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.
class MainController < Ramaze::Controller

# 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



The initial comments pretty much spell out what's going on here. We've added a links on each page to the other two pages. Within each method, we've used the %{} form of the double quotes to simplify the strings (i.e. we don't have to escape all of the quotes). For the links themselves, we've used the Rs helper. This allows us to create a link within the same controller. Later, we'll use the R helper between controllers. You can read more about helpers on the Ramaze wiki.

As always, copy this into a text editor, save it (as say "link.rb"), and run it using either
ruby link.rb
or
ramaze link.rb
.

Let me know if you have questions or comments.

Sunday, January 18, 2009

Ramaze Multiple Pages

Well, it wouldn't be too awfully interesting if all we could do was a single page no matter how easy it was to put it up. Here's a Ramaze program where we have multiple pages that is very similar to our "hello world" program.

require 'rubygems'
require 'ramaze'

# This example is based on the previous "hello world" example. All we've done
# is add two more methods to our MainController class that we will show "Page 1!"
# and "Page 2!" respectively.
class MainController < Ramaze::Controller

# You can access it now with http://localhost:7000/
# This should output
# Hello, World!
# in your browser
def index
"Hello, World!"
end

# You can access it now with http://localhost:7000/page1
# This should output
# "Page 1!"
# in your browser
def page1
"Page 1!"
end

# You can access it now with http://localhost:7000/page2
# This should output
# "Page 2!"
# in your browser
def page2
"Page 2!"
end
end

Ramaze.start




Once again, copy this into a text editor, save it as multiplepages.rb, and finally run it as either ruby multiplepages.rb or as ramaze mutiplepages.rb. After that you should be able to navigate to http://localhost::7000 and see "Hello World!". Additionally, you should be able to go to http://localhost::7000/page1 and see "Page 1!" or http://localhost::7000/page2 and see "Page 2!". You should also be able to go to http://localhost::7000/index and see "Hello World!" even though you don't actually need the "index".

What we're seeing here is that each method in a controller represents a view. You may have heard of MVC or Model, View, Controller architechture and Ramaze (as well as Rails) is built like this. We aren't seeing true "Views" yet but in a future post we will, ours are more implied. You can read more about the MVC architecture at on Wikipedia.

Go ahead and experiment with this code. You can add some more methods to the controller class or even play around with the text in each class (hint, try putting some HTML tags around the text, say h1 and see what happens.

Leave questions or comments in the comments section.

Saturday, January 17, 2009

Ramaze Hello World

It wouldn't be right to start out learning about any computer language without beginning with the ubiquitous "hello world" program. I first saw it with Kernighan and Richie's "The C Programming Language", but my guess is it predates them by quite a bit. The idea is that if you can get a program to run that does nothing but print out "hello world" to the screen, you have actually accomplished quite a bit. In the case of programming languages, you have used an editor to compose the program, the language's compiler to compile it to machine code, the operating system's linker to make the program runnable, and finally, you were able to actually run the program and see something.

Here's the "hello world" program for ramaze:

require 'rubygems'
require 'ramaze'

# you can access it now with http://localhost:7000/
# This should output
# Hello, World!
# in your browser

class MainController < Ramaze::Controller
def index
"Hello, World!"
end
end

Ramaze.start



This is straight from the ramaze.net site but also appears in your examples/basic directory. On my Ubuntu machine it is:

/usr/lib/ruby/gems/1.8/gems/ramaze-2009.01/examples/basic/hello.rb

There are other examples in there that may also be of interest.

So, what's going on here? The first couple of lines are bringing in required "libraries" including the ramaze "library" (I'm using the word library here loosely as they aren't compiled libraries as you would normally think of them). The next few lines beginning with a "#" are comments. If you didn't recognize them, you might want to have a basic Ruby book on hand to follow along with. A lot of this won't make sense if you don't know ruby. The next line starts the MainController (we actually could have called it anything) class and tells us it is derived from the Ramaze::Controller class. All of our controllers will be derived from this class. The next three lines define a method index for our class that returns the text string "Hello World!". Finally, we close out the class and start Ramaze.


If you copy this code into a text editor, save it as hello.rb, and then do either:
ruby hello.rb
or
ramaze hello.rb
you should start up the webrick web server. Point your browser to:
http://localhost::7000
and you should see Hello World! there.

We're using a few defaults here to get this to display. Notably the fact that if we don't tell Ramaze anything else, it will use the index method as the default to display. One other item is the ::7000 which is the port to use. 7000 is the default for Ramaze and in a future post, we'll discuss how to change this.. Rails uses 3000 as the default for comparison.


Post any questions or comments on this in the comments section.

Friday, January 16, 2009

Ramaze List

Here's a list of items that I'm considering doing posts on for Ramaze:
  • Installing Ramaze (last post)
  • Hello World (quintessential)
  • Multiple pages with one controller
  • Links with a single controller
  • Multiple controllers
  • Links with multiple controllers
  • External links
  • Using page.xhtml
  • Using different .xhtml pages for different views
  • CSS and the public directory
  • Forms and getting data from them using the request variable
  • Models
  • Connecting models to databases
I learn best with small samples rather than larger applications and I'm guessing that there are a few other people out there for which this is also true. The ramaze gem when installed has some nice basic examples (on my Ubuntu box they're in:
/usr/lib/ruby/gems/1.8/gems/ramaze-2009.01/examples/basic)
with which the list above will have some overlap. On the ones that do, I hope to at least provide some additional commentary.

Let me know in the comments if this list seems reasonable or if there are things that should be added or removed.

Ramaze Introduction

I must admit that I've been a fan of Ruby for quite some time but could never get the hang of Rails. I didn't get a chance to use it much and it was so big, that I could never really get my arms around it. While looking around a bit, I found another Ruby based web framework called Ramaze (http://ramaze.net/). It's simple to install, on Windows just do a:

gem install ramaze

and on Ubuntu (similar for other Linux I would assume) a:

sudo gem install ramaze

do a:

ramaze --version

and you should see something similar to:

Ramaze Version 2009.01, on ruby 1.8.6 (2007-09-24) [i486-linux]

I'll be posting a few simple scripts to show some of the basics of this nice, small, understandable web framework.