Home Backend Development PHP Tutorial F3-fatfree small php framework tutorial (5)_PHP tutorial

F3-fatfree small php framework tutorial (5)_PHP tutorial

Jul 13, 2016 am 10:32 AM
Tutorial frame

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>
Copy after login
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>
Copy after login

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>
Copy after login
<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>
Copy after login
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>
Copy after login

Then write in the template:

<code><p>{{ @buddy[0] }}, {{ @buddy[1] }}, and {{ @buddy[2] }}</p></code>
Copy after login
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&#43;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>
Copy after login
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>
Copy after login
After defining it, you can call it:

<code>{{ @func('hello','world') }}</code>
Copy after login

There is another way to call another file inside the file:

<code><include href="header.htm" /></code>
Copy after login
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>
Copy after login
Then:

<code><include href="{{ @content }}" /></code>
Copy after login
And this include can also be set to be conditional:

<code><include if="{{ count(@items) >= 2 }}" href="items.htm" /></code>
Copy after login
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>
Copy after login
besides

<code>{* <p>A chunk of HTML we don't want displayed at the moment</p> *}</code>
Copy after login
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>
Copy after login
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>
Copy after login


Then say a method to output the array :

First define:

<code>$f3->set('fruits',array('apple','orange ',' banana'));</code>
Copy after login
Then in the file

<code><repeat group="{{ @fruits }}" value="{{ @ifruit }}">
    <p>{{ trim(@ifruit) }}</p>
</repeat></code>
Copy after login
This will give you the effect:

<code><p>apple</p>
<p>orange</p>
<p>banana</p></code>
Copy after login

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>
Copy after login
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>
Copy after login
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>
Copy after login
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>
Copy after login
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>
Copy after login

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>
Copy after login
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>
Copy after login
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'));
Copy after login


















www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/755773.htmlTechArticleSeeing this, I want to write another helloworld program, but this time the program needs to call htm to implement it. First of all We know that fatfree is mainly based on PHP as the engine. We define a template...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

In summer, you must try shooting a rainbow In summer, you must try shooting a rainbow Jul 21, 2024 pm 05:16 PM

After rain in summer, you can often see a beautiful and magical special weather scene - rainbow. This is also a rare scene that can be encountered in photography, and it is very photogenic. There are several conditions for a rainbow to appear: first, there are enough water droplets in the air, and second, the sun shines at a low angle. Therefore, it is easiest to see a rainbow in the afternoon after the rain has cleared up. However, the formation of a rainbow is greatly affected by weather, light and other conditions, so it generally only lasts for a short period of time, and the best viewing and shooting time is even shorter. So when you encounter a rainbow, how can you properly record it and photograph it with quality? 1. Look for rainbows. In addition to the conditions mentioned above, rainbows usually appear in the direction of sunlight, that is, if the sun shines from west to east, rainbows are more likely to appear in the east.

How to evaluate the cost-effectiveness of commercial support for Java frameworks How to evaluate the cost-effectiveness of commercial support for Java frameworks Jun 05, 2024 pm 05:25 PM

Evaluating the cost/performance of commercial support for a Java framework involves the following steps: Determine the required level of assurance and service level agreement (SLA) guarantees. The experience and expertise of the research support team. Consider additional services such as upgrades, troubleshooting, and performance optimization. Weigh business support costs against risk mitigation and increased efficiency.

How to retrieve the wrong chain of virtual currency? Tutorial on retrieving the wrong chain of virtual currency transfer How to retrieve the wrong chain of virtual currency? Tutorial on retrieving the wrong chain of virtual currency transfer Jul 16, 2024 pm 09:02 PM

The expansion of the virtual market is inseparable from the circulation of virtual currency, and naturally it is also inseparable from the issue of virtual currency transfers. A common transfer error is the address copy error, and another error is the chain selection error. The transfer of virtual currency to the wrong chain is still a thorny problem, but due to the inexperience of transfer operations, novices often transfer the wrong chain. So how to recover the wrong chain of virtual currency? The wrong link can be retrieved through a third-party platform, but it may not be successful. Next, the editor will tell you in detail to help you better take care of your virtual assets. How to retrieve the wrong chain of virtual currency? The process of retrieving virtual currency transferred to the wrong chain may be complicated and challenging, but by confirming the transfer details, contacting the exchange or wallet provider, importing the private key to a compatible wallet, and using the cross-chain bridge tool

How do the lightweight options of PHP frameworks affect application performance? How do the lightweight options of PHP frameworks affect application performance? Jun 06, 2024 am 10:53 AM

The lightweight PHP framework improves application performance through small size and low resource consumption. Its features include: small size, fast startup, low memory usage, improved response speed and throughput, and reduced resource consumption. Practical case: SlimFramework creates REST API, only 500KB, high responsiveness and high throughput

How to choose the best golang framework for different application scenarios How to choose the best golang framework for different application scenarios Jun 05, 2024 pm 04:05 PM

Choose the best Go framework based on application scenarios: consider application type, language features, performance requirements, and ecosystem. Common Go frameworks: Gin (Web application), Echo (Web service), Fiber (high throughput), gorm (ORM), fasthttp (speed). Practical case: building REST API (Fiber) and interacting with the database (gorm). Choose a framework: choose fasthttp for key performance, Gin/Echo for flexible web applications, and gorm for database interaction.

Why do you need to know histograms to learn photography? Why do you need to know histograms to learn photography? Jul 20, 2024 pm 09:20 PM

In daily shooting, many people encounter this situation: the photos on the camera seem to be exposed normally, but after exporting the photos, they find that their true form is far from the camera's rendering, and there is obviously an exposure problem. Affected by environmental light, screen brightness and other factors, this situation is relatively normal, but it also brings us a revelation: when looking at photos and analyzing photos, you must learn to read histograms. So, what is a histogram? Simply understood, a histogram is a display form of the brightness distribution of photo pixels: horizontally, the histogram can be roughly divided into three parts, the left side is the shadow area, the middle is the midtone area, and the right side is the highlight area; On the left is the dead black area in the shadows, while on the far right is the spilled area in the highlights. The vertical axis represents the specific distribution of pixels

How does the learning curve of PHP frameworks compare to other language frameworks? How does the learning curve of PHP frameworks compare to other language frameworks? Jun 06, 2024 pm 12:41 PM

The learning curve of a PHP framework depends on language proficiency, framework complexity, documentation quality, and community support. The learning curve of PHP frameworks is higher when compared to Python frameworks and lower when compared to Ruby frameworks. Compared to Java frameworks, PHP frameworks have a moderate learning curve but a shorter time to get started.

Stable Diffusion 3 construction tutorial and official example demonstration, GALAXY GeForce RTX 4070 Ti SUPER Xingyao OC runs to full speed in an instant Stable Diffusion 3 construction tutorial and official example demonstration, GALAXY GeForce RTX 4070 Ti SUPER Xingyao OC runs to full speed in an instant Jun 24, 2024 am 05:59 AM

As the latest version of the AI ​​image generation model, StableDiffusion3 comes with great expectations. I believe that many friends must have used the image generation model more or less in work and life, so let’s share Stablediffusion3 below. Let’s take a look at the local construction process. Without further ado, here’s the practical information. The platform configuration used in this build is as follows: Considering the strong demand for computing power when running Stablediffusion3 locally, we chose the GALAXY GeForceRTX4070TiSUPER Xingyao OC graphics card this time. GEFORCERTX4070TiSUPER is built based on the AD103 core and is also the largest graphics card of this kind.

See all articles