Hosts file:
Hello, !
Note that the name here is a variable that has not been initialized.
Then in the main function:
$f3=require('lib/base.php');$f3->route('GET /', function($f3) { $f3->set('name','world'); $view=new View; echo $view->render('template.htm'); // Previous two lines can be shortened to: // echo View::instance()->render('template.htm'); });$f3->run();
Here, the view is a built-in object used to call the htm file, initialize the name here, call the template, and output helloworld. It should be fine.
There is also another fatfree template. The format is:
Hello, {{ @name }}!
$f3=require('lib/base.php');$f3->route('GET /', function($f3) { $f3->set('name','world'); $template=new Template; echo $template->render('template.htm'); // Above lines can be written as: // echo Template::instance()->render('template.htm'); });$f3->run();
The change is that the new class is different from the variable reference in the file.
We can find that all fatfree variables start with the @ symbol, and the framework will automatically generate the same class as the file name. Here it is the template, that is, the file name.
For another example, if you define:
$f3->set('buddy',array('Tom','Dick','Harry'));
Then write in the template:
{{ @buddy[0] }}, {{ @buddy[1] }}, and {{ @buddy[2] }}
You can output the array elements of buddy, but if you only write
{{ @buddy }}
Then, because an Array is transmitted, the output is the 'array' string.
File calls also support a series of operations:
{{ 2*(@page-1) }}{{ (int)765.29+1.2e3 }}Female{{ var_dump(@xyz) }}That is {{ preg_match('/Yes/i',@response)?'correct':'wrong' }}!
{{ @obj->property }}
These operations are valid as long as the variables are fully defined in the source file.
In fatfree, the function is defined as follows:
$f3->set('func', function($a,$b) { return $a.', '.$b; });
After definition, you can call:
{{ @func('hello','world') }}
Another trick isThe file calls another file:
If you are in trouble, you can also use the form of variables. First:
// switch content to your blog sub-template$f3->set('content','blog.htm');// in another route, switch content to the wiki sub-template$f3->set('content','wiki.htm');
Then:
This include can also be set to conditional:
Here count is the element used to calculate the @ items array, which is not detailed here.
Note:
A chunk of HTML we don't want displayed at the moment
And
{* A chunk of HTML we don't want displayed at the moment
*}
All are comments in fatfree.
Condition Statement:
Inserted if condition is false
Appears when condition is true
Appears when condition is false
Like ifelse, I won't talk about it here, but if it is not set to false, it will all be true by default:
HTML chunk to be included if condition is true
And then sayArray outputMethod:
First, define:
$f3->set('fruits',array('apple','orange ',' banana'));
Then file
{{ trim(@ifruit) }}
In this way, we can see the effect:
apple
orange
banana
Then let's take a complex example:
Definition:
$f3->set('div', array( 'coffee'=>array('arabica','barako','liberica','kopiluwak'), 'tea'=>array('darjeeling','pekoe','samovar') ));
In the file:
{{ @ikey }}
{{ @ispan }}
Output:
coffee
arabica barako liberica kopiluwak
tea
darjeeling pekoe samovar
Now we can see that it is quite easy to use. Next, we need to explain that the value corresponding to the key is the current pointer of the array (applicable in multiple dimensions), and the value corresponding to the value is the value of the array, will be listed as required. In addition, if the defined array is multi-dimensional, for example, the div-> coffee-> arabica two-dimensional array above, repeat also needs to be called twice. The first repeat is the first layer that enters coffee, then the second call enters the arabica layer, and the key corresponds to the current pointer.
You can also determine the category as before:
{{ trim(@fruit) }}
If the ctr is an odd number, it is added to the odd class. If the ctr is an even number, it is the even class.
Character encoding:
UTF-8:
$f3->set('ENCODING','ISO-8859-1');
Email template:
First, let's talk about the Logic definition of email:
This is the case in the welcome.txt file.
MIME-Version: 1.0Content-type: text/html; charset={{ @ENCODING }}From: {{ @from }}To: {{ @to }}Subject: {{ @subject }}Welcome, and thanks for joining {{ @site }}!
Our definition:
$f3->set('from','
');$f3->set('to','
');$f3->set('subject','Welcome');ini_set('sendmail_from',$f3->get('from'));mail( $f3->get('to'), $f3->get('subject'), Template::instance()->render('email.txt','text/html'));
Here there are two unfamiliar php functions. The ini_php function is used to modify the basic configuration file of php. ini, but it will be restored after the script is run. The mail function is the built-in core function of php and does not require additional installation, that is, sending emails. Here is a simple usage mode, which includes the receiver, subject, and content. However, if the message is sent successfully, the recipient will not receive the message.
Of course, the above Code only sends emails to a single user, but we often send emails to a series of users. So you cannot use this simple code.
We can use smtp class to send: tutorial
$mail=new SMTP('smtp.gmail.com',465,'SSL','account@gmail.com','secret');$mail->set('from','
');$mail->set('to','"Slasher"
');$mail->set('subject','Welcome');$mail->send(Template::instance()->render('email.txt'));