PHP内容分离 [英] PHP Content Separation

查看:85
本文介绍了PHP内容分离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我们都知道,不仅在PHP中,您应该始终将content / design / html中的代码分开。 (我看到有人说今天在这里相反)



我的意思是,你不希望其中一个在更大的项目中,是吗? b
$ b

 <?php 

echo'< div id =blah>< b>'
。 $用户名。 '< / b>'
。 $东西。 '<与PHP混合的更多HTML ...>';

?>

但是:将代码与内容分开的好方法是什么?



我一直使用一个简单的模板系统来取代模板中的通配符。



 < div id =blah>< b>%USERNAME%< / b>%STUFF%< ... > 

(位于单个文件中)

稍后,您可以调用类似于的函数GetTemplate('myTemplate',array('USERNAME'=>'陌生人'));



最后,问题:


  • 这是分隔代码的好方法和内容?

  • 如何在不使用框架的情况下做到这一点?

  • 有没有更好的方法?


解决方案

我不喜欢额外的模板语言(替换通配符),而是我喜欢保持我的模板纯PHP,所以我的vertion您所做的是:

< div id =blah>< b><?php echo $ username ?>< / b><?php echo $ stuff?>< ...>



echo GetTemplate('myTemplate.php',array('username'=>'Stranger','stuff'=>'Stuff'));

 函数GetTemplate($ templatePath,array $ vars = array())
{
extract($ vars);
ob_start();
include($ templatePath);
return ob_get_clean();
}

我也将其与helper函数/对象 - >方法结合起来:



<?php echo link_to($ name,$ url); ($ name,$ url,array $ attributes = array()函数link_to($ name,$ url,array $ attributes = array())

 
{
$ attributes ['href'] = urlencode($ url);
foreach($ attributes as $ attrib => $ value)
{
$ attributes [$ attrib] = $ attrib。= \$ value \;
}
返回sprintf('< a%s>%s< / a>',implode(,$ attributes),$ name);
}

我通常将这些助手应用于常用的标签/结构,一个类似于 content_tag($ tag,$ content,$ attributes)的通用html标签; 这可以帮助我避免随机标签的大量属性的php回显。我明显不使用它的每个html标签,我只用于那些使得可读性比一吨回声更好的标签。



就模板引擎而言,我不会真的看到好处,因为php是模板语言。就非程序员/编码人员而言,他们必须依赖循环和变量的语法,所以我没有看到如何将其更改为通配符或smarty的语法添加任何内容。

So we all know that you should always, not only in PHP, separate code from content/design/html. (I have seen people that say the opposite here today)

I mean, you don't want one of these in bigger projects, do you?

<?php

echo '<div id="blah"><b>'
     . $username . '</b>'
     . $stuff . '<more HTML mixed with PHP...>';

?>

But: What is a good approach to separate code from content?

I have been using a simple template system that replaces wildcards in templates mostly.

e.g:

<div id="blah"><b>%USERNAME%</b>%STUFF% <...>

(located in a single file)

Later you can just call a function similar to GetTemplate( 'myTemplate', array ( 'USERNAME' => 'Stranger' ) );

Finally, the questions:

  • Is this a good way of separating code and content?
  • How do you do that when not working with a framework?
  • Is there a better way?

解决方案

Im not fan ov additional templating languages (that replace wildcards) instead i like to keep my templates pure php so my vertion of what you have done would be:

<div id="blah"><b><?php echo $username ?></b><?php echo $stuff ?><...>

echo GetTemplate( 'myTemplate.php', array ( 'username' => 'Stranger', 'stuff' => 'Stuff' ) );

function GetTemplate($templatePath, array $vars = array())
{
  extract($vars);
  ob_start();
  include($templatePath);
  return ob_get_clean();
}

I alos combine this with helper functions/object->methods as well for example:

<?php echo link_to($name, $url); ?>

function link_to($name, $url, array $attributes = array())
{
  $attributes['href'] = urlencode($url);
  foreach($attributes as $attrib => $value)
  {
     $attributes[$attrib] = $attrib."=\"$value\"";
  }
  return sprintf('<a %s>%s</a>', implode(" ",$attributes), $name);
}

I generally apply helpers like these to commonly used tags/structures as well as having a general purpose html tag one that looks something like content_tag($tag, $content, $attributes); This helps me avoid a php echo for tons of attributes for random tags. I obviously dont use it for every html tag i use only for ones where it makes for better readability than a ton of echos.

As far as templating engines go i dont really see the benefit as php is templating language. As far as non-programmers/coders they have to leanr the syntax for a loop and a variable any how so i dont see how changing it to wildcards or the {} syntax of smarty adds anything.

这篇关于PHP内容分离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆