erlydtl实例¶
简单实例一¶
erlydtl的erlang代码:
1-module(erlydtl_example).
2
3-export([export/0]).
4
5%%
6export() ->
7 export("pri_sound_get.php", "Pri_sound_get", "pri_sound_get", "api_sound", "pri_sound_get.tpl", pri_sound_get).
8
9%%
10%%例:
11%% File: "erlydtl_example.php"
12%% Classname: "Erlydtl_example"
13%% InterfaceName: "Erlydtl_interface"
14%% TemplateName: "template.tpl"
15export(File, ClassName, InterfaceName, TemplateName, Mod)->
16 FileName="./"++File,%要写入的文件名
17 %% variable
18 Variable="$sn; $sign;", %要定义的变量名
19 Options=[
20 {classname, ClassName},
21 {interface_name, InterfaceName},
22 {variable, Variable}
23 ],
24 erlydtl:compile("./template/"++TemplateName, Mod),%模板编译
25 {ok, Data}=Mod:render(Options), %使用编译后的模板
26 file:write_file(FileName, Data). %把生成的内容写入文件
对应的模板:
1<?php
2class {{ classname }} extends MX_Controller {
3
4 // 类自有变量
5 private $interface_name="{{ interface_name }}";
6
7 //结构体
8 public function __construct() {
9 parent::__construct();
10 }
11
12 //首函数
13 public function index(){
14
15 // a. 参数变量声明:
16 {{ variable }};
17
18 return 1;
19
20 }// end the function index()
21
22}// end the Class Pri_sound_get
23
24
25/* End of file {{ classname }}.php */
26/* Location: ./application/modules/api/controller */
27
28
29