Let’s take a moment to step back and review what we have learnt. Consider the following method:
html paragraph: 'A plain text paragraph.'.
html paragraph: [
html render: 'A paragraph with plain text followed by a line break. '.
html emphasis: 'Emphasized text '.
html render: 'followed by a horizontal rule.'.
html render: 'An image URI: '.
html image url: 'http://www.seaside.st/styles/logo-plain.png' ]
There are four patterns that appear in this method.
render: renderableObject. This message adds the renderable object to the content of the component. It doesn’t use any brushes, it just tells the object to render itself.
- Message with zero or one argument. These create brushes. Just as a painter is able to use different tools to paint on a canvases, brushes represent the various tags we can use in XHTML, so
horizontalRulewill produce the tag
hr. Some brushes take an argument such as
emphasis:other don’t. Section 7.5 will cover this in depth.
- Composed messages. The expression
html imagecreates an image brush, and then sends it a
url:message to configure its attributes.
- Block passed as arguments. Using a block (code delimited by
]) allows us to say that the actions in the block are happening in the context of a given tag. In our example, the second paragraph takes an argument. It means that all the elements created within the block will be contained within the paragraph.
render: message. As you saw, we use the message
render: to render objects. Modify the method
renderContentOn: of your ScrapBook component as follows.
html paragraph: [
html render: 'today: '.
html render: Date today ]
Refresh your web browser, see Figure 61. The method
renderContentOn: renders a string and the object representing the current date. It uses the method
render:. Most of the time you will use the method
render: to render objects or other components, see Chapter 12.
We use a block as the argument of the
paragraph: because we want to specify that the string
'today ' and the textual representation of the current date are both within a paragraph. Seaside provides a shortcut for doing this. If you are sending only the message
render: inside a block, just use the renderable object as a parameter instead of the block. The following two methods are equivalent and we suggest you to use the second form, see Figure 62.
Two equivalent methods.
html paragraph: [ html render: 'today: ' ]
html paragraph: 'today: '
html text: 'some string'.
html text: Date today.
text: produces the string representation of an object, as would be done in an inspector. You can pass any object and by default its textual representation will be rendered. In Pharo and GemStone the method
text: will call the method
Object>>asString, while VisualWorks uses the method
Object>>displayString. In any case, the string representation is generated by sending the message
Object>>printOn: to that object.
html text: anObject is an efficient short hand for the expression
html render: anObject asString in Pharo.
About the method
renderOn:. If you browse the implementation of
WAHtmlCanvas>>render: you will see that
renderOn:. Do not conclude that you can send
renderOn: in your
Object>>renderOn: is an internal method which is part of a double dispatch between the canvas and an object it is rendering. Do not invoke it directly!