|
MTemplate: Matlab Code Generation and Text Templating |
|
|
|
Written by Administrator
|
|
Tuesday, 01 November 2005 |
MTEMPLATE_COMPILE fun = mtemplate_compile( template, preserve_whitespace ) Compiles an m template and returns a function handle to use that function.
Arguments template - The name of the template M Templates are a powerfull templating language based on matlab. You can use templates to generate any kind of file you wish, html xml C source M source Template have many of the features of matlab functions. In fact they support nested functions and sub functions. The only restriction is that all matlab 'end' commands must be balanced. A basic template is # function mtemplate_usecase1(rows) <html> <body> # for i = 1:rows <%=genline(i)%> # end </body> </html> # function genline(row) <p> <%= 1:row %> </p> # end #end and it is stored in the file 'mtemplate_usecase1.mt' You can use this template by the following actions. >> f = mtemplate_compile('mtemplate_usecase1.mt'); >> f(3) ans = <html> <body> <p> 1 </p> <p> 1 2 </p> <p> 1 2 3 </p> </body> </html> To view the html in the web browser you could do web(sprintf('text://%s',f(3))); ADVANCED TEMPLATE PROGRAMMING Templates support an advanced notation for cutting down on code within templates. A new operator '#=' is defined for opening a template block. A template block is a block which can be passed to another function for processing. The notation for use is #= iterator_fun( ... iterator_args ... ) @( ... block_args ... ) ... block body ... #end The iterator function should be defined to accept a function handle as its last argument. This function handle implements the template block function iterator_fun( ... iterator_args ... , block_fun ) ... iterator body ... block_fun( ... block_args ... ); end You don't pass the last argument explicity. The template compiler takes care of that. You just have to declare the correct arguments to the block using the @( ... block_args ... ) notation. If the block does not require any arguments the @( ... block_args ... ) part of the opening statement can be left out. See the below example for a simple foreach block. TEMPLATE -------- # function template <table> #= foreach(1:10) @(row) <tr> #= foreach(1:10) @(col) <td> <%= [row col] %> </td> # end </tr> # end <table> # end # function foreach(vector, fun) # for i = 1:length(vector), e = vector(i); <%= fun(e) %> # end # end %%%%%%%%%%%%%%%%%%%%% Another example of using blocks is to create wrappers. For example TEMPLATE -------- #function template #= box( 'cats' ) black white grey red #end #= box( 'dogs' ) big small fat thin #end #end #function box( name, fun ) <div class="box"> <div class="boxheading"><%=name%></div> <div class="boxcontents"> <%= fun() %> </div> </div> #end -------- will generate ------- <div class="box"> <div class="boxheading">cats</div> <div class="boxcontents"> black white grey red </div> </div> <div class="box"> <div class="boxheading">dogs</div> <div class="boxcontents"> big small fat thin </div> </div> Template Blocks and matlab for loops: Template blocks are compiled to nested functions within matlab. Within matlab it is not possible to define a nested function within the body of a for loop. To get around this a function in the template toolbox called 'foreach' exists. This function behaves exactly as the foreach function defined above does so you can nested your template blocks as deep as you like. See mtemplate_usecase3.mt for deep nesting examples using foreach. TEMPLATE DIRECTIVES You can add some directives to the template to control the way it is processed. Directives start with the #? token and follow with a command. The following directives are supported at the moment. #?CWS Collapse whitespace. This collapses all whitespace runs to a single space. All leading whitespace on each line is removed. This is usefull for code generation where whitespace is unimportant as in html code. #?PWS Preserve whitespace. This is the default if no directive is found and preserves whitespace as found in the template. NOTES The compiler generates a file in the same directory as the template. The file is an m file with the same name as the template except with a .m extension. This means it will overwrite any file with that name.<p> You could pre-compile your templates and then just use the generated m-files See mtemplate_usecase1.mt as an example script to start with See also xmlt which is an easy way to embed XML/HTML into your documents Copyright Brad Phelan 2005 http://xtargets.com
Goto downloads area
|
|
Last Updated ( Saturday, 10 December 2005 )
|