Home > Article > Backend Development > F3-fatfree small php framework tutorial (5)_PHP tutorial
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>Note that the name here is a variable that has not yet been initialized.
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>The change is that the class of new is different and the reference of the variable in the file is different.
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>You can output the buddy's array elements, but if you only write
{{
@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>As long as these variables are fully defined in your source file, these operations are valid.
The function definition in fatfree is as follows:
<code>$f3->set('func', function($a,$b) { return $a.', '.$b; } );</code>After defining it, you can call it:
<code>{{ @func('hello','world') }}</code>
There is another way to call another file inside the file:
<code><include href="header.htm" /></code>If you are afraid of trouble, you can also call it in the form of a variable. First:
<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>Then:
<code><include href="{{ @content }}" /></code>And this include can also be set to be conditional:
<code><include if="{{ count(@items) >= 2 }}" href="items.htm" /></code>The count here is to count the elements of the @items array, so I won’t go into details here.
Note:
<code><exclude> <p>A chunk of HTML we don't want displayed at the moment</p> </exclude></code>besides
<code>{* <p>A chunk of HTML we don't want displayed at the moment</p> *}</code>They are all comments in fatfree.
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>It’s the same as ifelse, so I won’t go into it here, but if you don’t write false, everything will default to true:
<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>Then in the file
<code><repeat group="{{ @fruits }}" value="{{ @ifruit }}"> <p>{{ trim(@ifruit) }}</p> </repeat></code>This will give you the effect:
<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>Then in the file:
<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>Output:
<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>Now I find it is quite easy to use. Then I would like to explain that the value corresponding to key is the current pointer of the array (only applicable to multi-dimensional), and the value corresponding to value is the value of the array, which will be listed according to requirements. And if the defined array is multi-dimensional, such as the two-dimensional array of div -> coffee -> arabica above, repeat must be called twice. The first repeat is to enter the first layer of coffee, and then the second call is to enter At the Arabica layer, the key corresponds to the current pointer.
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>If ctr is an odd number, enter the odd class; if it is an even number, enter the even class.
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>Our definition:
<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>There are two relatively unfamiliar php functions here. The ini_php function is used to modify the php.ini basic configuration file, but it will be restored after the script is finished. The mail function is a core function built into PHP and does not require additional installation. It is used to send emails. Here is a simple usage mode, which is the recipient, subject, and content. However, please note that successful transmission does not mean that the other party will definitely receive it.
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'));