9

これは、複数のページで smarty を使用する最も効果的な方法ですか?:

if (empty($_GET[page])) {
    $template = "home.tpl";
    $smarty->assign('pagename', ' - Home');
} else {
    $page = $_GET["page"];
    switch ($page) {
        case "home":
            $template = "home.tpl";
            $smarty->assign('pagename', ' - Home');
            break;

        case "contact":
            $template = "contact.tpl";
            $smarty->assign('pagename', ' - Contact us');
            break;

        case "verify":
            $template = "verify.tpl";
            $smarty->assign('pagename', ' - Verify your account');
            break;

        default:
            $template = "404.tpl";
            break;
    }
}

$smarty->assign('sitename', $sitename);
$smarty->display($template);

「ログイン」と「ユーザーエリア」とすべてを持っている場合はどうなりますか? それぞれが独自の機能をきれいに実行できるようにするにはどうすればよいですか?

4

3 に答える 3

3

はい、

$page 変数を次のように更新できます。

<?php
$page = isset($_GET['page']) ? $_GET['page'] : '';
?>

しかし、フロントコントローラーでページを変更する方法は良い方法です。いくつかのアップグレードを行うことができます...私のワークフロー。

  1. index.html ファイルを表示し、その index.htm ファイル内の他の TPL/HTML ファイルをフロントコントローラーにロードします。

何かのようなもの:

$content = "";
$page = isset($_GET['page']) ? $_GET['page'] : '';

// FRONTCONTROLLER
switch ($page) {
    case 'stack':
        require_once('includes/stack.php');
        $content = getContent();
        break;

    case 'overflow': 
        require_once('includes/overflow.php');
        $content = "overflow....";
        break;

    default:
        $content = "blalala";
        break;
}

$smarty->assign('page', $page);
$smarty->assign('content', $content);
$smarty->display('index.htm');
于 2013-04-04T07:19:24.433 に答える
0

これは、Smarty テンプレートを使用した私のメインの Index.php です。このページには、管理パネルでアクティブ化されたときに JQuery ログイン ウィジェットが含まれています。$sel はあなたの $page です。

インデックスページにさらにビューを追加した場合、スイッチを通過します。たとえば、Google 広告からそこにアクセスした人のための宣伝ビューです。したがって、宣伝は ?sel=googlead1 にリンクでき、それに基づいてページを表示できます。

私は自分の認証クラスを呼び出してユーザーをロードします (呼び出されたメソッドはサイト上の彼の存在を更新するので役に立ちません)

次に、選択したページを関数呼び出しで読み込みます。その後、コードの実行を終了します。

この関数では、ユーザーが JQuery パネルからログインできるようにする複数のページの共有ウィジェットを呼び出します。それはページを取得します。

include "./include/includes.php";

$sel=null;
if(isset($_POST["sel"]) or isset($_GET["sel"]) )
{
    $sel =isset($_POST["sel"])?$_POST["sel"]:$_GET["sel"];
}

$auth = new authentification($dbconn, "", "","");
$user = $auth->checkuser();

switch($sel){
    default:    IndexPage();
}
exit;

function IndexPage(){   
    global $smarty, $lang, $config;

    //load the text for the login
    $smarty->assign("text", $lang["basiclogin"]);

    if($config["auth_widget"] == "true")
    {
        $smarty->assign("auth_widget",getAuthWidget());
    }
    //display the whole index page
    $smarty->display($config["index_theme_path"]."/index_page.tpl");
    exit;
}

実際の index_page.tpl では、次のようにウィジェットをロードします。

{if isset($auth_widget)}
<div id="auth_widget" style="float:right;">
    {$auth_widget}
</div>
{/if}

これが、Smarty を使用してコードを整理する別の方法を示すのに役立つことを願っています (私の意見では、これは本当に素晴らしいことです)。

編集: これは共有 getAuthWidget 関数です。表示ではなくフェッチを使用していることに注意してください。

/**
 * Allows various pages to get the authentification widget if desired
 * @global Object $smarty
 * @global Array $lang
 * @global Array $config
 * @global Array $user
 * @return Page returns the fetched template widget
 */
function getAuthWidget($err = ""){
    global $smarty, $lang, $config, $user;

    $smarty->assign("text", $lang["basiclogin"]);
    //check if user is loaded, if not, throw error
    if(isset($user) && $user["id"] >= -1)
    {
        $smarty->assign("user", $user);
    }
    else 
    {
        echo "user not set";
        exit;
    }

    return $smarty->fetch($config["index_theme_path"]."/auth_widget.tpl");
}
于 2014-02-22T02:34:18.593 に答える