pass the variable to child view in zend - zend-framework

I am using zend framwork
in my controller i have set string to display in view like
Controller
$this->view->foo = 'sample string or content';
main view
echo $this->foo;
this is working fine show me string sample string or contentbut when i call other view inside this main view its not give any output
i am caaling something like this view inside view.
<?=$this->myform;?>
in myform.phtml
echo $this->foo;
this is not given any output...
Please help me what i am going worng...
thanks

Use Partial Helper like this:
In view:
<?php echo $this->partial('myform.phtml', array('foo' => $this->foo)); ?>

Related

zend framework 3 how can call phtml file in controller

I want to get html content in controller .
for this
public function posteemailAction(){
$view=$this->render("auth-acl/email/_getemail.phtml");
print_r($view);die;
}
Can anyone help for getting content of html in controller.
Thanks
Try this:
public function ajaxAction() {
// Turns off the layout and the view
$this->_helper->layout()->disableLayout();
$this->_helper->viewRenderer->setNoRender(true);
// Creates a new instance of Zend_View
$html = new Zend_View();
// Set a script path for above view
$html->setScriptPath(APPLICATION_PATH . '/views/scripts/your_custom_path/');
// Assinging some data to the view
$html->assign('myVar', $someValue);
// Renders the view to specified variable
$responseContent = $html->render('mycontent.phtml');
echo $responseContent;
}

Translate data in view sent from controller

I have a question about translating data sent from the controller to the view.
I can translate text in my view like this <?php echo $this->translate("Hello World!"); ?>
But in my controller I have this:
$this->view->message = 'Welcome! You do not have any surveys or quizzes yet.
To start creating your first survey or quiz,
click the button "Create New Survey" or "Create New Quiz".';
And in my view:
<?php echo $this->message ?>
How can I now make sure that the message will be translated? (I work with gettext and .po files ...)
Instead of $this->view->message = 'yourstring' in your controller, use this:
$this->view->message = $this->view->translate('your string to be translated');

Way to define others layout() parts such layout()->content?

Ive done some search but no success.. i am trying to figure out how to define others layout() parts such layout()->content variable.. i would love to get int layout()->navigation (a custom one) which display the navigation..
Any ideas ?
Thanks.
Not sure if this is what you want, but you can create additional 'parts' of layout simply by assigning a value to your new part. ZF will take care of the rest. For example, in a bootstrap.php you could do:
public function _initNewLayoutPart() {
$view = $this->bootstrap('view')->getResource('view');
$view->layout()->newpart = 'some new part';
}
Then in your layout.phtml you could just echo the new part:
<?php echo $this->layout()->newpart; ?>
It is possible by just creating a new variable in layout, you can define it in your controller (preferably in init or postDispatch). Just like this:
public function init()
{
$this->view->layout()->motd = '<b>Message of the day.</b>';
}
Then in your actual view where you want to see the message, all you have to do is:
<?php echo $this->layout()->motd; ?>
If you want something fancier, such as rendering a whole page or sidebar, try the following:
public function init()
{
$this->view->layout()->sidebar = $this->view->action('render', 'sidebar');
}
With render being the action (including render.phtml) and sidebar being the controller.

Zend Newbie Question - How to pass info from Controller Action into View?

On my controller I have the following Action:
public function indexAction()
{
$teamDao = new TeamsDao();
return $teamDao->showName(3);
}
So, I'm returning this team name, and my question is, how can I display this team name on the view ?
Thanks a lot,
MEM
Put this in your controller:
$this->view->teamName = $teamDoa->showName(3);
And this in your view
<?php echo $this->teamName?>

How do i assign a custom helper to each view differently in zend framework

i have a custom helper written which returns the html form as string which extends the Zend_view-hepler_Abstract
Now i have 3 helpers .How do i assign each helper to a different view .
It is something like this in the controller
class abc extends Zend_controller_front{
public action page1Action (){
// I want to use a different Helper
//How do i assign custom1 helper to this view Separately
}
public action page2Action (){
// I want to use a different Helper
//How do i assign custom2 helper to this view Separately
}
public action page3Action (){
// I want to use a different Helper
//How do i assign custom3 helper to this view Separately
}
}
um, should you be inheriting from Zend_Controller_Action not Zend_Controller_Front?
also the word 'action' is not a valid php keyword?
just use the view helper in your view script, so in abc/page1.phtml
print $this->page1Helper()
similarly for page2 and page3
but there may be an easier way... you can just
print $this->form;
and the form will print without the need for a helper?