これは、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");
}