Reuven Mlerner

Why not use your server-side programming language to generate the JavaScript for you?

RJS Templates

The power of Ajax to fetch and run JavaScript generated by your server-side language.

The past few months, I've written a number of articles in this space about JavaScript. This language, built in to nearly every modern Web browser, has now come into its own and is at the heart of a paradigm for modern Web development known as Ajax. Whereas knowledge of JavaScript was long an optional skill for Web developers, it has become a must-have skill, along with SQL, HTML, HTTP and CSS.

One of the reasons for JavaScript's renaissance is the emergence of cross-platform libraries, which hide the incompatibilities that long plagued the language. For quite some time, programs written in JavaScript had to contain many if/then statements that looked at possible cross-platform incompatibilities.

Today, we can avoid having such if/then statements in our code by using libraries that take care of these low-level tasks for us. Prototype and Dojo, two of the JavaScript libraries I profiled in previous columns, have become popular precisely because they hide many of these details. They make JavaScript a truly cross-platform language, where "platform" means the Web browser as much as the operating system.

Some clever programmers, in an effort to make JavaScript standardization even more complete and effortless, have gone one step further. Why not use your server-side programming language to generate the JavaScript for you? That is, if you are using Ruby on Rails, perhaps you could write commands in Ruby and have them translated into JavaScript. Doing so would allow you to use roughly the same code in all of your templates, without having to switch syntax in different parts of the template.

This might sound like a strange idea, but the more I think about it, the more I like it. RJS (short for Ruby JavaScript) templates are one incarnation of this. If you prefer to create your JavaScript in Java, you might want to look at the Google Web Toolkit, which is now available under an open-source license and has gained many fans in the Java world.

This month, we look at RJS and how it makes life much easier for Web developers. Although I don't think that JavaScript will ever disappear, or that Web developers will be able to ignore it completely, technologies such as RJS mean that it might become something like machine code today—available and sitting at the bottom of the pyramid, but generally ignored by high-level programmers.

Listing 1. index.rhtml

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html>

<title id="title">Sample HTML page</title> <%= javascript_include_tag 'prototype' %>

<script type="text/javascript">

function updateHeadline() {

var new_headline_text = SF('future-headline');

Element.update(headline, new_headline_text) ;

<h1 id="headline">Headline</h1> <p><input type="text" id="future-headline" /></p> <p><input type="button" onclick="updateHeadline();" value="Update headline" /></p>

Ajax and Rails

To create a new Rails project called ajaxdemo, type: rails ajaxdemo

Now, let's create a simple controller called showme: script/generate controller showme

We're not going to have any model in this system, but you still might have to define one or more lines in config/database.yml. Instead, let's create a new view within our showme controller, stored as app/views/showme/index.rhtml, shown in Listing 1.

As you can see, the index.rhtml page is a relatively standard page of HTML, with some JavaScript code that uses the Prototype library. The page consists of a headline, a text field and a button. Pressing the button invokes the

4TB Raw SATA Storage

8TB Raw SATA Storage

12TB Raw SATA Storage

Your integrated neighborhood iust got friendlier

With Pogo's Network Attached Storage appliance, maximum interoperability with the most popular operating systems is easier. Take control of your ever-expanding storage needs with an easy to use graphical management interface, multiple snapshots, built-in back up, IP failover, and enhanced security. Powered by Intel Dual-Core Xeon processors and a fast 64-bit OS. Call today for more information.

www.pogolinux.com

[linux]

Experience, Imagination, and Support. Pogo Linux is Hardware Solutions Built for Linux, Built for You.

To get started, contact us at 888.828.POGO or [email protected] Pogo Linux, Inc. 701 Fifth Ave. Suite 6850, Seattle, WA98104

Xeon'

Quad-core. Unmatched.

Intel, Intel logo, Intel Inside logo, Pentium, Xeon, and Xeon Inside are trademarks or registered trademarks of Intel corporation or its subsidiaries in the United States and other countries. For additional terms and conditions please visit www.pogolinux.com

Was this article helpful?

0 0
No Authority SEO Tool Management Made Easy

No Authority SEO Tool Management Made Easy

Only By Using The Precise SEO Tools.. Can You Get Tons Of Free Targeted Traffic To Your Websites amp Then Only IF You Know The Right Tools amp Exactly How To Use Them! Nobody Builds A House Using A Butter Knife And A Pair Of Pliers! And You Can't Get To The Top Of The Search Engines Without The Right Tools, Either.

Get My Free Ebook


Post a comment