简体   繁体   English

Symfony:模板,检查base.html.php是否已扩展

[英]Symfony: templating, check if base.html.php is already extended

I have a small problem. 我有一个小问题。

Im creating currently a Menu which works on ajax calls and should only replace a specific path part. 我目前正在创建一个菜单,该菜单可用于ajax调用,并且应该仅替换特定的路径部分。

My base.html.php is this: 我的base.html.php是这样的:

<html>
  <head>
  </head>
  <body>
    <nav>
    </nav>
    <div id="contentWrapper">
      <div id="sidebar">
        <?php if ($view['slots']->has('sidebar')): ?>
          <?php $view['slots']->output('sidebar') ?>
        <?php endif; ?>
      </div>
      <div id="content">
        <?php $view['slots']->output('content') ?>
      </div>
    </div>
  </body>
</html>

My pages are all build like this: 我的页面都是这样构建的:

<?php $view->extend('Bundle::base.html.php') ?>
<?php $view['slots']->start('sidebar') ?>
<ul>
</ul>
<?php $view['slots']->stop() ?>
<?php $view['slots']->start('content') ?>
  some content
<?php $view['slots']->stop() ?>

My problem is now when im loading a new page via ajax call and replace the contentWrapper , it puts also once again the whole base.html.php into my contentWrapper . 现在我的问题是,当我通过ajax调用加载新页面并替换contentWrapper ,它又将整个base.html.php再次放入了contentWrapper The problem is, im using the current way because it should be also possible to access the files with the whole base.html.php with a single URL, without clicking first. 问题是,即时通讯使用的是当前方式,因为还可以通过单个URL访问整个base.html.php文件,而无需先单击。

Is there a way to check if base.html.php had been already expanded? 有没有办法检查base.html.php是否已经扩展? Or is there another solution which could solve my case easier? 还是有其他解决方案可以更轻松地解决我的案件?

You should check if it is an ajax request in your controller and then conditionnally extend or not to the base.html.php template : 您应该检查它是否是控制器中的ajax请求,然后有条件地扩展到base.html.php模板:

Controller : 控制器:

public function someAction()
{
    $request = $this->getRequest();

    // ...
    return $this->render(
        'MyBundle:Controller:action.html.php', 
        array('isAjax' => $request->isXmlHttpRequest())
    );
}

Template : 范本:

<?php 
if ($isAjax) { 
    $view->extend('Bundle::base.html.php');
}
// ...
?>

Or you could also check directly in the template : 或者,您也可以直接在模板中签入:

<?php 
if ($app->request->xmlHttpRequest) { 
    $view->extend('Bundle::base.html.php');
}
// ...
?>

Or at last, you could get only the html content you want from the response (not the cleanest way to go since you retreive useless content). 或最后,您只能从响应中获取所需的html内容(这不是最干净的方法,因为您检索了无用的内容)。 Assuming you use jQuery for your ajax request : 假设您将jQuery用于ajax请求:

$.get('/url', function(html){
    $('#contentWrapper').html($(html).find('#contentWrapper').html());
});
// or 
$('#contentWrapper').load('/url #contentWrapper');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM