zimki | documentation | search
 
Zimki Documentation - CookBook.HelloWorld.WithArguments

Augmenting Hello World to use a Trimpath Template

First, create the HelloWorld.Trimpath application. Go to the javascript link in the portal and update the Main instance to the read the following:

zimki.library.require('trimpath.js');
function hello(args) {

    // default data object to be passed to the template
    var data = { who: 'Superman' };

    // if we're called with a 'who' argument, override the default
    if (args.who)
        data.who = args.who;

    // set the pagecount attribute
    data.pagecount = 7;

    // render the template 'hello'
    return zimki.render.trimpath('hello', data );
}
zimki.publishPath("/", hello);

Next, we need to amend the "hello" template. Click on the templates link, then click on the "hello" template to edit it. Make it look like this:

<html>
<head>
  <title>my greeting</title>
</head>
<body>
  <h1>Hello ${who}</h1>
  <p>Pagecount: ${pagecount}</p>
</body>
</html>

Now visit your realm again, and you should see the greeting "Hello Superman".

Append '?who=Lois' to the realm url and watch the page change to 'Hello Lois'; you have made your page take an optional argument.

Unfortunately the pagecount is always 7. See the CookBook.PersistingObjects example for a way to deal with this.

What did we do?

We amended the 'hello()' function to pass an argument to the template, specifying who we should greet. If the function is passed the argument who, that will be passed to the template. Otherwise, the string Superman will be used.

See also

Parent

CookBook.HelloWorld