View
What is a Smarty Template?
Smarty is a template engine for PHP which helps to keep code cleaner.
Template files are saved with .tpl extension.
Template files contain simple html tags and some php code.
Example :
How to display a message on smarty template?
Add following code to message.tpl
<p>{$message}</p>
Add following code to helloworld.php
class helloworld {
function __construct(Controller $ctl){
}
function page(Controller $ctl){
$ctl->assign("message","Hello world!!!");
$ctl->show_multi_dialog("helloworld", "message.tpl", "Hello world!!!",1000,true,true);
}
}
How to use an array with smarty template?
You can use arrays to display,
1) Radio button options
2) Dropdown menu options
In the template file where you want to use the array,
1)Add the following code to display radio buttons
{html_radios name="fruits" options=$fruits}
2)Add the following code to display dropdown menu options
{html_options name="fruits" options=$fruits}
In the helloworld.php add the following code.
class helloworld {
private $fruits;
function __construct(Controller $ctl){
$this->fruits= [0=>"apple", 1=>"orange"];
$ctl->assign("fruits",$this->fruits);
}
}
Now you can see the dropdown menu and radio buttons when you run the program.
Open a Window
The below code example is used to open a window. You must specify the below attributes.
class: HTML class name
data-class: PHP Class Name
data-function: PHP Function Name
$ctl->assign(key, value): Parse data to a view
$ctl->show_multi_dialog(window identifier, template file name, title, width=600, hide_ok_button=false, $hide_cancel_button=false): To make a window
<div class="ajax-link" data-class="helloworld" data-function="view">Hello World</div>
<?php
class helloworld {
function __construct(Controller $ctl) {}
function view(Controller $ctl){
$ctl->assign("message","Hello World!");
$ctl->show_multi_dialog("hello", "hello.tpl", "Hello World", 1000, true, true);
}
}
Open Multiple Windows
Use an ID field to separate windows.
<div class="ajax-link" data-class="helloworld" data-function="edit" data-id="{$post.id}">Edit</div>
function edit(Controller $ctl){
$id = $ctl->POST('id');
$ctl->show_multi_dialog("edit".$id, "edit.tpl", "Edit Post", 1000, true, true);
}
Close Window
function edit_exe(Controller $ctl){
$id = $ctl->POST('id');
$ctl->close_multi_dialog("edit".$id);
}