====== Case Converter Tutorial ====== This tutorial is meant as a first introduction to WACT. We will create a tiny application that accepts a text value as input and will convert that text to all lower case or all upper case text. ===== Hello World ===== Lets start by creating a simple html page for our application. We will include the form elements in our page. Case Converter

Case Converter



Create a new directory in your web server's document root and save this html as index.tpl.html. It is common in WACT for file names to have two extensions. The extension at the end indicates the type of the file for your operating system, so editing programs can choose the correct syntax highlighting and double clicking on the file works as expected. In this case, our file type is html. The inner extension identifies the file as a specific kind of file to WACT. Here, tpl indicates to certain WACT tools that this file is a template. Now we need to create a php file to display this template. This program is the template equivalent of hello world. setView($loader->load('index.tpl.html')); } } $front = new CaseConverter(); $front->start(); ?> Save this file as index.php in the same directory as index.tpl.html. We start out by defining a controller class, ''CaseConverter'' to represent our application. We don't need this to display a template, but we will use it to structure our application later on. ''CaseConverter'' is a [[pattern:Front Controller]] that acts as the single point of entry into our application. The ''setUp'' method is a template method which is called when a new instance of a WactController object is created. ''setUp'' is where we define the structure of our application. In this example, our only structure is the template that we want to display. In order to load our template, we need an instance of a ''WactTemplateLoader''. We use the default settings for ''WactTemplateLoader'', which searches for templates in the php ''include_path'' and caches the compiled templates into ''/tmp/wact''. These settings can be changed if necessary. Once we have our template loader, we can use it to get a view that represents our template. We'll associate the view with our controller with ''setView''. Once this is done, the controller will automatically display the view at the appropriate time. That time is when the ''start'' method is called on the controller. ===== Composing Views ===== WACT uses the [[pattern:CompositeView]] pattern. That means that view objects are arranged in a nested hierarchy. This closely parallels the organization of an HTML page and the DOM. WACT uses the [[pattern:TemplateView]] pattern. This means that templates in WACT are also composite views. The object returned by the ''load'' method in our example is an instance of ''WactView''. However, there is only one object in our hierarchy. Unlike the DOM, WACT will not create an object for each tag in your template. Unless you tell it otherwise, WACT treats the text in a template as plain text. So, lets tell it otherwise. Since we want to work with the form embedded in our template, lets tell WACT to treat the form as a sub-view. We can do this by changing the form tag
to The ''wact:id'' attribute tells WACT to treat the tag and its contents as a sub-view. That subview is identified by the name "converter". We call this the server id. The standard html id attribute is called the client id. In WACT, the server id is not output with the template. If you run the program after this change, you will see that the ''wact:id'' attribute is not in the output of our program. Yet, we can now use that ''converter'' id to manipulate the form in our template. Forms are a special kind of tag in WACT. When we told WACT to make a subview from the tag, WACT automatically recognized the other elements of the form and made sub-views out of them, too. This includes the
setView($loader->load('index.tpl.html')); $this->defineInput(new WactPostParameter('text'))->bindToModel(); $this->setDispatcher(new WactButtonDispatcher()); $this['upper'] = new WactCommandController(new WactCallback($this, 'toUpper')); $this['lower'] = new WactCommandController(new WactCallback($this, 'toLower')); } function toLower($source, $request, $responseModel) { $responseModel->text = strtolower($responseModel->text); } function toUpper($source, $request, $responseModel) { $responseModel->text = strtoupper($responseModel->text); } } $front = new CaseConverter(); $front->start(); ?>