how to pass variable 开发者_StackOverflowfrom zf to javascript/jquery?
thanks
you can create your javascript dynamicly by useing Headscript View Helper it had function like :
<?php $this->headScript()->captureStart() ?>
var action = '<?php echo $this->baseUrl ?>';
$('foo_form').action = action;
<?php $this->headScript()->captureEnd() ?>
Source : ZF documentation
<?php
$this->view->foo = $foo;
?>
In your view:
<script type="text/javascript">
var foo = <?=Zend_Json::encode($this->foo)?>;
</script>
I know this is an old question, but I'm sure others could benefit from this:
You could use this extended HeadScript class and pass variables to JavaScript from a controller like this:
$this->view->headScript()->appendVar('dogName', 'Rocky')
->appendVar('dogPictures', $pictures);
prependVar
, setVar
and offsetSetVar
are also available for convenience' sake.
<?php
class SomeController extends Zend_Controller_Action {
public function indexAction() {
// a file
$this->view->headScript()->appendFile('yourJsFile.js');
// inline script
$message = 'hello!';
$this->view->headScript()->appendScript("jQuery(function(){alert('$message');});");
}
}
This requires that you add echo $this->headScript()
to your view- or layout-script.
I know this will be late answer, And I have used @chelmertz solution, but currently I got some issue with that. I am working in an complex UI application and I needed lots of php arrays and other json objects in my external javascript files. And inclusion of my js scripts was not uniform, So each time if I need some thing from php to javascript I have to check whole things like ....
1) Am I placing appendScript() at correct position so the some.js script will find my variables defined
2) generated string which will be passed to appendScript() is a valid js syntax. and lot more ...
There is another way I have developed for Zend Framework specially. I have written a detailed tutorial here http://rupeshpatel.wordpress.com/2012/09/21/add-json-objects-arrays-and-variables-from-php-to-javascript/
summary:
1) create a common view file enclosed within tag, which checks for a property(an Array) let say $this->jsVars of a view object and defines js variables
code of this view file
<script>
<?php if(!empty($this->jsVars)){ ?>
<?php
foreach($this->jsVars as $var) {
if($var['type'] == 'json'){
?>
var <?php echo $var['name']?> = JSON.parse('<?php echo $var['value'];?>');
<?php } elseif($var['type'] == 'text'){ ?>
var <?php echo $var['name']?> = "<?php echo $var['value'];?>";
<?php }else{?>
var <?php echo $var['name']?> = <?php echo $var['value'];?>;
<?php
}}?>
<?php }?>
</script>
2) render this view in your layout file before echo $this->headScript()
so all external scripts will have those js variables
<?php
echo $this->render('index/include_js_vars.phtml'); // rendering view
$this->headScript()->prependFile(JS_BASE_URL.'/jquery-1.7.2.min.js');
$this->headScript()->appendFile(JS_BASE_URL.'/jquery.blockUI.js');
$this->headScript()->appendFile(JS_BASE_URL.'/commonJS.js');
$this->headScript()->appendFile('http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/jquery-ui.min.js');
echo $this->headScript();
?>
3) append php arrays or json objects to $this->view->jsVars like
// in your action method
$test = array('mango','orange','banana');
$this->view->jsVars[] = array('name'=>'test','type'=>'json','value'=>json_encode($test));
now test
will be defined in any of your script files as an array, No matter at what instance you have included your js file.
I came here and tried all the answers but non of them helped me, so what I ended up doing was the next (maybe it's too late, but could be helpful for someone else):
Inside the controller where you want to pass the variable to js:
public function someAction()
{
$var = array(
'test' => 'phpVar',
'test2' => 'phpVar2'
);
$this->view->jsVar = $var;
}
Now in your layout, inside head tag, and before load any other script do this:
$this->headScript()->appendScript('var phpVars='.json_encode($this->jsVar));
Finally in your js file, you would be able to access the variable phpVars:
console.log(phpVars);
you could see in the console:
Object {test: "phpVar", test2: "phpVar2"}
being able to access each variable like phpVars.test
and phpVars.test2
Now you can pass variables from your controller to js files everytime you want.
精彩评论