:
, , php-/, ob_start/ob_get_clean, js -, js . php...
, - :
class UnobtrusiveJsHelper {
protected static $_instance;
protected $_js = array();
protected $_ready = array();
public static function getInstance()
{
}
public static function setInstance(UnobtrusiveJsHelper $instance)
{
}
public function captureStart($key = null)
{
if(null !== $key)
{
$this->_js[$key] = null;
}
ob_start();
}
public function captureEnd($key = null)
{
if(null !== $key)
{
$this->_js[$key] = ob_get_clean();
return;
}
$this->_js[] = ob_get_clean();
public function __toString()
{
return $this->dumpJs() . $this->_dumpReady();
}
public function dumpJs(array $attributes = null)
{
if(!empty($this->_js))
{
return "<script type=\"text/javascript\">". implode("\n", $this->_js) . "</script>";
}
return null;
}
public function dumpReady(array $attributes = null)
{
if(!empty($this->_js))
{
return '<script type="text/javascript">$(document).ready(function(){'. implode("\n", $this->_js) . '});</script>';
}
return null;
}
}
: $js = UnobtrusiveJsHelper::getInstance();
:
<?php $js->captureStart(); ?>
var myjsvariable = 0;
<?php $js->captureEnd();
( ):
<?php echo isset($js) ? $js : null ?>
, . , Zend_Framework onLoad/Ready . .
, jQ, - Symfony.
, $jq->setVar('myjsvar', 1);, , , - :
var myjsvar = 1; script .
ZendX_Jquery Zend_Dojo .