在Smarty上使用多页的最佳方式

时间:2013-02-17 03:12:25

标签: php smarty

这是将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);

如果我有“登录”和“用户区”以及所有内容怎么办?我怎样才能让他们各自干净利落地完成自己的职能呢?

3 个答案:

答案 0 :(得分:3)

是,

也许您可以将$ page变量更新为以下内容:

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

但是你用前控制器改变页面的方式是好方法。你可以做一些升级...我的工作流程;

  1. 显示index.html文件,并在index.htm文件中加载前控制器中的其他TPL / HTML文件。
  2. 类似的东西:

    $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;
    
            case default:
                            $content = "blalala";
                            break;
    
    
    
    
    $smarty->assign('page',$page);
    $smarty->assign('content',$content);
    $smarty->display('index.htm');
    

答案 1 :(得分:0)

这是我使用Smarty Templating的主要Index.php。在此页面中,我在管理面板中激活时包含一个JQuery登录小部件。 $ sel是你的$ page。

它通过一个Switch,我为索引页面添加了更多的视图,例如那些通过谷歌广告到达那里的人的宣传视图。所以宣传可以链接到?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函数 - 请注意它使用fetch而不是display。

/**
 * 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");
}

答案 2 :(得分:0)

我最好的选择:

<?php
   $pages = array(
        "home" => array("home.tpl", " - Home"),
        "contact" => array("contact.tpl", " - Contact us"),
        "verify" => array("verity.tpl"), " - Verify your account"),
        "e404"   => array("404.tpl", " - Page not fount")
    );

    $pag_selected = $pages["e404"];
    if(isset($_GET["page"]) && isset($pages[$_GET["page"]])):
        $pag_selected = $pages[$_GET["page"]];
    endif;
    $smarty->assign('pagename', $pag_selected[1]);
    $smarty->display($pag_selected[0]);
?>