2011-11-05 21 views
1

基本上我想創造的東西,讓我做這件事:如何在PHP中創建分段的foreach()函數?

<?php 
function herp(){foreach($users as $user):} 
function derp(){endforeach;} 
?> 


<html><body> 
<?php herp();?> 

<?php $user->name();?>,<?php $user->name();?>,<?php $user->name();?> 

<?php derp();?> 
</body></html> 



--------- output --------- 

John Doe, 1900, Buttery Yellow 
Jane Doe, 1900, Platypus Brown 

我不想創建或依賴模板引擎。我只是想讓它創建頁面模板的人可以爲他提供循環。

+1

'make'絕對不是一個恰當的標籤! –

+0

結帳[小鬍子](http://mustache.github.com/) - https://github.com/bobthecow/mustache.php – hakre

+0

你不能像這樣分割出一個'foreach','endforeach'就是預計將在相同的範圍內。 – CodeCaster

回答

0

這是可能的,實際上很受歡迎。您需要使用templating engine

它的功能基本上允許您將編程從設計中分離出來。在這種情況下,來自HTML的PHP​​。如果你想要一個簡單的,我建議Rain。來自網站的示例:

//PHP 
include "inc/rain.tpl.class.php"; //include Rain TPL 
raintpl::$tpl_dir = "tpl/"; // template directory 
raintpl::$cache_dir = "tmp/"; // cache directory 

<!--HTML--> 
My age is {$age} // 30 

and also use = for assign a value to a variable 

{$year=30} 
My age is {$age+$year} // 60 

You can use functions with variables 

My name is {$name|substr:0,3} // Rai 
+1

我只是喜歡人們如何在PHP之上創建模板引擎,這是一個開始的模板。喲dawg ... –

+0

@ MarcB我同意這一點,但你不能否認他們是有用的hommie。 – amosrivera

+0

我不想使用已經建立的模板引擎。我只是想知道如何去分割一個循環,以便循環的開始被一個var或函數調用,並且循環的結尾被調用相同的方式。 – RTHNBrakes