让php脱离身体

时间:2013-02-09 23:39:32

标签: php html

我已经阅读并且听说过尽可能地分离PHP和HTML标签是非常明智的。现在我的问题是我应该怎么做?我真的找不到答案所以我决定问你。 我正在考虑使用POST功能。

查看我的代码:

<div class="aftelklok rondenSB">
    <a href="?page=agenda">
    <?php 
    // countdown function
    // parameters: (year, month, day, hour, minute, seconds)
    countdown(2013,2,24,18,0,0);
    function countdown($year, $month, $day, $hour, $minute, $seconds)
    {
        // make a unix timestamp for the given date
        $the_countdown_date = mktime($hour, $minute, $seconds, $month, $day, $year, -1);

        // get current unix timestamp
        $today = time();

        $difference = $the_countdown_date - $today;
        if ($difference < 0) $difference = 0;

        $days_left = floor($difference/60/60/24);
        $hours_left = floor(($difference - $days_left*60*60*24)/60/60);
        $minutes_left = floor(($difference - $days_left*60*60*24 - $hours_left*60*60)/60);
        $seconds_left = floor($difference - $days_left*60*60*24 - $hours_left*60*60 - $minutes_left*60);
        if($difference > 0){ echo "Nog ".$days_left." dagen ".$hours_left." uur               .$minutes_left." minuten  tot de dienst"; }
        else{ echo "De vorige dienst is afgelopen."; } 
    }
    ?> 
    </a>
</div>

所以我想要的只是回声但是然后让所有的PHP代码不在我的div中但在html代码之上。像:

<?php ..... ?>
<html>
<body>
echo
</body>
</html>

7 个答案:

答案 0 :(得分:1)

正如pemeon所说, Smarty 是一种非常聪明(双关语)的方法。

如果您想了解有关背景的更多信息,可能需要谷歌搜索“模型 - 视图 - 控制器在php中”或类似的东西。基本上,它是将您的视图(所有演示文稿内容,例如HTML)与代码逻辑(控制器)和数据对象/源(模型)分开。

Smarty 很不错,但您需要花一些时间来弄清楚模板引擎的设计方式,如何使用它以及如何将其应用于您的特定挑战。

如果你现在不想要这么大的解决方案并想要开始变得更小更容易,你可以在函数file_get_contents(...)和{{1}周围编写自己非常简单的模板“引擎” }。这个想法看起来像这样:你把你的HTML东西放在模板文件中(例如* .html或* .tpl文件结尾),它们不包含任何PHP代码,但是用于动态创建内容的持有者:

示例: main-layout.tpl

str_ireplace

示例: welcome.tpl

<html>
<head><title>${Title}</title></head>
<body>
<img src="yourfancylogo.png" alt="Header logo"><br>
<a href="#">Here some navigation</a> | <a href="#">...</a> | ... <br>
${Content}
<hr>
<div id="footer">&copy; 2013 John Doe - <a href="index.php?requestedpage=contact">Contact us</a></div>
</body>
</html>

示例: tos-document.txt

<h1>Hello, ${Username}! Nice to see you!</h1>
<p>So your username is ${Username}? Then you might want to read our terms of service before starting to use our app:</p>
<pre>${TOS}</pre>

在你的php脚本中,你可以这样做:

1) An apple a day keeps the doctor away!
2) No Smoking!
3) ...

使用模板和要插入的数据的这种分离,您可以稍后修改布局/模板,而无需触摸您的PHP脚本。例如,如果要修改所有页面上显示的页眉或页脚,则可以使用单点更改,因为您可以从多个模板砖中模块化地组装您的站点。

为了保持php源的可读性,当你的源变得越来越大时,你可以把所有的处理程序代码放到单独的php文件中。您可以将<?php $template = file_get_contents('main-layout.tpl'); if (isset($_GET['requestedpage'])) { // Parameter given! $requestedPage = $_GET['requestedpage']; } else { // No page parameter. Assume "home". $requestedPage = "home"; } $username = "Monty"; // get from session data if ($requestedPage == 'home') { // -- begin handler code for page "home" -- $title = "Start Page - Welcome"; $content = file_get_contents('welcome.tpl'); $tos = file_get_contents('tos-document.txt'); $content = str_ireplace('${TOS}', $tos, $content); // -- end handler code for page "home" -- } else if ($requestedPage == 'aboutus') { ... } else { $title = "Page Not Found - Error"; $content = file_get_contents('error404.tpl'); $content = str_ireplace('${PageThatWasNotFound}', htmlentities($requestedPage), $content); } $output = str_ireplace('${Content}', $content, $template); $output = str_ireplace('${Title}', htmlentities($title), $output); $output = str_ireplace('${Username}', htmlentities($username), $output); die($output); ?> include添加到主要源文件中。

但请注意:您必须逃避可能来自用户输入的所有占位符值 - 无论是从数据库获取还是直接来自require$_GET( - &gt; XSS漏洞) )。 所有输入都是邪恶的!

答案 1 :(得分:0)

您可以将php放在单独的文件中,然后使用includerequire

do_stuff.php:

<?php
  // some calculations
  echo "stuff";
?>

然后在你的HTML中......

index.php:

<html>
<body>
<?php include 'do_stuff.php'; ?>
</body>
</html>

About include

答案 2 :(得分:0)

最好的方法是使用某些模板引擎,例如smartytwig。如果你没有时间学习,你现在可以只在顶部编写代码,当你想使用某些计算时,即使用。

<?php
    // calculations
    $foo = 'foo';
?>
<html>
<body>
<div><?=$foo?></div>
</body>
</html>

当你开始使用这种'分离'(文件顶部的php代码)和底部的html,你将使用这个快捷版的echo函数,你将很容易转换为使用模板系统

答案 3 :(得分:0)

您不能在PHP块之外使用php函数或变量。但是,可以在php variable中存储一个值,然后在一个很小的PHP块中使用它。

例如:

<?php
// ...
$foo = 42;
// ...
?>

然后

<html><body>
    <p>Answer is <?php echo $foo; ?></p>
</body></html>

<html><body>
    <p>Answer is <?= $foo; ?></p>
</body></html>

答案 4 :(得分:0)

目标不应该是php远离html代码,目标应该是保持业务逻辑来自表示逻辑。其中一种方法是使用模型 - 视图 - 控制器布局,或者您可以使用许多其他范例中的一种。这种方法的要点是改变一个部分独立于另一部分:想象创建一个单独的移动或json前端,它完成所有完全相同的逻辑,但输出完全不同。如果业务逻辑和表示逻辑完全纠缠在一起,那么您将很难复制或公然复制代码,以及创建需要保持同步的两个不同分支,这使得维护它成为一场噩梦。

答案 5 :(得分:0)

我现在正在使用这种框架,它有点像MrSnurb所做的框架,但后来用HTML而不是php。你对这个框架人有什么看法?

<?php
if(isset($_GET['page'])) {
    $page = $_GET['page'];
}else{
    $page = "home";
}   
?>
<!DOCTYPE html>
<html>
<head>
<title> Younited - <?php echo $page; ?></title>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="css/style.css">
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script src="js/slides.min.jquery.js"></script>
</head>
 <body>
<div class="container ronden">  
    <?php 
        include "partials/header.php";
        include "partials/menu.php"; 
    ?>
<div class="content">   
    <?php
        if (file_exists("partials/pages/".$page.".php")) {
            include "partials/pages/".$page.".php";
        }   
        else{ echo "<p>De pagina <b>".$page."</b> bestaat niet.</p>"; 
        } 
        include "partials/sidebar.php"; 
    ?>  
</div>
</div>
    <?php
        include "partials/footer.php";  
    ?>
</body>
</html>

答案 6 :(得分:-1)

使用smarty之类的模板引擎。要创建html代码的模板并推送varibles。