9

Is this the most effective way to use smarty with multiple pages?:

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);

What if I have "log-in" and "user area" and everything? How can I make them each do their own functions cleanly?

Linus Unnebäck
  • 23,234
  • 15
  • 74
  • 89
  • 1
    Smarty is just a templating engine, it doesn't care 'how' or 'why' the page is loaded. What you're doing is making a front end controller. Usually what people will do is route a request (uri) to a specific controller->method rather than directly load the templates like you are currently doing. That being said, there is nothing wrong with the way you are doing it. – Supericy Feb 17 '13 at 03:14
  • Its good, keep it like this. – sofl Feb 18 '13 at 10:10

3 Answers3

3

Yes,

Maybe you could update the $page variable to the following:

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

But the way you are changing pages with a frontcontroller is the good way. You can do some upgrading... My workflow;

  1. Display index.html file and load in the frontcontroller other TPL/HTML files in that index.htm file.

Something like:

$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');
Linus Unnebäck
  • 23,234
  • 15
  • 74
  • 89
Duikboot
  • 1,093
  • 1
  • 12
  • 24
0

This is my main Index.php using Smarty Templating. In this page I include a JQuery login widget when activated in admin panel. $sel is your $page.

It goes through a Switch incase I add more views for the index page, for example a publicity view for those who get there though a google advert. So the publicity can link to ?sel=googlead1 and I can display a page based on that.

I call my authentification class and load the user (method called refreshes his presence on site so its not useless)

Then I load the selected page through a function call. After that I exit code execution.

In the function, I call a shared widget for several pages which allows the user to log in through a JQuery panel. That gets the page.

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;
}

In the actual index_page.tpl I load the widget like so:

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

Hope this helps show another way to organize your code with Smarty (which is really awesome in my opinion)

Edit: Here is the shared getAuthWidget function - notice it uses fetch instead of 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");
}
Jack M.
  • 1,195
  • 13
  • 30
0

My best option:

<?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]);
?>
Darwin
  • 62
  • 2
  • 8
  • Using multi-dimensional arrays whose contents have different meanings... This begs a strongly-typed class. (`Template`, `TemplateInfo`, etc.) – EthanB Feb 27 '14 at 01:57