Your first website powered by haXe
We have our templates, now we're going to make our "template engine".
Create two haxe files into /src directory : Main.hx and View.hx
package;
import templo.Loader;
import php.Lib;
import php.Web;
import php.FileSystem;
class Main
{
static function main()
{
new Main();
}
public function new()
{
/*set some parameters :
tpl and tmp directories, macros path*/
templo.Loader.BASE_DIR = "tpl/";
templo.Loader.TMP_DIR = "tmp/";
templo.Loader.MACROS = "tpl/macros.mtt";
templo.Loader.OPTIMIZED = true;
/*use the Web API to get all GET and POST parameters
remember that we're going to specify the page we want to display
by passing GET parameters in the URL like this >> http://plop.com/?p=plop*/
var params = Web.getParams();
/*Create a new View , set the context of the page you want to display,
and make a render of the view*/
var view = new View();
/*if the template that we want to request exists, initialize the context variable "templatePage" with the template name,
if not, display the template coresponding to the "page_not_found"*/
view.context.templatePage = params.exists('p')?((FileSystem.exists(Loader.BASE_DIR+params.get('p')+'.mtt'))?params.get('p'):'page_not_found'):'home';
/*template files generated by templo look like this*/
view.context.templatePage = 'tpl__'+view.context.templatePage+'.mtt';
/*Make a render of the current page*/
view.render();
}
}
You can notice the flag OPTIMIZED set to true. In fact, templo provides an optimized mode that you can use while your application is deployed on your server.
Indeed you can decide to install temploc on your server. If the mode OPTIMIZED isn't set, the templo system is able to detect if any file has been modified. If it's the case, templo will be executed in order to recompile your templates.
Setting the flag OPTIMIZED to true will desactivate this process
.Then the View class :
package;
import php.Lib;
class View
{
public var context:Dynamic;
public function new()
{
}
public function render()
{
/*define a title page*/
context.titlePage = "My first website powered by haXe, HSS and temploc2";
/*you can create some content*/
context.someContent = "A content.
";
/*load the main template and make a render of your page
coresponding to the context you created*/
var t = new templo.Loader ("tpl/page.mtt");
var r = t.execute(context);
Lib.print(r);
}
}
Go back into /src directory and compile :
haxe -main Main -php ../ -lib templo
Check that "index.php" have been generated and also some files in the /lib directory :)
And... that's all... just run your website into a browser.