I have a class MyData.php like this:
class myData {
function render() {
$view = new Zend_View();
$view->str = 'This is string.';
echo $view->render('myview.phtml');
}
}
and a myview.phtml file:
<div id='someid'><?= $this->str ?></div>
In another view I am doing something like this:
<?php
$obj = new myData ();
$obj->render(); // it should be <div id='someid'>This is string.</div>
?>
It is giving me following exception:
Message: no view script directory set; unable to determine location for view script
MyData.php and myview.phtml are in same directory.
I did it like this:
I changed my myview.phtml to myview.php
<div id='someid'><?= $this->str ?></div>
In myData class render function:
And all things are working as I asked in question. I was missing
$view->setScriptPath($path);
in my code.Help:
You are creating a new Zend_View instance. You should not do this. To get the existing view instance you could do as follows:
Also, I think that the view script path should be relative to
APPLICATION_PATH/views/scripts
folder.If you are usin the full MVC stack its youre better off just creating a view helper for this type of thing... or simply passing the using the Partial view helper and passing your object to it.
For example with the exisiting Zend_View_Helper_Partial....
in your controller create the myData object and assign it to the view:
in the view for the action:
Then in your
myview.phtml
you can do:For your example it looks like you dont even need the myData object at all you can just assign the
str
variable to the view and pass it along to the partial instead of creating an object.You should read the
Zend_View
docs...