开发者

Simple Template Conversion

To help with the organisation and structure of my project, I would like to adopt some kind of template technique.

I have looked over quite a few examples, and would like to use only php to convert place holders in the template, to actual content.

The issue I have, is what happens when you wish to loop through information retrieved from a database, or if you only want to display certain things on the page if certain cond开发者_如何学Goitions are met (if statements)?

I’m still toying with the idea if this is the way to go, and would consider any views you may have.


Try taking a look at this page, makes for very neat php including (so to say) in html templates.

http://php.net/manual/en/control-structures.alternative-syntax.php

for instance:

<ul>
<?php foreach ($list as $item): ?>
 <li><?=$item?></li>
<?php endforeach; ?>
</ul>

if this is not what you mean, please elaborate.


PHP is a template engine itself so in my opinion an additional templating system is redundant. PHP provides all the tools for conditional output, loops, escaping, number formatting, etc and there's no reason to re-create all that.

It does make sense to split your templates into multiple PHP files for re-use. Keep your controller/model in a different file which does all the heavy work and includes the PHP templates. See the MVC pattern for more info.

I wouldn't recommend short tags like <?=$var?> because on most servers that option is disabled.

Hope that helps

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜