To try to get a better handle on Seaside, the Smalltalk based web application framework – I decided to start reimplementing some of the simple example and demonstration web applications I’ve seen around for other frameworks. The idea being that not only will it give me more exposure to Seaside, but it will also show me how to translate the ideas.

The obvious first example is Hello World – I won’t bother showing how it’s done in another framework for that one, because it’s too simple, but I will show you what’s required in Seaside.

Smalltalk [Show Plain Code]:
  1. WAComponent subclass: #HelloWorld
  2.   instanceVariableNames:
  3.   classVariableNames:
  4.   poolDictionaries:
  5.   category: ‘Stuarts-Examples’
  6.  
  7. HelloWorld>>renderContentOn: html
  8.     html heading: ‘Hello World!’

As you can see, there’s not much required there – the first part is a standard Smalltalk class declaration – I’m creating a new class called HelloWorld which is a subclass of WAComponent.
renderContentOn: is the method that does the work.

Now for something slightly more functional, I’ll take the little example Howard Lewis Ship used in his recent Tapestry 5 screencast.
This basically lists numbers 1 to 10 with links on them, and shows you which one you clicked.

First, the Tapestry source:

Java(TM) 2 Platform Standard Edition 5.0 [Show Plain Code]:
  1. package org.example.helloworld.pages;
  2.  
  3. import org.tapestry.annotations.ComponentClass;
  4. import org.tapestry.annotations.OnEvent;
  5. import org.tapestry.annotations.Persist;
  6.  
  7. @ComponentClass
  8. public class Start {
  9.   private int _index;
  10.  
  11.   @Persist
  12.   private int _selected;
  13.  
  14.   public int getIndex() {
  15.     return _index;
  16.   }
  17.  
  18.   public void setIndex(int index) {
  19.     _index = index;
  20.   }
  21.  
  22.   @OnEvent(component="choose")
  23.   public void choose(int index) {
  24.     _selected = index;
  25.   }
  26.  
  27.   public int getSelected() {
  28.         return _selected;
  29.   }
  30. }
  1. <html xmlns:t="http://tapestry.apache.org/schema/tapestry_5_0_0.xsd">
  2. <title>Hello World</title>
  3. </head>
  4. <h1>Hello World</h1>
  5.  
  6. Please choose a number between 1 and 10:
  7. </p>
  8.  
  9. <t:comp type="Loop" source="prop:1..10" value="prop:index">
  10.   <t:comp type="ActionLink" id="choose" context="prop:index">${index} –
  11. </p>
  12.  
  13. <t:comp type="If" test="prop:selected">
  14. You chose: <strong>${selected}</strong>
  15. </p>
  16. </t:comp>
  17.  
  18. </body>
  19. </html>

Not much there, just the fairly basic HTML template to create the page, and the component behind it providing the data.

Now for the Seaside version:

Smalltalk [Show Plain Code]:
  1. WAComponent subclass: #NumberChooser
  2.   instanceVariableNames: ‘selected’
  3.   classVariableNames:
  4.   poolDictionaries:
  5.   category: ‘Stuarts-Examples’
  6.  
  7. NumberChooser>>rendererClass
  8.   ^WARenderCanvas
  9.  
  10. NumberChooser>>choose: aNumber
  11.   selected := aNumber
  12.  
  13. NumberChooser>>renderContentOn: html
  14.   html
  15.     heading: ‘Hello World!’;
  16.     paragraph: ‘Please choose a number between 1 and 10:’;
  17.     paragraph: [
  18.       1 to: 10 do: [:val|
  19.         html anchor callback: [ self choose: val ]; text: val.
  20.         html text: ‘ — ‘
  21.       ]
  22.     ].
  23.  
  24.   selected ifNotNil: [
  25.     html paragraph: [
  26.       html text: ‘You choose: ‘; strong: selected
  27.     ]
  28.   ]

That’s a little smaller, mostly due to Smalltalk / Java differences, but it’s interesting to see how similar Tapestry and Seaside are in concept at least for this simple example (admittedly that’s Tapestry 5 which won’t be out for a long time yet, but Tapestry 4 is the same concept with a bit more typing)

Anyway, off to find some more examples to try!

 

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Please leave these two fields as-is:

Protected by Invisible Defender. Showed 403 to 1,123 bad guys.