您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

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

2024/5/6 9:18:18发布30次查看
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 }}value=f {{ @active?'selected=selected':'' }}>female>{{ 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'));
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product