Small php framework F3-fatfree tutorial (5) _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
F3-fatfree small php framework tutorial (5 ). Here I want to write another helloworld program, but this time the program needs to call htm for implementation. first we know that fatfree is mainly based on php, and we define a templat program 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('p',    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. If the defined array is multidimensional, for example, the preceding two-dimensional array p-> coffee-> arabica, repeat also needs to be called twice. the first repeat is the first layer to enter 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'));
  
 


















...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.