开发者

PHP functions vs includes for site template

开发者 https://www.devze.com 2022-12-24 17:01 出处:网络
What is the best, easiest, and fastest way to use a template for a page. Right now I have been using a function to create my template for each page.

What is the best, easiest, and fastest way to use a template for a page. Right now I have been using a function to create my template for each page.

$title = "Kick Me";
<?php pageHtmlHeadStart($title)>
    <!-- Were Javascript would go -->
<?php pageHtmlHeadEnd(); guiHeader(); ?>

    Content went here.

<?php guiFooter; ?>

I uses to use includes...

<?php include('header.php'); ?>

    Content went here.

<?php include('footer.php') ?>

I was thinking about using PHP Object to do the same thing too. It would run like the functions way of doing it. I would create a GUI class that would have the template wrapper and include some scripts to display content (via echo) that I use a few time over again on开发者_运维知识库 different page.

I probably will answer my own questions when say this... I like the current way do it with the functions. I just don't know if this is bad coding habits.


In the context of templates, there is nothing wrong to use include() statements. They make clean looking code and makes it easy to share html snippets between templates.

Whether to use instance methods, static methods or functions for "template tags" depends on the architecture of your framework or application. While generally OOP is really good thing, making everything an object doesn't magically make your code better.

PHP itself does make a good template language. There is no need to invent another "language" for templates – neither is it wrong, as long as you cache your parsed files (like Smarty does).

You may want to check out Savant and Smarty template engines. I also recommend you to study various PHP MVC frameworks and see how things are implemented there: Symfony, Kohana.


If you want a template, then use a templating language. Using PHP for templating is definitely not a good practice according to me.

0

精彩评论

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