F3-fatfree小型php框架课程(五)
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了F3-fatfree小型php框架课程(五),小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含5229字,纯文字阅读大概需要8分钟。
内容图文
![F3-fatfree小型php框架课程(五)](/upload/InfoBanner/zyjiaocheng/230/08f558fc7f1b40f29bdf502ff143be6b.jpg)
看到这里我想再写一个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