[英]Twig and Xajax implementating
i am in trouble. 我有麻烦了。 I need to remake a site with Twig.
我需要用Twig重新制作一个网站。 But i cannot implement my xajax library working.
但是我无法实现我的xajax库工作。 Before Twig it was working perfect, but now i think i am calling xajax variables not right.
在Twig之前,它工作得很完美,但现在我认为我称xajax变量不正确。 So i will show some code.
所以我将显示一些代码。 I'm doing login page.
我正在登录页面。
php header: php标头:
global $command;
global $xajax;
<!DOCTYPE html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>site name :: </title>
$xajax->printJavascript("/library/xajax");
twig header(header.tpl): 树枝头(header.tpl):
{{command}} //this should be global variable
{{xajax}} //this should be global variable
<html>
<head>
<script type="text/javascript" src="/library/jquery/jquery-1.7.1.min.js"></script>
{{ xajax.printJavascript("/library/xajax") }} //this should be something like this in php header -> $xajax->printJavascript("/library/xajax");
<title> :: {{pageName}} :: Marius page</title>
<link rel="stylesheet" type="text/css" href="/includes/ui/default/css/styles.css">
</head>
<body>
That is my page controller: 那是我的页面控制器:
try {
// specify where to look for templates
$loader = new Twig_Loader_Filesystem('includes/ui/default/');
// initialize Twig environment
$twig = new Twig_Environment($loader);
$twig->addGlobal('xajax', $xajax);
// load template
$template = $twig->loadTemplate('login.tpl');
if (!isset($_SESSION['currentuser'])) {
if ($_SERVER['REQUEST_URI'] != '/login') {
header("Location: /login");
}
}
$app = $_SERVER['REQUEST_URI'];
echo $template->render(array(
'url' => $app ,
'pageName' => 'Login',
));
} catch (Exception $e) {
die ('ERROR: ' . $e->getMessage());
}
This is login view file, it is the same with php and Twig, just two global variables has called in php brackets, this one is in login.tpl 这是登录视图文件,与php和Twig相同,只是在php括号中调用了两个全局变量,这是在login.tpl中
{{command}} //this should be global variable global $command
{{xajax}} //this should be global variable global $xajax
{% include 'include/header.tpl' %}
<header>
<h1>Wellcome,</h1>
<h3>this is Marius <em>Twig</em> page!</h3>
</header>
{% include 'include/navigation.tpl' %}
<h2>Login</h2>
<form action="" id="loginform">
<p>Please enter your login information.</p>
<!-- TEXTBOXES -->
<label>Username</label><br />
<input name="username" type="text" class="text large required" id="username" />
<br />
<div class="clearfix"> </div
><label>Password</label><br />
<input name="password" type="password" class="text large required" id="password" />
<br />
<div class="clearfix"> </div>
<p>
<input name="btnLogin" type="button" class="submit" id="btnLogin" value="LOGIN" onclick=" xajax_login(xajax.getFormValues('loginform'));" />
<input type="checkbox" class="checkbox" id="cbdemo2" />
<label for="cbdemo2">Remember Me</label>
<div id="loginresult" style="text-align:center; color:#000; text-shadow: #FFF 1px 1px 1px"></div>
</p>
</form>
</body>
</html>
And now when i am clicking login button, browser console gives me an error: 现在,当我单击登录按钮时,浏览器控制台给我一个错误:
ReferenceError: xajax_login is not defined
xajax_login(xajax.getFormValues('loginform'));
So i think there is a problem with calling xajax library, and i have confused in this templating engine(TWIG).. 因此,我认为调用xajax库存在问题,并且我对此模板引擎(TWIG)感到困惑。
In your page controller 在页面控制器中
$twig->addGlobal( 'xajaxjs', $xajax->printJavascript("/library/xajax") ); $ twig-> addGlobal('xajaxjs',$ xajax-> printJavascript(“ / library / xajax”));;
And in your header template 并在标题模板中
{{xajaxjs}} {{xajaxjs}}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.