tpl文件:
- <div>{{ BEGIN test }} <p> {{ $num }} {{ $titile }} <br>
- {{ BEGIN abc }} This is a TEST _ {{ $titile}}_{{$num}}<br> {{ END abc }}
- </p>{{ END test }}
- </div>
想实现的效果是:
test中从1循环到4,作为4个
标签,每个
标签中都再循环一次abc,也是四行。
php的预定义数组:
- $arr = array(
- array('num'=>1,"titile"=>"The number 1"),
- array('num'=>2,"titile"=>"The number 2"),
- array('num'=>3,"titile"=>"The number 3"),
- array('num'=>4,"titile"=>"The number 4"),
- );
- $arr2 = array(
- array('num'=>11,"titile"=>"The number 11"),
- array('num'=>12,"titile"=>"The number 12"),
- array('num'=>13,"titile"=>"The number 13"),
- array('num'=>14,"titile"=>"The number 14"),
- );
php示例1
- foreach($arr as $array){
- $t->block("/test",$array);
- }
- foreach($arr2 as $array){
- $t->block("/test/abc",$array);
- }
这个的结果是什么呢?是不是需要的结果呢?
- 1 The number 1
- 2 The number 2
- 3 The number 3
- 4 The number 4
- This is a TEST _ The number 11_11
- This is a TEST _ The number 12_12
- This is a TEST _ The number 13_13
- This is a TEST _ The number 14_14
在上例中,Blitz先对test进行了循环,所以先得到4行,然后又对/test/abc进行了循环,所以又得了四行,一共得了八行,而不是我们需要的结果。那么需要怎么做呢?
正确的写法:
- foreach($arr as $array){
- $t->block("/test",$array);
- foreach($arr2 as $array){
- $t->block("/test/abc",$array);
- }
- }
结果如下:
- 1 The number 1
- This is a TEST _ The number 11_11
- This is a TEST _ The number 12_12
- This is a TEST _ The number 13_13
- This is a TEST _ The number 14_14
- 2 The number 2
- This is a TEST _ The number 11_11
- This is a TEST _ The number 12_12
- This is a TEST _ The number 13_13
- This is a TEST _ The number 14_14
- 3 The number 3
- This is a TEST _ The number 11_11
- This is a TEST _ The number 12_12
- This is a TEST _ The number 13_13
- This is a TEST _ The number 14_14
- 4 The number 4
- This is a TEST _ The number 11_11
- This is a TEST _ The number 12_12
- This is a TEST _ The number 13_13
- This is a TEST _ The number 14_14
思考:
在手册的6.2节中,也提到:
- 6.2. Slowdowns
- Blitz performance receipes are the same as for any web-applications with CPU bottleneck. Following things in Blitz may slow down your application:
- lot of includes (all the data will seat in VFS cache almost immediately, but includes always add some kernel syscalls and need CPU to parse/initialize objects).
- lot of hash lookups to resolve path-variables $obj.smth.blabla, performance downgrades proportionally to the product number_of_variables*average_path_length
- lot of hash lookups back through the scope stack
- lot of block()/iterate() calls instead of preparing set data and just calling $View->display($data)
- lot of non-built-in Blitz methods in templates (user defined callbacks or PHP-functions)
但是要嵌套的循环,就必须使用block,所以还是建议尽量少使用嵌套。实际上对于模板来说,能输出变量已经能解决大多数的情况了,特别是要求性能(速度)的应用中,尽量少使用嵌套的循环。
阅读(1521) | 评论(0) | 转发(0) |