F3-fatfree Small PHP Framework Tutorial (v) _php Tutorial

Source: Internet
Author: User
Tags php framework
See here I want to write a HelloWorld program, but this time the program needs to invoke the HTM to implement, first we know that Fatfree mainly PHP as the engine, we define a template.htm file:

Hello, !

Note that the name here is a variable that has not yet been initialized

Then inside 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();

The view inside is a built-in object that calls the HTM file, and then it initializes the name and then calls template to output HelloWorld, which should be fine.


There is also a template for the fatfree itself, in the following format:

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 differs from the reference to the variable in the file.

You can see that the Fatfree variable starts with the @ sign, and the framework automatically generates the same class as the file name, which is the template, the name of the file.

For example, if you define:

$f3->set('buddy',array('Tom','Dick','Harry'));

And then write it down in the template:

{{ @buddy[0] }}, {{ @buddy[1] }}, and {{ @buddy[2] }}

You can output buddy array elements, but if you only write them in the file {{ @buddy }}, then because the transmission comes in an array, the output is a string of ' array '.

The calls inside the file 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 actions are valid as long as you have defined the variables in the source file completely.

and fatfree inside the function definition is this:

$f3->set('func',    function($a,$b) {        return $a.', '.$b;    });
After the definition is complete, you can call:

{{ @func('hello','world') }}

Another trick is to call a different file inside the file:


  
   
If you're afraid of trouble, you can call it in the form of a variable, 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');
And then:


  
   
This include can also be set to a conditional limit:


  
   
The count here is the element that computes the @items array, which is not described in detail here.



Comments:


  
       
   
    

A chunk of HTML we don't want displayed at the moment

And also

A chunk of HTML we don't want displayed at the moment

*}
Are all comments inside the fatfree.


Conditional statements:


  
       
   
    
    
     Inserted if condition is false
   
    
  
   
  
       
   
    
     
            Appears when condition is true    
   
        
   
    
     
            Appears when condition is false    
   
    
  
   
As with IfElse, this is not the case, but if you do not write false then all defaults to true:


  
       
   
    

HTML chunk to be included if condition is true



Then say an array output method:

First define:

$f3->set('fruits',array('apple','orange ',' banana'));
Then in the file


  
       
   
    

{{ trim(@ifruit) }}

So that you can come out with the effect:

apple

orange

banana


Then we have a complicated example:

Defined:

$f3->set('div',    array(        'coffee'=>array('arabica','barako','liberica','kopiluwak'),        'tea'=>array('darjeeling','pekoe','samovar')    ));
Then in the file:


  
               
   
    

{{ @ikey }}

{{ @ispan }}

Output:

    

coffee

arabica barako liberica kopiluwak

tea

darjeeling pekoe samovar

Now found to be very useful, and then to illustrate, key corresponding value is the array current pointer (multidimensional only applicable), value corresponds to the values of the array, will be listed according to the requirements. And if the definition of the array is multidimensional, such as the above div---coffee-arabica this two-dimensional array, repeat also call two times, the first repeat is to enter the first layer of coffee, and then the second call into the Arabica layer, The key corresponds to the pointer at that time.


Of course, you can also judge the category as before:


  
       
   
    

{{ trim(@fruit) }}

If the CTR is an odd number in the odd class, if an even number is the even class


Character encoding:

UTF-8:

$f3->set('ENCODING','ISO-8859-1');

Email Template:

First, let's start with the logical definition of email:

This is 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 are two more unfamiliar PHP functions, the ini_php function is used to modify the php.ini basic configuration file functions, but will be restored after the end of the script run. and the Mail function is PHP built-in core functions, do not need additional installation, that is, send email, here is a simple use mode, respectively, the recipient, subject, content. However, it is important to note that sending success does not mean that the other party will receive it.


Of course, the code above simply sends a message to a single user, but we often send a message to a series of users. So you can't use this simple code anymore.

We can send it using the SMTP class: 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 '));
  
   
 
  


















http://www.bkjia.com/PHPjc/755773.html www.bkjia.com true http://www.bkjia.com/PHPjc/755773.html techarticle See here I want to write a HelloWorld program, but this time the program needs to invoke the HTM to implement, first we know that Fatfree mainly PHP for the engine, we define a Templat ...

  • Related Article

    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.