繁体   English   中英

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

[英]How to write php+html in one file more beautiful?

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

如何在不学习聪明的情况下将php + html写入一个更漂亮的文件中?

您可以对控制结构使用PHP的替代语法

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

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

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

<?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变量和数组的回显。

为了获得更好的未来(设计)可维护性,我建议使用以下方法:

<?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>

对于一些HTML代码,我建议使用HEREDOC。

像这样:

<?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>

正如许多其他建议一样,您可以使用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. 必须指示设计师不要触摸占位符。

一个更简单的解决方案可以是:

//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

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

<?php

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

?>

你可以这样

<?
$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)替换)

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

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

       <p>html goes here</p>

<? } else       {    ?>

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

<? }                 ?>

比其他答案要简单一些,但是我觉得最易读。 尽管已经建议了许多其他好的方法(这正是我的方法),但这实际上取决于您的喜好。

添加到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或更高版本

确实有两种情况:

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() ?>{$obj->func()}进行替换通常更冗长

如果您的代码变得更加复杂,那么可能是时候进行重构了。 我喜欢Heredoc,因为如果我做临时工作,它会脱颖而出,并且在几个月后变得很明显(当您添加需求时,临时工作会退化为泥泞球)。有40行Heredoc可能从5开始。当我返回查看代码并开发非完全临时版本时,识别文件中我不好的部分要容易得多。 这可以追溯到我对缩进所做的粘滞便笺和绿色突出显示的注释。

通过使用此 -PHP回声。 如果这就是您想要的,那就是-如果要输出大量HTML,则继续执行您的操作可能更具可读性。

 <?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>";

}

?> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM