F3-fatfree小型php框架课程(五)

原创
2016-06-13 11:53:58 951浏览

F3-fatfree小型php框架教程(五)

看到这里我想再写一个helloworld的程序,不过这次的程序需要调用htm来实现,首先我们知道fatfree主要是以php为引擎的,我们定义一个template.htm文件:

>Hello, !

>
注意,这里的name是还没初始化的变量

然后在主函数里面:

$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();

这里面的view是内置对象,用于调用htm文件,然后这里给name初始化了然后调用template,输出helloworld,应该没什么问题。


这里也有另外一种fatfree本身的模板,格式是:

>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();
变动就是new的类不同和文件中变量的引用不同。

[email protected],而且框架会自动产生跟跟文件名一样的类,在这里是template,也就是文件的名字。

再举个例子,如果你定义了:

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

然后再在template里面写下:

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

>
就可以输出buddy的数组元素了,但是如果你只在文件里写 {{ @buddy }}的话,那么因为传输进来的是一个数组,所以输出的是‘Array’这个字符串。

文件里面的调用还支持一系列的操作:

{{ 2*(@page-1) }}{{ (int)765.29+1.2e3 }}>{{ var_dump(@xyz) }}

>That is {{ preg_match('/Yes/i',@response)?'correct':'wrong' }}!

>{{ @obj->property }}
只要你源文件里面给这些变量定义完全了,这些操作都是有效的。

还有fatfree里面给函数定义是这样的:

$f3->set('func',    function($a,$b) {        return $a.', '.$b;    });
定义完之后就可以调用了:

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

还有一招在文件里面调用另外一个文件:

href="header.htm" />
如果你怕麻烦也可以用变量的形式调用,首先:

// 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');
然后:

href="{{ @content }}" />
而且这个include还可以设置为有条件限制的:

if="{{ count(@items) >= 2 }}" href="items.htm" />
[email protected],这里就不详细说了。



注释:

>    

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

>
>
还有

{* 

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

> *}
都是fatfree里面的注释。


条件语句:

if="{{ @page=='Home' }}">    >>Inserted if condition is false>>>if="{{ @gender=='M' }}">    >        
>Appears when condition is true
>
> >
>Appears when condition is false
>
>
>
跟ifelse一样,这里就不说了,但是如果不写false那就全部默认为true:

if="{{ @loggedin }}">    

>HTML chunk to be included if condition is true

>
>


然后说一个数组输出的方法:

首先定义:

$f3->set('fruits',array('apple','orange ',' banana'));
然后文件中

group="{{ @fruits }}" value="{{ @ifruit }}">    

>{{ trim(@ifruit) }}

>
>
这样就可以出来效果:

>apple

>

>orange

>

>banana

>

然后我们来个复杂的例子:

定义:

$f3->set('div',    array(        'coffee'=>array('arabica','barako','liberica','kopiluwak'),        'tea'=>array('darjeeling','pekoe','samovar')    ));
然后文件中:

group="{{ @div }}" key="{{ @ikey }}" value="{{ @idiv }}">    
>

>>>{{ @ikey }}>>

>

> group="{{ @idiv }}" value="{{ @ispan }}"> >{{ @ispan }}> >

>
>
>
输出:

>

>>>coffee>>

>

> >arabica> >barako> >liberica> >kopiluwak>

>

>
>

>>>tea>>

>

> >darjeeling> >pekoe> >samovar>

>
>
现在发现挺好用了吧,然后要说明一下,key对应的值是数组当前指针(多维才适用),value对应的值是数组的值,会根据要求罗列出来。而且如果定义的数组是多维的,例如上面那个div -> coffee -> arabica这个二维数组,repeat也要调用两次,第一次的repeat是进入coffee的第一层,然后第二次调用进入arabica这一层,key对应的就是当时的指针。


当然也可以像之前那样判断类别:

group="{{ @fruits }}" value="{{ @fruit }}" counter="{{ @ctr }}">    

class="{{ @ctr%2?'odd':'even' }}">{{ trim(@fruit) }}

>
>
如果ctr是奇数就进odd类,如果是偶数就是even类


字符编码:

UTF-8:

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

email模板:

首先下面先说一下email的逻辑定义:

welcome.txt文件里面是这样的

MIME-Version: 1.0Content-type: text/html; charset={{ @ENCODING }}From: {{ @from }}To: {{ @to }}Subject: {{ @subject }}

>Welcome, and thanks for joining {{ @site }}!

>
我们的定义:

$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'));
这里有两个比较陌生的php函数,ini_php函数是用来修改php.ini基本配置文件的函数,但是会在脚本运行结束后恢复。而mail函数是php内置的核心函数,不需要额外安装,也就是发email的,这里是简单的使用模式,分别是接收者,主题,内容。但是要注意,发送成功不表示对方一定会接收到。


当然,上面这个代码只是给单一用户发送邮件的代码,但是我们经常要给一系列的用户发送邮件。所以就不能用这种简单的代码了。

我们可以用smtp的类来发送:教程

$mail=new SMTP('smtp.gmail.com',465,'SSL',[email protected]','secret');$mail->set('from','');$mail->set('to','"Slasher" ');$mail->set('subject','Welcome');$mail->send(Template::instance()->render('email.txt'));


















声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。