wCMF Wiki : AddTemplates

HomePage :: Categories :: PageIndex :: RecentChanges :: Login/Register

Model Iteration and Template Matching

The generation process always starts with the template Root.tpl (located in /templates/with_domain_classes). It instructs the generator how to act on different model elements, when he iterates over them (How the mode elements are mapped onto the specific meta model classes is defined in /mappings/metamappings.xml). A typical wCMF root template may look like this:

«DEFINE Root FOR Model»
	«EXPAND Check::Check FOR This»
	«EXPAND DataDefinition::file FOR This»
	«EXPAND Soap::file FOR This»
	«EXPAND Configuration::file FOR This»
	«EXPAND Root FOREACH OwnedElement»
«ENDDEFINE»

«DEFINE Root FOR Package»
	«EXPAND Root FOREACH SubPackage»
	«EXPAND Root FOREACH Class»
«ENDDEFINE»

«DEFINE Root FOR ModelElement»
«ENDDEFINE»

«REM mapper/node ------------------------------------»
«DEFINE Root FOR WCMFNode»
«IF !IsLibraryClass»
	«EXPAND RDBMapper::file FOR This»
	«EXPAND Node::file FOR This»
«ENDIF»
«ENDDEFINE»

«REM mapper/node ------------------------------------»
«DEFINE Root FOR WCMFManyToMany»
«IF !IsLibraryClass»
	«EXPAND RDBMapper::file FOR This»
	«EXPAND Node::file FOR This»
«ENDIF»
«ENDDEFINE»

«REM class ------------------------------------------»
«DEFINE Root FOR Class»
«IF !IsLibraryClass && IsNode»
	«EXPAND Class::file FOR This»
«ENDIF»
«ENDDEFINE»

«REM controller -------------------------------------»
«DEFINE Root FOR WCMFController»
«IF !IsLibraryClass»
	«EXPAND Controller::file FOR This»
«ENDIF»
«ENDDEFINE»

«REM views ------------------------------------------»
«DEFINE Root FOR WCMFView»
«IF !IsLibraryClass»
	«EXPAND View::file FOR This»
«ENDIF»
«ENDDEFINE»


Each time the generator hits an EXPAND command it will look for a template definition (enclosed in DEFINE/ENDDEFINE) that is named like the first parameter and is defined for types of the second parameter (This refers to the type, the current template is defined for). For example

«EXPAND Root FOREACH Class»


will let the generator search for a template definition like

«DEFINE Root FOR Class»
...
«ENDDEFINE»


To allow the template definitions to be located in different files the template name may be prefixed by a filename like in

«EXPAND DataDefinition::file FOR This»


Adding a Template

Say you want to add a html documentation template for each WCMFNode class defined in the model. The template definition should be located in a file called Doc.tpl. There are 3 steps to execute:

  1. Add the template call at the appropriate location. Using the preceeding example you may add a call to Doc::file to the Root template defined for WCMFNode.

...
«REM mapper/node ------------------------------------»
«DEFINE Root FOR WCMFNode»
«IF !IsLibraryClass»
	«EXPAND Doc::file FOR This»
	«EXPAND RDBMapper::file FOR This»
	«EXPAND Node::file FOR This»
«ENDIF»
«ENDDEFINE»
...


  1. Create the template file. Since you decided to put the template definition in a file called Doc, you'll have create that file. We created all wCMF specific files in a folder wcmf (the generator automatically finds them if it is a subfolder). So put an empty Doc.tpl in the folder /templates/with_domain_classes/wcmf.
  2. Provide the template definition. Since the template name is file, you'll have to put a template called file in the file Doc.tpl. The type you define the template for must be WCMFNode. The complete template may look like this:

«REM 
This template expands the documentation for model elements
»
«DEFINE file FOR WCMFNode»
«FILE Name".html"»<?php
/**
 * This file was generated by wCMFGenerator «GeneratorVersion» from «ModelFile» on «Date». 
 * Manual modifications should be placed inside the protected regions.
 */
<html>
<head>
</head>
<body>
<h1>Documentation for «Name»</h1>
<p>«Documentation»</p>
</body>
</html>
«ENDFILE»
«ENDDEFINE»


The line «FILE Name".html"» instructs the generator to generate a file named Name.html in the target's root directory. If you like to reuse this template for different metamodel classes, you may replace the line «DEFINE file FOR WCMFNode» with «DEFINE file FOR ModelElement», which will let the template match for all model elements.
Valid XHTML 1.0 Transitional :: Valid CSS :: Powered by WikkaWiki
Page was generated in 0.0731 seconds