0

我有麻烦了。我需要用 Twig 重新制作一个网站。但我无法实现我的 xajax 库工作。在 Twig 之前它工作得很好,但现在我认为我调用 xajax 变量不正确。所以我会展示一些代码。我正在做登录页面。

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

树枝头(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> 

那是我的页面控制器:

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

这是登录视图文件,和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">&nbsp;</div

                    ><label>Password</label><br />
                    <input name="password" type="password" class="text large required" id="password" />
                     <br />

                     <div class="clearfix">&nbsp;</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>

现在,当我单击登录按钮时,浏览器控制台给了我一个错误:

ReferenceError: xajax_login is not defined
xajax_login(xajax.getFormValues('loginform'));

所以我认为调用 xajax 库有问题,我对这个模板引擎(TWIG)感到困惑..

4

1 回答 1

0

在您的页面控制器中

$twig->addGlobal('xajaxjs', $xajax->printJavascript("/library/xajax"));

在你的标题模板中

{{xajaxjs}}

于 2013-03-12T15:12:32.893 回答