Php Symfony检查jQuery是否已经包含
有没有办法在sfContext中检查是否已经添加了jQuery库, 使用: 或 或 在模板中Php Symfony检查jQuery是否已经包含,php,jquery,symfony1,symfony-1.4,Php,Jquery,Symfony1,Symfony 1.4,有没有办法在sfContext中检查是否已经添加了jQuery库, 使用: 或 或 在模板中 在我看来,添加更多jQuery会停止它们的操作。您应该使用自己的检查 使用您自己的sfWebResponse 重写addJavascript 加支票 因此,在/lib/response/文件夹中创建一个新的myWebResponse.class.php文件(创建它),代码如下: class myWebResponse extends sfWebResponse { public function
在我看来,添加更多jQuery会停止它们的操作。您应该使用自己的检查
sfWebResponse
addJavascript
/lib/response/
文件夹中创建一个新的myWebResponse.class.php
文件(创建它),代码如下:
class myWebResponse extends sfWebResponse
{
public function addJavascript($file, $position = '', $options = array())
{
$this->validatePosition($position);
// check if the file to add is jquery
// nb: you might update the regex
if (preg_match('/jquery/i', $file))
{
// retrieve all loaded js
$files = array_keys($this->getJavascripts());
// check if one them containt jquery
// nb: you might update the regex
$isJquery = preg_grep("/jquery/i", $files);
// jquery found so do not add the file
if ( ! empty($isJquery))
{
return;
}
}
$this->javascripts[$position][$file] = $options;
}
然后,默认情况下使用新的响应。在您的应用程序/frontend/config/factories.yml
中,添加:
all:
response:
class: myWebResponse
你完了。真的吗?你一定弄错了!我们需要moar jquery!!
use_javascript('jquery-1.4.2.min.js');
class myWebResponse extends sfWebResponse
{
public function addJavascript($file, $position = '', $options = array())
{
$this->validatePosition($position);
// check if the file to add is jquery
// nb: you might update the regex
if (preg_match('/jquery/i', $file))
{
// retrieve all loaded js
$files = array_keys($this->getJavascripts());
// check if one them containt jquery
// nb: you might update the regex
$isJquery = preg_grep("/jquery/i", $files);
// jquery found so do not add the file
if ( ! empty($isJquery))
{
return;
}
}
$this->javascripts[$position][$file] = $options;
}
all:
response:
class: myWebResponse