F3-fatfree小型php框架教程(五)
看到这里我想再写一个helloworld的程序,不过这次的程序需要调用htm来实现,首先我们知道fatfree主要是以php为引擎的,我们定义一个template.htm文件:
<code><p>Hello, <?php echo $name; ?>!</p></code>
然后在主函数里面:
<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>
这里面的view是内置对象,用于调用htm文件,然后这里给name初始化了然后调用template,输出helloworld,应该没什么问题。
这里也有另外一种fatfree本身的模板,格式是:
<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>
可以发现fatfree的变量都是以@符号开头的,而且框架会自动产生跟跟文件名一样的类,在这里是template,也就是文件的名字。
再举个例子,如果你定义了:
<code>$f3->set('buddy',array('Tom','Dick','Harry'));</code>
然后再在template里面写下:
<code><p>{{ @buddy[0] }}, {{ @buddy[1] }}, and {{ @buddy[2] }}</p></code>
{{
@buddy }}
的话,那么因为传输进来的是一个数组,所以输出的是‘Array’这个字符串。
文件里面的调用还支持一系列的操作:
<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>
还有fatfree里面给函数定义是这样的:
<code>$f3->set('func', function($a,$b) { return $a.', '.$b; } );</code>
<code>{{ @func('hello','world') }}</code>
还有一招在文件里面调用另外一个文件:
<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>
注释:
<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>
条件语句:
<code><check if="{{ @page=='Home' }}"> <false><span>Inserted if condition is false</span></false> </check> <check if="{{ @gender=='M' }}"> <true> <p>Appears when condition is true</p> </true> <false> <p>Appears when condition is false</p> </false> </check></code>
<code><check if="{{ @loggedin }}"> <p>HTML chunk to be included if condition is true</p> </check></code>
然后说一个数组输出的方法:
首先定义:
<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>
然后我们来个复杂的例子:
定义:
<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 }}"> <p> <p><span><b>{{ @ikey }}</b></span></p> <p> <repeat group="{{ @idiv }}" value="{{ @ispan }}"> <span>{{ @ispan }}</span> </repeat> </p> </p> </repeat></code>
<code><p> <p><span><b>coffee</b></span></p> <p> <span>arabica</span> <span>barako</span> <span>liberica</span> <span>kopiluwak</span> <p> </p> <p> <p><span><b>tea</b></span></p> <p> <span>darjeeling</span> <span>pekoe</span> <span>samovar</span> </p> </p></code>
当然也可以像之前那样判断类别:
<code><repeat group="{{ @fruits }}" value="{{ @fruit }}" counter="{{ @ctr }}"> <p class="{{ @ctr%2?'odd':'even' }}">{{ trim(@fruit) }}</p> </repeat></code>
字符编码:
UTF-8:
<code>$f3->set('ENCODING','ISO-8859-1');</code>
email模板:
首先下面先说一下email的逻辑定义:
welcome.txt文件里面是这样的
<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>
当然,上面这个代码只是给单一用户发送邮件的代码,但是我们经常要给一系列的用户发送邮件。所以就不能用这种简单的代码了。
我们可以用smtp的类来发送:教程
$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 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

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.

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.
