You are here

drupal主题编码习惯

g089h515r806 的头像
Submitted by g089h515r806 on 星期日, 2008-08-31 13:27

主题作者应该仔细的编写干净、结构良好的代码,这和其它项目上的程序员一样。这样做,可以使代码更容易阅读、理解和维护。虽然不同的组织有着不同的习惯,但是最好大家都遵从Drupal标准,这样有利于协作工作或者需求帮助。

  • 缩进采用2个空格;而不是一个tab键
  • HTML标签的开始和结束部分的缩进一定要匹配。
  • PHP 和HTML的缩进要区分开来

不是这样:

  ...
  <?php if ($header): ?>
  <div id="header">
    <?php print $header; ?>
  </div>
  <?php endif; ?>
  ...
   

应该这样:

  ...
<?php if ($header): ?>
  <div id="header">
    <?php print $header; ?>
  </div>
<?php endif; ?>
  ...
   

这样,良好的缩进,使得一眼就能够找到匹配的开始和结束标签了。

 

  • 最好在HTML中使用PHP,而不是在PHP中使用HTML。例如,

不应这样:

<?php
if (!$page) {
  print "<h2><a href=\"$node_url\" title=\"$title\">$title</a></h2>";
}

if ($submitted) {
  print "<span class=\"submitted\">$submitted</span>";
}
?>

应该这样:

<?php if (!$page): ?>
  <h2><a href="<?php print $node_url ?>" title="<?php print $title ?>"><?php print $title ?></a></h2>
<?php endif; ?>

<?php if ($submitted): ?>
  <span class="submitted"><?php print $submitted ?></span>
<?php endif; ?>
   

毕竟,PHP是一个嵌入到HTML中的脚本语言-----而不是其它方式(HTML嵌入到PHP中)。

 

原文:http://drupal.org/node/1965

 译者:葛红儒, Think in Drupal,

Drupal版本: