Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4261602
  • 博文数量: 601
  • 博客积分: 15410
  • 博客等级: 上将
  • 技术积分: 6884
  • 用 户 组: 普通用户
  • 注册时间: 2007-05-16 08:11
个人简介

独学而无友,则孤陋而寡闻!

文章分类

全部博文(601)

文章存档

2020年(1)

2018年(4)

2017年(7)

2016年(42)

2015年(25)

2014年(15)

2013年(36)

2012年(46)

2011年(117)

2010年(148)

2009年(82)

2008年(37)

2007年(41)

分类: 系统运维

2012-08-10 21:21:00

tpl文件:

  1. <div>{{ BEGIN test }} <p> {{ $num }} &nbsp;&nbsp; {{ $titile }} <br>

  2.           {{ BEGIN abc }} This is a TEST _ {{ $titile}}_{{$num}}<br> {{ END abc }}

  3.           </p>{{ END test }}

  4.   </div>
想实现的效果是:
 test中从1循环到4,作为4个

标签,每个

标签中都再循环一次abc,也是四行。

php的预定义数组:

  1. $arr = array(
  2.                          array('num'=>1,"titile"=>"The number 1"),
  3.                          array('num'=>2,"titile"=>"The number 2"),
  4.                          array('num'=>3,"titile"=>"The number 3"),
  5.                          array('num'=>4,"titile"=>"The number 4"),
  6.                  );
  7. $arr2 = array(
  8.                          array('num'=>11,"titile"=>"The number 11"),
  9.                          array('num'=>12,"titile"=>"The number 12"),
  10.                          array('num'=>13,"titile"=>"The number 13"),
  11.                          array('num'=>14,"titile"=>"The number 14"),
  12.                  );
php示例1

  1. foreach($arr as $array){
  2.                   $t->block("/test",$array);

  3. foreach($arr2 as $array){
  4.                   $t->block("/test/abc",$array);
  5.                   }
这个的结果是什么呢?是不是需要的结果呢?


  1. 1 The number 1

  2. 2 The number 2

  3. 3 The number 3

  4. 4 The number 4
  5. This is a TEST _ The number 11_11
  6. This is a TEST _ The number 12_12
  7. This is a TEST _ The number 13_13
  8. This is a TEST _ The number 14_14

在上例中,Blitz先对test进行了循环,所以先得到4行,然后又对/test/abc进行了循环,所以又得了四行,一共得了八行,而不是我们需要的结果。那么需要怎么做呢?
正确的写法:

  1. foreach($arr as $array){
  2.                   $t->block("/test",$array);
  3.                   foreach($arr2 as $array){
  4.                           $t->block("/test/abc",$array);
  5.                   }

  6.           }
结果如下:
  1. 1 The number 1
  2. This is a TEST _ The number 11_11
  3. This is a TEST _ The number 12_12
  4. This is a TEST _ The number 13_13
  5. This is a TEST _ The number 14_14

  6. 2 The number 2
  7. This is a TEST _ The number 11_11
  8. This is a TEST _ The number 12_12
  9. This is a TEST _ The number 13_13
  10. This is a TEST _ The number 14_14

  11. 3 The number 3
  12. This is a TEST _ The number 11_11
  13. This is a TEST _ The number 12_12
  14. This is a TEST _ The number 13_13
  15. This is a TEST _ The number 14_14

  16. 4 The number 4
  17. This is a TEST _ The number 11_11
  18. This is a TEST _ The number 12_12
  19. This is a TEST _ The number 13_13
  20. This is a TEST _ The number 14_14

思考:
在手册的6.2节中,也提到:

  1. 6.2. Slowdowns
  2. Blitz performance receipes are the same as for any web-applications with CPU bottleneck. Following things in Blitz may slow down your application:

  3.     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).
  4.     lot of hash lookups to resolve path-variables $obj.smth.blabla, performance downgrades proportionally to the product number_of_variables*average_path_length
  5.     lot of hash lookups back through the scope stack
  6.     lot of block()/iterate() calls instead of preparing set data and just calling $View->display($data)
  7.     lot of non-built-in Blitz methods in templates (user defined callbacks or PHP-functions)
但是要嵌套的循环,就必须使用block,所以还是建议尽量少使用嵌套。实际上对于模板来说,能输出变量已经能解决大多数的情况了,特别是要求性能(速度)的应用中,尽量少使用嵌套的循环。



阅读(1521) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~