zimki | documentation | search
 
Zimki Documentation - zimki.publishPath

The 'publishPath' method attaches a function to a URL. It can take either a regex or a string as the URL. In the case that a string is provided as the first parameter to 'publishPath' the handler function is called with one parameter, an Object containing the CGI parameters that the URL is been called with. When 'publishPath' is provided with regex the handler function is also passed the actual path called as its second parameter, and the return value of the regular expression match.

For example:


function helloWorld ( args ) {
  return "Hello World";
}

function helloPath ( args, path, match ) {
  return "Hello " + match[1];
}

zimki.publishPath( "/hello", helloWorld );
zimki.publishPath( /^hello_(.+)/, helloPath );

If a file object is published to the same path as a a JavaScript function (via the File.publish method) the File object takes priority.

The return value from the function will be returned as the contents of the requested page. Return a string or E4X object to just return content. You can return an instance of a zimki.file object to return the contents of that file as the contents of the page. Finally, if you want more control over the mime-type and character set of the returned data, return an Object:


zimki.publishPath('/complex', function() {
  return {
    content => "that page is not found",
    mimetype => "text/plain",
    charset => "iso-8859-1",
    status => 404
  };
});

zimki.publishPath can also publish a simple string to a path:

zimki.publishPath('/static', 'This string will be published');

Parent

zimki