Toupl

{{ Infobox Software | name = Toupl | logo = | screenshot = | caption = | developer = | latest release version = 0.9 beta | latest release date = | latest PREview version = | latest preview date = | operating system = Cross platform | programming language = C++ | genre = template engine | license = Apache License 2.0 | website = http://code.google.com/p/toupl/ }}

Toupl (abbreviate from Text Oriented Universal Programming Language) is a multi target language simplified generic template engine. IT Works on the principle of compiler \ preprocessor, producing target language source code. Syntax based on Foobar 2000 Tagz Script, but modified and extended for template engine purposes. Currently supporting generation to:

  • C++
  • C#
  • Java
  • JavaScript
  • ECMAScript
  • Lua
  • PHP
  • Python
  • Visual Basic.

Syntax

Text May Be simply outputted as is.

Hello world!

This are no other operator except variable\expression output, condition variable\expression output and target language code include. Other operators such as for while etc must be written in terms of target language operators.

Target language target language variable or expression can be outputted by enclosing in % % tags

Variable var0 is equivalent %var0%
1+2=%1+2%

Target language code can be included by enclosing in ?% %? tags

?%
  int i;
  int n=100;
  
  while(i<n)
  {
%?%i*i% ?%
    ++i;
  }
%?

Native condition output must by following construction

?%
var _genImage=function(src,alt,width,height)
{
%?
<img [% src=&%src%&%][% alt=&%alt%&%][% width="&%width%&"%][% height="&%height%&"%]>
?%
  return fpText;
}
%?

Conditional text putted between [% %] tags and will be outputted if all condition variables inside not null. Conditional variables same as ordinary variables but enclosing by &% %& instead % %.

As in any language - there is a own comments

/%
  block comment
%/
//% line comment  

In situation, when you need output text, that contains elements\tags of toupl,you can enclose this in special ignore tags !% %!

!% all /% this % text ?% outputted  %] 
even !% you can output %!
!% if you want to output %!%! -  simply twice is %!

Example of generation

For example. Simple HTML table generation.

/%
Generate html table 
%/ 

?%
var _genTable=function(table)
{
  var fpText="";
%?
<table>
?%
for(var i=0;i<table.length;++i)
{
%?  <tr>
?%
  for(var j=0;j<table[i].length;++j)
  {
%?    <td>[%&%table[i][j]%&%]</td>
?%
  }
%?  </tr>
?%
}
%?</table>
?%
  return fpText;
}
%?

After generation we get

var _genTable=function(table)
{
  fpText="";
fpText+="\n<table>\n";

for(var i=0;i<table.length;++i)
{
fpText+="  <tr>\n";

  for(var j=0;j<table[i].length;++j)
  {
fpText+="    <td>";
var fpVar188;
var fpVar190;
fpVar188=(fpVar190=table[i][j],_fpT(fpVar190));
if(fpVar188)
{
fpText+=fpVar190;
}
fpText+="</td>\n";

  }
fpText+="  </tr>\n";

}
fpText+="</table>\n";

  return fpText;
}

Next we must define _fpT test function

var _fpT=function(obj)
{
  return typeof(obj)!="undefined" && obj!=null;
}

And calling this code

document.write(_genTable([[1,2],[null,4]]));

we get

<table>
  <tr>
    <td>1</td>
    <td>2</td>
  </tr>
  <tr>
    <td></td>
    <td>4</td>
  </tr>
</table>