Seeing this, I want to write another helloworld program, but this time the program needs to call htm to implement. First of all, we know that fatfree mainly uses PHP as the engine. We define a template.htm file:
<code><p>Hello, <?php echo $name; ?>!</p></code>
Then inside the main function:
<code>$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();</code>
The view here is a built-in object, which is used to call html files. Then the name is initialized here and then the template is called to output helloworld. There should be no problem.
There is also another template of fatfree itself, the format is:
<code><p>Hello, {{ @name }}!</p></code>
<code>$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();</code>
You can find that the variables of fatfree all start with the @ symbol, and the framework will automatically generate a class with the same name as the file, in this case template, which is the name of the file.
For another example, if you define:
<code>$f3->set('buddy',array('Tom','Dick','Harry'));</code>
Then write in the template:
<code><p>{{ @buddy[0] }}, {{ @buddy[1] }}, and {{ @buddy[2] }}</p></code>
{{
@buddy }}
in the file, then because an array is transmitted, the string 'Array' will be output.
The calls in the file also support a series of operations:
<code>{{ 2*(@page-1) }} {{ (int)765.29+1.2e3 }} <option value="F" {{ @active?'selected="selected"':'' }}>Female</option> {{ var_dump(@xyz) }} <p>That is {{ preg_match('/Yes/i',@response)?'correct':'wrong' }}!</p> {{ @obj->property }}</code>
The function definition in fatfree is as follows:
<code>$f3->set('func', function($a,$b) { return $a.', '.$b; } );</code>
<code>{{ @func('hello','world') }}</code>
There is another way to call another file inside the file:
<code><include href="header.htm" /></code>
<code>// 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');</code>
<code><include href="{{ @content }}" /></code>
<code><include if="{{ count(@items) >= 2 }}" href="items.htm" /></code>
Note:
<code><exclude> <p>A chunk of HTML we don't want displayed at the moment</p> </exclude></code>
<code>{* <p>A chunk of HTML we don't want displayed at the moment</p> *}</code>
Conditional statement:
<code><check if="{{ @page=='Home' }}"> <false><span>Inserted if condition is false</span></false> </check> <check if="{{ @gender=='M' }}"> <true> <div>Appears when condition is true</div> </true> <false> <div>Appears when condition is false</div> </false> </check></code>
<code><check if="{{ @loggedin }}"> <p>HTML chunk to be included if condition is true</p> </check></code>
Then say a method to output the array :
First define:
<code>$f3->set('fruits',array('apple','orange ',' banana'));</code>
<code><repeat group="{{ @fruits }}" value="{{ @ifruit }}"> <p>{{ trim(@ifruit) }}</p> </repeat></code>
<code><p>apple</p> <p>orange</p> <p>banana</p></code>
Then let’s take a complex example:
Definition:
<code>$f3->set('div', array( 'coffee'=>array('arabica','barako','liberica','kopiluwak'), 'tea'=>array('darjeeling','pekoe','samovar') ) );</code>
<code><repeat group="{{ @div }}" key="{{ @ikey }}" value="{{ @idiv }}"> <div> <p><span><b>{{ @ikey }}</b></span></p> <p> <repeat group="{{ @idiv }}" value="{{ @ispan }}"> <span>{{ @ispan }}</span> </repeat> </p> </div> </repeat></code>
<code><div> <p><span><b>coffee</b></span></p> <p> <span>arabica</span> <span>barako</span> <span>liberica</span> <span>kopiluwak</span> <p> </div> <div> <p><span><b>tea</b></span></p> <p> <span>darjeeling</span> <span>pekoe</span> <span>samovar</span> </p> </div></code>
Of course, you can also determine the category as before:
<code><repeat group="{{ @fruits }}" value="{{ @fruit }}" counter="{{ @ctr }}"> <p class="{{ @ctr%2?'odd':'even' }}">{{ trim(@fruit) }}</p> </repeat></code>
Character encoding:
UTF-8:
<code>$f3->set('ENCODING','ISO-8859-1');</code>
email template:
First of all, let’s talk about the logical definition of email:
The welcome.txt file looks like this
<code>MIME-Version: 1.0 Content-type: text/html; charset={{ @ENCODING }} From: {{ @from }} To: {{ @to }} Subject: {{ @subject }} <p>Welcome, and thanks for joining {{ @site }}!</p></code>
<code>$f3->set('from','<no-reply@mysite.com>'); $f3->set('to','<slasher@throats.com>'); $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') );</code>
Of course, the above code is just for sending emails to a single user, but we often need to send emails to a series of users. So you can't use this simple code.
We can use the smtp class to send: Tutorial
$mail=new SMTP('smtp.gmail.com',465,'SSL','account@gmail.com','secret'); $mail->set('from','<no-reply@mysite.com>'); $mail->set('to','"Slasher" <slasher@throats.com>'); $mail->set('subject','Welcome'); $mail->send(Template::instance()->render('email.txt'));