简体   繁体   English

什么是使用php创建模板的最佳方法?

[英]Whats the best way to create templates using php?

I have a class that gathers templates and displays the final output after connecting all the templates. 我有一个收集模板并在连接所有模板后显示最终输出的类。

class Template{

$private $output = '';

public function Load_Template($template, $data = null){
    ob_start();
    include($template);
    $this->output .= ob_get_clean();
}

public function Display($add_footer = true){
    echo $this->output;
}

}

Now, Currently my templates look something like this. 现在,目前我的模板看起来像这样。

<h1><?php echo $data['name']; ?></h1>

or the more complex ones that involve loops look more like 或更复杂的涉及循环的看起来更像

<ul>
  <li>
  <?php foreach($data as $user){ ?>
   <h1><?php echo $user['name']; ?></h1>
  <?php } ?>
  </li>
</ul>

Actually theres way more data than that in them, but im sure you guys get the point. 实际上,那里的数据比它们中的要多,但我肯定你们明白了。 Now, I have heard people say thats it better to have templates like this 现在,我听说人们说拥有这样的模板会更好

<h1>{name}</h1>

or 要么

<ul>
  <li>
   <h1>{name}</h1>
  </li>
</ul>

and then use a str_replace function... Now, if im using a foreach loop, how would I accomplish something like this... should i alter my class, and if so can i get some ideas as to how... And do you guys suggest using templates with 然后使用str_replace函数...现在,如果即时通讯使用foreach循环,我将如何完成类似的工作...我应该改变班级吗?如果可以,我可以就如何实现获得一些想法...你们建议与

对于这些“ {tags}”类型模板,您有一个非常流行的引擎: Smarty

Smarty is too redundant! 聪明人太多余了! Your class with the function of cutting a page is enough. 具有剪切页面功能的课程就足够了。

Every template language has its own way of approaching the problem. 每种模板语言都有其解决问题的方式。 You could, instead of writing your own template language from scratch, use one of the standard ones out there like Smarty. 您可以使用Smarty之类的标准语言,而不必从头开始编写自己的模板语言。

If you are writing your own, you'll need at a minimum a construct that will let you express loops and conditionals. 如果您要编写自己的代码,则至少需要一个可让您表达循环和条件的构造。 One simple approach that I've seen used before is something like: 我以前使用过的一种简单方法是:

<!-- BEGIN conditional_or_looped_block_named_foo -->
Stuff that may appear zero or more times
<!-- END conditional_or_looped_block_named_foo -->

The syntax varies widely, of course, from one language to another, but the basic approach is the same: have some markup that surrounds the portion of code you want to isolate and repeat (or omit). 当然,从一种语言到另一种语言,语法有很大的不同,但是基本方法是相同的:在您要隔离和重复(或省略)的代码部分周围加上一些标记。

This should give you an idea: http://www.handyphp.com/index.php/PHP-Resources/Handy-PHP-Tutorials/Creating-Your-First-Template-Driven-PHP-Website-Part-2.html 这应该给您一个想法: http : //www.handyphp.com/index.php/PHP-Resources/Handy-PHP-Tutorials/Creating-Your-First-Template-Driven-PHP-Website-Part-2.html

I found it by doing a quick search, it has code to do what you are asking, it does search and replace with simply variables that you have created. 我通过快速搜索找到了它,它具有执行您所要求的代码的功能,它可以搜索并替换为您创建的简单变量。

So your foreach() call would not echo but store in a variable, then you replace that variable with {users} in the template. 因此,您的foreach()调用不会回显,而是存储在变量中,然后在模板中用{users}替换该变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM