Marmot Manual

Chapter 9. Getting Started

Consider the following example of a Marmot application making use of the Presentation Layer:

<?php $presentation = new MU_Presentation($_SESSION['theme']); $body =& $presentation->add_body(); $my_form = new MU_Form('my_form'); if (($my_form->user_action) && ($my_form->validated)) { $body->add_element('head_1', null, 'Hello, ' . $my_form->value('your_name') . '!'); } $my_form->action = $_SERVER['SCRIPT_NAME']; $my_form->title = 'A Greeting Form'; $my_form->instructions = 'Please provide your name.'; $section =& $my_form->add_section('section_1'); $section->add_input_item('your_name', array('label'=>'Your Name:', 'type'=>'text', 'size'=>'15','validate' => 'required')); $my_form->add_user_action('save', ' Submit ', 'validate'); $body->add_object($my_form); $presentation->display( array('title' => 'Greeting', 'appname' => 'My First Marmot Application')); ?>

The output from this example and the others in this chapter can be seen in the "examples" directory included in the Marmot distribution. It can be accessed at http://your-server/phpapps/mu/examples/.

We must first instantiate a new MU_Presentation object:

$presentation = new MU_Presentation();

We can then call the MU_Presentation::add_body() method to add a body to our document:

$body =& $presentation->add_body();

Think of the body as a container that holds the rest of the document. $body is a reference to an MU_XML_Element object. Using this reference, we can place additional elements within the body, like a form or a heading. (We will discuss these elements in detail later in the chapter. Though the code used to create the form is relatively simple, quite a lot is being done. Marmot lays out the elements of the form, validates the input returned by the user- the name field cannot be blank- redisplaying the form with errors highlighted if there are any, and presents everything within the framework of a theme.)

Finally, we invoke the MU_Presentation::display() method to give our page a title, our application a name, and process and display the themed output:

$presentation->display(array('title' => 'Greeting', 'appname' => 'My First Marmot Application'));

As you can see, the Presentation Layer greatly reduces the effort required to create a web application. In addition, the programmer does not have to interact with HTML at all.