Documentation Home -  API Home

renderer

The renderer allows you to invoke Chrome Server template pages directly or indirectly.

By default every Chrome Server page is equipped with a global function called display() which in turn invokes the renderer object's display() method to process and write the contents of the page template to the client browser.

As the following code illustrates, this default functionality can be replaced by customised versions that use the renderer object in other ways:

function display() {
   if( request.method === GET ) {
      renderer.display();
   } else {
      var name = request.getParameter('name');
      if( name ) {
         database.save('name',name);
         renderer.redirect('/view/name');
      } else {
         renderer.redirect('/error/missingname');
      }
   }
}

Functions

renderer.forward(path)

function
Parameters
pathstringThe path to which the incoming request will be forwarded.

Passes the incoming request to another Chrome Server page to be rendered. The path must be relative to the domain, not the application, and must not include the domain name.

For example:

function display() {
   if( request.method === 'GET' ) {
      renderer.display();
   } else {
      renderer.forward('/errors/unsupported_method');
   }
}
      

The page receiving a forwarded request from the forward(path) function has access to the page parameters, so you can build script logic for these pages as if they were receiving the original GET or POST parameters.

For security reasons you can only forward to content from the same domain (or its aliases) as the domain for which the current script is executing.

renderer.include(path)

function
Parameters
pathstringThe path to the content to be included.

The include function allows you to include arbitrary content into your rendered output without needing to duplicate it wherever it is needed.

For example the following substitutions in the page function would pull in standard header and footer content from other pages.

function display() {
   renderer.include('/header');
   renderer.display();
   renderer.include('/footer');
}
      

Included content will cause the target global and page script to run and will make variable substitutions in the target template from the global variables of the scripts run for the target (not the global variables for the script from which you call the include(path) function).

For security reasons you can only include content from the same domain (or its aliases) as the domain for which the current script is executing.

When including pages avoid creating "loops" where one page incorporates data that includes the first! For example, the script example given above cannot be used as a global script unless the /header and /footer paths are given their own specific display functions that omit the calls to include these pages!

renderer.redirect(path)

function
Parameters
pathstringThe path to which the client browser should be redirected.

Generates a redirect header in the response which causes the client browser to load the page indicated on the path parameter.

The path can be specified as a fully qualified URL including the domain name (e.g. http://example.com/foo/bar/), or relative to the current domain (e.g. /foo/bar/).

Redirects are often used to prevent browser re-submission problems after form POSTs as the "Post, Redirect, Get" or "PRG" pattern. A good article on the subject, albeit oriented towards Java rather than Javascript developers, appeared on The Server Side website as "Redirect After Post" in August 2004.

renderer.display()

function

The function invoked by the default display function implementation. This renders the display template associated with the current page, populating it with the current set of global variables where appropriate.

The page template is the content (usually html) defined on the "Page" tab of the administration application. By default this is populated with a set of Chrome Server documentation links.

The following example shows a typical usage in the PRG pattern where POST requests are processed via a redirect, but GET requests render the page template:

function display() {
   if( request.method === 'GET' ) {
      renderer.display();
   } else if ( request.method === 'POST' ) {
      processFormData(request);
      renderer.redirect('/');
   } else {
      renderer.redirect('/errors/unsupported_method');
   }
}
      

For details of the how the rendering process interacts with the Chrome Server scripts' global variables, see the Chrome Server templates documentation pages.