如何在一个文件中编写php + html更漂亮?

时间:2010-05-08 22:14:53

标签: php html

<?
if($id == 2) {
?>
       html goes here
<?
} 
else { 
?>
       if id is not 2 then something goes here
<?
}
?>

如何在不懂智能的情况下将php + html写在一个更漂亮的文件中?

12 个答案:

答案 0 :(得分:23)

您可以使用PHP's alternative syntax for control structures

<? if ($id == 2): ?>
    <h2>some html</h2>
<? else: ?>
    <h2>another html chunk</h2>
<? endif ?>

请注意,short_open_tags是一个需要显式启用的php.ini指令,否则每次都必须编写<?php

答案 1 :(得分:5)

您可以实现MVC(Model,View,Controler)设计模式,您的代码可以用作加载视图的模型:

<?php
if($id == 2) 
{
 //somes variable and array controls
}
else  
{
 //somes variable and array controls
}
//include the view
include('view.php');
?>

在view.php中,显示的html页面只包含用于php变量和数组的回声。

答案 2 :(得分:4)

为了更好的未来(设计)可维护性,我建议这样的事情:

<?php
if ($id == 2){
$someText = 'I like your id!';
}else{
$someText = 'I love your id!';
}
?>
<html>
...complex html...
<p><?php echo $someText ?></p> <!-- or <?= $someText ?> -->
...complex html...
</html>

答案 3 :(得分:2)

我推荐HEREDOC获取一些HTML代码。

像这样:

<?php
if($id==2)
 echo <<<EOT
Blah
EOT;
else
 echo <<<EOT
Your id is <b>{$id}</b>
EOT;
?>

此外,您可以尝试一种方法,其中有两个文件:php文件和phtml文件。 php文件是主要逻辑,然后phtml文件是html输出。

就像你的例子一样,

// logic.php file
if($id==2)
 $sOutput = 'Yes';
else
 $sOutput = 'No';
// somewhere at the end of the file
include 'logic.phtml';

// logic.phtml
<html>
<title><?=$sOutput?></title>
<body>
Blah blah blah <?=$sOutput?> you can login
</body>
</html>

答案 4 :(得分:2)

正如许多其他人所建议的,你可以使用MVC,或者如果你不想实现严格的MVC结构,你应该使用模板系统。

这并不意味着你必须学会​​聪明,你可以编写自己的模板系统,只需要你真正需要的功能。

如果您正在与设计师合作并且性能不是您的第一点,您可以使用简单的占位符构建一个html文件,其中动态内容必须去,然后用php替换它(str_replace,preg_replace)..但这会慢一点你的申请。

示例:

//html template
// the @[var] syntax is just as an example
<title>@[title]</title>
<body>
    @[content]
</body>

和你的模板文件:

$title = 'Hello world';
if($id == 2){
    $content = get_content();
}else{
    $content = get_another_content();
}
//really, this is just as example ;)
ob_start();
include('template.html');
$output = ob_get_clean();
echo str_replace(
    array('@[title]', '@[content]'),
    array($title, $content),
    $output
);

这是一个基本的例子,有两个问题:

  1. 性能;
  2. 必须指示设计师不要触摸占位符。
  3. 更简单的解决方案可以是:

    //html template
    <title><?php echo $title; ?></title>
    <body>
        <?php echo $content; ?>
    </body>
    

    和php:

    $title = 'Hello world';
    if($id == 2){
        $content = get_content();
    }else{
        $content = get_another_content();
    }
    include('template.php');
    

    但应尽可能减少回应html,这不是一个好的做法,它将逻辑与内容混合在一起 Logic is logic, data is data, and life is good

答案 5 :(得分:1)

我喜欢并使用 soulmerge 的方法。但退出另一个:

<?php

if ($id == 2)
echo <<< END
    <h2>some html</h2>
END;
else
echo <<< END
    <h2>another html chunk</h2>
END;

?>

答案 6 :(得分:0)

你可以这样做......

<?
$html_1 = <<<EOH
my html goes here
EOH;

$html_2 = <<<EOH
my html <i>something else</i> html goes here
EOH;

echo ($id==2?$html_1:$html_2);
?>

Buuut,如果你想让你的设计师更容易,只需使用外部文件:

<?
$html=($id==2 ? file_get_contents("html_1.html") : file_get_contents("html_2.html") );
$html=str_replace("%php generated content%", 1 + 1, $html );
echo $html;
?>

并使用预定义的关键字插入php计算的内容(在此示例中,%php生成的内容%被替换为2(1 + 1))

答案 7 :(得分:0)

我个人不喜欢使用短标签,因为我发现它们在文件中不一致,而且更喜欢坚持使用一种方法。通过一些简单的更改,您可以立即使其看起来更具可读性:

<? if($id == 2) {    ?>

       <p>html goes here</p>

<? } else       {    ?>

       <span>if id is not 2 then something goes here</span>

<? }                 ?>

比其他一些答案简单一些,但也是我觉得最可读的方式。这是关于你自己的偏好,虽然已经提出了许多其他好的方法(这就是我的方法)。

答案 8 :(得分:0)

添加DaNieL的答案,您可以执行以下操作:

function htmlVars($html, $vars){
    $html = preg_replace_callback('/@\[([0-9a-zA-Z_\$]*)\]/',function ($matches) use ($vars){
  return isset($vars[$matches[1]])?$vars[$matches[1]]:"";
 },$html);
 return $html;
}

示例:

echo htmlVars('foo is @[foo] and bar is not @[bar]',
    Array('foo'=>'foo','bar'=>'barz'));

结果:

foo is foo and bar is not barz

匿名函数仅适用于php 5.3或更高版本

答案 9 :(得分:0)

真的有两个案例:

1)模板代码。我建议使用soulmerge的方法来控制结构的替代语法。

2)简短的视图助手PHP代码,它生成输出(通常用于以后替换),不一定值得它自己的子模板。

2我更喜欢这样的东西:

public function draw(){
    $html = array();
    foreach($this->item as $item){
       $html[] = <<<EOD 
<li class="{$item->class()}">{$item->label()}</li>
EOD;
    }
    return implode($html);
}

我比任何可能涉及ob_start()?> <?php块的不稳定替代品更喜欢这个。您总是希望函数返回字符串,而不是直接回显输出,以便被调用者可以选择连接和操作结果,而不会在被调用者上传递输出缓冲的负担。

heredoc可以很容易地识别字符串输出,即使我认为缩进的破坏有点难看,但最终会非常有效,因为你的html输出就像小粘滞便笺一样突出。那个,以及在我的IDE(PDT)中它以绿色突出显示的事实。 (这里没有在SO荧光笔中看到......)因此它总是明确地说明了我所处的“模式”。

相比之下,?> <?php技术使得很难区分html和php代码。一旦你做了几次,跟踪你是否需要?><?php会很烦人。最后,使用<?php echo $obj->func() ?> vs {$obj->func()}进行替换通常会更加冗长

如果您的代码变得更复杂,那么可能是重构的时候了。我喜欢heredoc,因为如果我正在做一些特别的事情,它会脱颖而出并在几个月后变得非常明显(特别是当你增加要求时,ad-hoc倾向于退化为泥球)当我得到40行heredoc,可能已经开始为5.当我回来查看我的代码并开发非完全临时版本时,识别文件中我不好的部分要容易得多。这可以追溯到粘滞便笺,绿色突出显示我对缩进的评论。

答案 10 :(得分:-2)

By using this - PHP回声。如果这就是你想要的,那就是 - 如果你输出大量的HTML,继续你正在做的事情可能会更具可读性。

答案 11 :(得分:-2)

 <?php

if($id == 2) {
  //echo all your html stuff
  echo "<title>Title is where id is 2</title>";
  echo "<h1>Hellow</h1>";

}
else {

  echo "<title>Title is where id isn't 2</title>";

}

?>