Twig和Xajax实现

时间:2013-03-12 08:42:50

标签: php twig xajax

我遇到了麻烦。我需要用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");

twig header(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)感到困惑。

1 个答案:

答案 0 :(得分:0)

在您的页面控制器中

$ twig-&gt; addGlobal('xajaxjs',$ xajax-&gt; printJavascript(“/ library / xajax”));

在标题模板中

{{xajaxjs}}