xajax与smarty整合一例(转自XAJAX.ORG)
时间:2007-06-29 来源:liuxingyuyuni
Hi
Some users asked for an example on how to use smarty with xajax and so I made a short one smile
This example shows how to display a small login screen including validation error-messages and how to display new contents based on smarty templates afterwards.
Basically, I always use 3 main php files called:
- index.php (displays the inital template including html header/footer and javascripts)
- index.common.php (including smarty+xajax and configs)
- index.server.php (xajax response functions)
All you need is xajax 0.5b2 and smarty installed
index.php
Code:
?
include("index.common.php");
// assign the xajax javascript function to smarty
$smarty->assign("xajax_javascript",$xajax->getJavascript('/xajax/'));
$smarty->display("index.tpl");
index.common.php
Code:
?
require_once ("xajax/xajax_core/xajax.inc.php");
require_once("smarty/Smarty.class.php");
session_start();
$smarty = new Smarty;
// put in your server configuration here
$smarty->cache="/srv/www/htdocs/smarty/cache";
$smarty->compile_dir="/srv/www/htdocs/smarty/compile";
$smarty->template_dir="/srv/www/htdocs/smarty/templates";
// turn caching off for dynamic pages and/or development
$smarty->force_compile=1;
$smarty->caching=0;
$xajax = new xajax("index.server.php");
$xajax->registerFunction("initLayout");
$xajax->registerFunction("login");
index.server.php
Code:
?
include("index.common.php");
function login($aFormValues) {
// new xajaxResponse Object
$objResponse = new xajaxResponse();
//clear error fields
$objResponse->assign("error_userpass","innerHTML"," ");
$objResponse->assign("error_username","innerHTML"," ");
//check for username
if ($aFormValues['username'] == "test") {
if ($aFormValues['userpass'] == "test") {
// login successfull. set your session variables here
$_SESSION['username'] = "tester";
$_SESSION['user_id'] = 1;
// load the basic layout
$objResponse->loadcommands(initLayout(array()));
} else {
// password error
$objResponse->assign("error_userpass","innerHTML","invalid password");
}
} else {
// username error
$objResponse->assign("error_username","innerHTML","unknown user");
}
return $objResponse;
}
function initLayout($aFormValues){
global $smarty;
$objResponse = new xajaxResponse();
// instead of display() we use fetch() to get the template output
$html = $smarty->fetch("layout.tpl");
// assign the html code
$objResponse->assign("layout","innerHTML",$html);
return $objResponse;
}
$xajax->processRequest();
?>
............................................................................................................................................................................................................
smarty templates
index.tpl
Code:
!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
html xmlns="http://www.w3.org/1999/xhtml">
head>
title>Smarty + XAJAX/title>
meta http-equiv="Content-Type" loginwindow="text/html; charset=uf8" />
style type="text/css"> */
-->/style>
{/literal}
{$xajax_javascript}
/head>
body>
div id="layout">
{include file="login.tpl"}
/div>
/body>
/html>
login.tpl
Code:
div id="loginwindow">
p align="right">
h1>Smarty & XAJAX/h1>
p>b>Please login with test:test/b>br />br />
/p>
div>
form id="loginform" action="" onsubmit="xajax_login(xajax.getFormValues('loginform'));return false;">
div class="formlabel">
username:
/div>
div class="formfield">
input type="text" name="username" id="username" />
/div>
div class="formerror" id="error_username"> /div>
br style="clear:both" />
div class="formlabel">
password:
/div>
div class="formfield" id="userpass">
input type="text" name="userpass" id="userpass" />
/div>
div class="formerror" id="error_userpass"> /div>
br style="clear:both" />
div class="formlabel">
/div>
div class="formfield">
input type="submit" value="login" />
/div>
br style="clear:both" />
/form>
/div>
/div>
layout.tpl
Code:
div style="float:left;width:300px;border-right:1px solid #000;">
h1>Menu/h1>
ul>
li>menu1/li>
li>menu2/li>
li>menu3/li>
/ul>
/div>
div style="float:left;width:500px;overflow:auto;">
h1>Welcome {$smarty.session.username}/h1>
/div>
That
相关阅读 更多 +
排行榜 更多 +