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>
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'));

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











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.

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.

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.

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

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

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 master Bootstrap customization and component usage includes: 1. Use CSS variables and Sass preprocessor for style customization; 2. Deeply understand and modify component structure and behavior. Through these methods, a unique user interface can be created to improve the responsiveness and user experience of the website.

Go framework development FAQ: Framework selection: Depends on application requirements and developer preferences, such as Gin (API), Echo (extensible), Beego (ORM), Iris (performance). Installation and use: Use the gomod command to install, import the framework and use it. Database interaction: Use ORM libraries, such as gorm, to establish database connections and operations. Authentication and authorization: Use session management and authentication middleware such as gin-contrib/sessions. Practical case: Use the Gin framework to build a simple blog API that provides POST, GET and other functions.
