0

敬意を表して、

主に静的コンテンツを配信する Web サイトで使用するために、PHP 内で HTML マークアップを整理する方法を探しています (読み取り: .edu Web サイトの場合)。(これがこの質問の主な目的ですが、セッションやユーザーログインイベントなどを処理する必要がすぐに「必要になる」可能性があるため、静的でないケースにも間違いなく興味があります)

PHP (および Coldfusion、.NET などの他の言語) でよく使用されるパターンは次のとおりです。

// define header stub in header.php
<!DOCTYPE html>
<html>
    <body>
        <div id="headerWrapper">
            <!-- header content goes here -->
        </div>
        <div id="contentWrapper">
// end header stub

// define footer stub in footer.php
        </div><!-- end contentWrapper -->
        <div id="footer">
            <!-- footer content goes here -->
        </div>
    </body>
</html>
// end footer stub

このパターンを使用するには、次のようにコンテンツ ファイルを定義します。

// define content stub in contentFile.php
<?php include("header.php") ?>
            <p>hello world</p>
            <!-- other content goes here -->
<?php include("footer.php") ?>
// end content stub

もっと探しています。上記の例で私が抱えている問題は、一般的なパターンで必要な結合のレベルです。つまり、たとえば、header.php と footer.php の間にはいくつかの重要な依存関係があります (つまり、ページ構造、ぶら下がっている contentWrapper div タグ、および下位レベルでは、すべてのページで必要とされない避けられない css および js インクルード)。 . 特にカプセル化を探しています。私の理想的なコンテンツ ページは次のようになります。

    // ideal.php
    <?php
        $someObj.pageTitle="My First Page!!!";
        $someObj.headInclude("/css/960.css");
        $someObj.headInclude("/js/myFancyThing.js");
        $someObj.preferedTemplate = "default"; /* or maybe "MathDept" or something like that */
    ?>
    <!-- page content goes here, just semantic and structural markup, let the 
template handle H1 and Paragraph formatting :) -->
    // end ideal stub

この最後のパターンは、私が非常に見たいものです。私には、プレゼンテーションからコンテンツを分離するためのはるかにエレガントなソリューションのように思えます。PHPでこれを明確にする方法はわかりませんが、実現可能だと思います。どんな助けでも大歓迎です!ありがとうございました!!!:)

/* 私が質問した内容が適切にスコープされ、タイトルが付けられていることを願っています。それ以外の場合はご容赦ください。私は、Stack Exchange、PHP、およびほとんどすべてのことについて、まだ少しグリーンです笑 */

4

1 に答える 1

0

この種の (優れた) 考え方とアプローチを使用すると、すぐに MVC 設計パターンを再発明していることに気付くでしょう。プレゼンテーション層に対する Zend Framework のアプローチを確認することを強くお勧めします。2 段階の設計 (レイアウト + ビュー) を使用します。パターンの単純な例を次に示します。

コントローラ:

public function someAction()
{
    // Add the CSS files.
    $this->view->headLink()
        ->appendStylesheet($this->view->baseUrl() . '/css/site.css')
        ->appendStylesheet($this->view->baseUrl() . '/css/ie6.css', 'screen', 'IE 6')
        ->appendStylesheet($this->view->baseUrl() . '/js/jquery/plugins/ui/css/ui-lightness/jquery-ui-1.8.18.custom.css', 'screen')

    // Add the JavaScript files.
    $this->view->headScript()
        ->appendFile($this->view->baseUrl() . '/js/jquery/jquery-1.9.1.min.js')
        ->appendFile($this->view->baseUrl() . '/js/jquery/plugins/ui/jquery-ui-1.8.17.custom.min.js')
        ->appendFile($this->view->baseUrl() . '/js/modernizr.js');

    // Add the meta tags.
    $this->view->headMeta()
        ->appendHttpEquiv('X-UA-Compatible', 'IE=edge')
        ->appendName('description', '...')
        ->appendName('keywords', '...');

    // Render the View.
    $this->view->render();
}

レイアウト:

<?php
$this->headMeta()->appendHttpEquiv('Content-Type', 'text/html;charset=utf-8');
echo $this->doctype(); ?>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <?php echo $this->headMeta(); ?>
    <?php echo $this->headTitle(); ?>
    <?php echo $this->headLink();?>
    <?php echo $this->headScript();?>
</head>
<body>


    <div id="wrapper">

        <?php echo $this->partial('partials/header.phtml');?>

        <?php echo $this->layout()->content;?>

        <?php echo $this->partial('partials/footer.phtml');?>

    </div>

</body>
</html>

意見:

<h1>This View script is rendered by the View and will get injected into the layout.</h1>
<div>Page specific markup goes in this file</div>

この道を進むことにした場合は、自分でフレームワーク コードを記述する前によく考えてください。

リンク: http://framework.zend.com/manual/2.0/en/modules/zend.view.quick-start.html

于 2013-04-04T12:06:04.897 に答える