3.2. The basics

3.2.1. Create a new layout

  • Choose an identifier, e.g. my-layout.

  • Create the directory structure as described in Section 2.1.3, “Layouts and views” or simply copy an existing layout from the naos.framework.v5 directory.

  • Edit the file layout/my-layout/layout.tpl and set the id attributes on the tags you want to represent a view output area.

    Example 3.1. The template file of a layout which is going to have one view (id=view_main).

    						<html >
    						
    						<head>
    							<title>My Layout</title>
    						</head>
    						
    						<body>
    						
    						<!-- view: main -->
    						<div id="view_main"></div>
    						
    						</body>
    						
    						</html>
    						

  • Edit the file layout/my-layout/layout.conf and define how the framework should recognize the view areas.

    Example 3.2. The configuration file of a layout which is going to have one view (with view identifier main).

    						[layout]
    						
    						title = tr: My Layout
    						views = main						; comma separated list
    						
    						view.main.match = id:view_main		; define identification of html tag, which we want to use as view output
    						view.main.replace = inner			; modes are "inner" and "outer"
    						

  • Activate the new layout in your application.conf file.

    Example 3.3. Activate the layout my-module in your application.

    						[layout]								; activated layouts
    
    						layouts = naos,my-layout				; comma separated list
    						
    						layout.naos.title = tr: NAOS Layout
    						layout.naos.description = tr: Framework Layout
    						layout.naos.context = naos				; context is either "naos" or "application"
    						
    						layout.my-layout.title = tr: My Layout
    						layout.my-layout.description = tr: Your new layout
    						layout.my-layout.context = application	; context is either "naos" or "application"
    						

  • Rebuild the applications apache.conf and restart/reload apache afterwards.

    					phing -f ../naos.framework.v5/development/build-config-apache.xml
    					

3.2.2. Create a new module

  • Choose an identifier, e.g. my-module.

  • Create the directory structure as described in Module or simply copy an existing module from the naos.framework.v5 directory.

  • Activate the new module in your application.conf file.

    Example 3.4. Activate the module my-module in your application.

    						[module]								; activated modules
    						
    						modules = my-module,naos-error			; comma separated list of module identifiers
    						
    						module.my-module.context = application	; context is either "naos" or "application"
    						module.naos-error.context = naos
    						

  • Rebuild the applications apache.conf and restart/reload apache afterwards.

    					phing -f ../naos.framework.v5/development/build-config-apache.xml
    					

3.2.3. Create a new controller

  • Choose an identifier, e.g. my-forum.

  • Create the appropriate PHP file, e.g. according to this example it is module/my-module/controllers/MyForumController.php. See Controller for further information.

    					<?php
    					class Module_MyModule_MyForumController extends Naos_Controller_Action
    					{
    						// ...
    					}
    					

3.2.4. Create a new action

  • Choose an identifier, e.g. forum-outline.

  • Add the appropriate method to the controller. See Action for further information.

    					<?php
    					class Module_MyModule_MyForumController extends Naos_Controller_Action
    					{
    						public function forumOutlineAction()
    						{
    							// ...
    						}
    					}
    					

3.2.5. Render a view output

The output rendering is based on an internal HTML compiler as well as the well-known Smarty Template Engine. This is an simple example which produces output based on some variables. See the Smarty Documentation for further information.

  • Add the render call to the appropriate action.

    					<?php
    					class Module_MyModule_MyForumController extends Naos_Controller_Action
    					{
    						public function forumOutlineAction()
    						{
    							// ...
    							
    							$this->view->assign('username', 'peter.pan');
    							$this->view->assign('lastname', 'Pan');
    							
    							$this->render();
    						}
    					}
    					
  • Add a template to the template directory of the controller with the name <action>.tpl, in this example it is module/my-module/views/template/my-forum/forum-outline.tpl.

    					<div>
    						Hello Mr <strong>{$lastname|escape}</strong>, your username is <strong>{$username|escape}</strong>.
    					</div>
    					

3.2.6. Create a data node and menu entry

  • Choose a name for the data node, e.g. welcome.

  • Add the data node name to the application.conf.

    Example 3.5. Data node welcome configured in application.conf.

    						[datanode]			; predefined datanodes
    
    						nodes = welcome											; comma separated list
    						
    						; define "welcome" node
    						node.welcome.id = A										; identifier (everything except numbers)
    						node.welcome.title = tr: Welcome
    						node.welcome.description = tr: Welcome Screen Data
    						

  • Choose an identifier for the menu, e.g. default.

  • Add the menu configuration to the application.conf.

    Example 3.6. Menu default configured in application.conf.

    						[menu]			; menus and predefined menu items
    
    						menus = default									; comma separated list
    						menu_defaults = default							; standard menu, comma separated list
    						
    						menu.default.title = tr: Default Menu
    						
    						menu.default.items =							; to be defined
    						menu.default.item_defaults =					; to be defined
    						

  • Choose an identifier for the menu item, e.g. welcome.

  • Add the menu item configuration to the application.conf.

    Example 3.7. Menu item welcome configured in application.conf.

    						[menu]			; menus and predefined menu items
    
    						menus = default									; comma separated list
    						menu_defaults = default							; standard menu, comma separated list
    						
    						menu.default.title = tr: Default Menu
    						
    						menu.default.items = welcome					; to be defined
    						menu.default.item_defaults = welcome			; to be defined
    						
    						; define "welcome" menu item
    						menu.default.item.welcome.id = I								; identifier (everything except numbers)
    						menu.default.item.welcome.title = tr: Welcome
    						menu.default.item.welcome.position = 1							; menu item position
    						menu.default.item.welcome.locales = *							; comma separated list of locale codes or all = *
    						menu.default.item.welcome.layout = naos							; layout identifier
    						menu.default.item.welcome.view.main.node = A					; node identifier
    						menu.default.item.welcome.view.main.module = naos-example		; view "main": module we want to call
    						menu.default.item.welcome.view.main.controller = view			; view "main": controller we want to call
    						menu.default.item.welcome.view.main.action = welcome			; view "main": action we want to call
    						menu.default.item.welcome.view.main.parameter =					; view "main": the parameter
    						

3.2.7. Processing a form

  • Choose an identifier for the form, e.g. login.

  • Create the HTML code for the form and render the template in an action, e.g. login-form. We assume the module my-module, the controller test and the subsequent action login.

    Example 3.8. HTML example code of form login.

    						<compiler-flags autotranslate="true"/>
    
    						<h1>Login</h1>
    						
    						<div class="form-errors:my-module/login" errorClass="error"></div>
    						
    						<form name="my-module/login" action="vapp:/my-module/test/login">
    							Username: <input type="text" name="username" errorClass="error"/>
    							Password: <input type="password" name="password" errorClass="error"/>
    							<input type="checkbox" name="autologin"/> auto-login
    							<input type="submit" name="login" value="Login"/>
    						</form>
    						
    						<?php
    						class Module_MyModule_TestController extends Naos_Controller_Action
    						{
    							public function loginFormAction()
    							{
    								$this->render();
    							}
    						}
    						

  • Add a form definition to the forms.conf of the module in case you want to guarantee a certain state of integrity of the data to your business logic.

    Example 3.9. Example form definition of form login.

    						; Default attributes of different naos input types:
    						; text		=> text
    						; textarea*	=> text
    						; password	=> text
    						; date~		=> isodate,text
    						; time~		=> isotime,text
    						; datetime~	=> isodatetime,text
    						; email~	=> email,text
    						; decimal~	=> decimal,text
    						; checkbox	=> checked,text
    						; radio		=> checked,text
    						; select*	=> text
    						; hidden	=> text
    						; file		=> file
    						; submit	=> submitted,text
    						;
    						; * = different tag
    						; ~ = based on input tag
    						
    						[login]			; form: login
    						
    						items=username,password,autologin
    						
    						item.username.title = tr: Username
    						item.username.attributes = text			; list of required attributes (text,isodate,isotime,isodatetime,checked)
    						item.username.text_filter.0.type = regexp
    						item.username.text_filter.0.parameter.regexp = "^[a-z]+$"
    						item.username.text_filter.0.parameter.errorMessage = "tr: Please use the characters a-z in %{title}!"
    						
    						item.password.title=tr: Password
    						item.password.attributes=text
    						item.password.text_filter.0.type=password
    						
    						item.autologin.title=tr: Autologin
    						item.autologin.attributes=checked
    						

  • Process the data in your target action.

    Example 3.10. Processing of form login.

    						<?php
    						class Module_MyModule_TestController extends Naos_Controller_Action
    						{
    							public function loginFormAction()
    							{
    								$this->render();
    							}
    							
    							public function loginAction()
    							{
    						    	$form = Naos_Form::form('my-module', 'login', false); // pass true if you require to have a received form
    						    	
    						    	if($form->stateReceived() == false || $form->hasErrors())
    						    	{
    							    	$this->getRequest()->redispatch('login-form');
    						    	}
    						    	else
    						    	{
    							    	$this->getRequest()->redispatch('login-done',
    							    		array('user' => $form->getAttribute('username')));
    						    	}
    							}
    							
    							public function loginDoneAction()
    							{
    								// $this->render();
    							}
    						}