Template language proposal

Joe Armstrong (AL/EAB) joe.armstrong@REDACTED
Thu Sep 22 12:10:05 CEST 2005


 
Here is a proposal for a template language (suitable for yaws)

Here is a file mis.template - it contains two templates main and bar

       @module misc
       @template main(E)

       <? M= 30, N = 20, "" ?>

       <h1>Example</h1>

       <p> Factorial <? N ?> is <? fac(N) ?>
       I suppose and M = <? M ?>.

       <? bar(fac(M)) ?>

       @template bar(X) 

       <table><tr><td bgcolor="grey"> <? X ?> </tr></td></table>

       @code

       fac(0) -> 0;
       fac(N) -> N * fac(N-1).

Semantics

	<? Expr1, Expr2, ... Exprn ?>

means evaluate Expr1, Expr2 in order, the result is the result of
evaluating ExprN - this value is pasted into the doument - and bindings created
in evaluation Expr1,... are carried forward.




It is expanded into:

	  -module(misc).

	  -export([test/0, main/1, bar/1]).

	  test() ->
	      binary_to_list(list_to_binary(main(""))).

	  main(E) ->
	      V1 = s(<<"\n">>),
	      V2 = s(begin M = 30, N=20, "" end),
	      V3 = s(<<"<h1>Example</h1>\n<p>Factorial ">>),
	      V4 = s(begin N end),
	      V5 = s(<<" is ">>),
	      V6 = s(begin fac(N) end),
	      V7 = s(<<" I supppose and M = ">>),
	      V8 = s(begin M end),
	      V9 = s(begin bar(fac(M)) end),
	      [V1,V2,V3,V4,V5,V6,V7,V8,V9].

	  bar(X) ->
	      V1 = s(<<"<table><tr><td bgcolor=\"grey\">">>),
	      V2 = s(begin X end),
	      V3 = s(<<"</tr></td></table>">>),
	      [V1,V2,V3].

	  fac(0) -> 1;
	  fac(N) -> N * fac(N-1).

	  s(N) when integer(N) -> 
	      integer_to_list(N);
	  s(N)  when atom(N) ->
	      atom_to_list(N);
	  s(N) ->
	      N.

Evaluating misc:test() yields

    > misc:test().
"\n<h1>Example</h1>\n<p>Factorial 20 is 2432902008176640000 I supppose and M = 30<table><tr><td bgcolor=\"grey\">265252859812191058636308480000000</tr></td></table>"

As expected

   Cheers

/Joe




More information about the erlang-questions mailing list