zend framework 3 how can call phtml file in controller - zend-framework

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;
}

Related

pass the variable to child view in zend

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)); ?>

Zend capture layout and view content as variable

I have a controller Mycontroller with simple exemple action:
public function exempleAction(){
// Using layout "mail"
$this->_helper->layout()->setLayout("mail");
}
I want to get HTML content of the view using: (to use it later as email content)
$view_helper = new Zend_View_Helper_Action();
$html_content = $view_helper->action('exemple', 'Mycontroller','mymodule');
This successfully allow me to get the view content but WITHOUT the layout content. All the HTML code of the layout "mail" is not included in $html_content.
How can i capture the whole content includind the layout part?
If I'm not mistaken, it is normal that you do not have the layout after $view_helper->action('exemple', 'Mycontroller','mymodule');
Indeed, the layout is call in postDisatch() of Zend_Layout_Controller_Plugin_Layout's plugin.
You can still try this:
In your layout 'mail.phtml' put this:
echo $this->layout()->content;
In your method :
$view_helper = new Zend_View_Helper_Action();
$html_content = $view_helper->action('exemple', 'Mycontroller','mymodule');
$layout_path = $this->_helper->layout()->getLayoutPath();
$layout_mail = new Zend_Layout();
$layout_mail->setLayoutPath($layout_path) // assuming your layouts are in the same directory, otherwise change the path
->setLayout('mail');
// Filling layout
$layout_mail->content = $html_content;
// Recovery rendering your layout
$mail_content = $layout_mail->render();
var_dump($mail_content);
Try this:
//this will get the current layout instance
//clone it so you wont see any effects when changing variables
$layout = clone(Zend_Layout::getMvcInstance());
//if you want to use another layout script at another location
//$path = realpath(APPLICATION_PATH . '/../emails/');
//$layout->setLayoutPath($path);
//set the layout file (layout.phtml)
//$layout->setLayout('layout');
//prevent this layout from beeing the base layout for your application
$layout->disableLayout();
//get your view instance (or create a new Zend_View() object)
$view = $this->view; //new Zend_View();
//set the path to view scripts if newly created and add the path to the view helpers
//$view->setBasePath(realpath(APPLICATION_PATH.'/../application/emails')."/");
//$view->addHelperPath(realpath(APPLICATION_PATH.'/../application/layouts/helpers/')."/", 'Application_Layout_Helper');
//set some view variables if new view is used (used in the view script $this->test)
$view->assign('test', 'this can be your value or object');
//set the content of your layout to the rendered view
$template = 'index/index.phtml';
$layout->content = $view->render($template);
$bodyHtml = $layout->render();
Zend_Debug::dump($bodyHtml);
Or
Get the response body in your action.
This will store the html that normally would be send to the browser as response.
//first disable output if needed
$this->view->layout()->disableLayout();
//get the response object
$response = $this->getResponse();
$bodyHtml = $response->getBody();
Zend_Debug::dump($bodyHtml);
Have fun!

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.

Load Zend_Form from a view helper in Zend?

Is it possible to load a Zend_Form from a view helper? I'm using thise form in a login action method. But I also want this form to be visible on the navigation on every page (so without the login action actually being called yet), the post method of the form will send to the login action method.
I'm guessing it should be done with a view helper but I don't see how.
Any ideas?
I tried with this:
my view helper:
class Zend_View_Helper_LoginForm
{
function getLoginForm(){
$form = new Form_LoginForm();
return $form;
}
}
and I call it from my layout like this:
<?php echo $this->form(); ?> but this doesn't work. (I'm able to call the same form through an action method though!)
In this case it gives me this error (which doesn't make sense because my helper is only 9 lines long):
Warning: Missing argument 1 for Zend_View_Helper_Form::form() in C:\xampplite\htdocs\zendpr\library\Zend\View\Helper\Form.php on line 44
Your view helper should extends the Zend_View_Helper_Abstract class and the method of the view helper must have the same name as the class :
class Zend_View_Helper_LoginForm extends Zend_View_Helper_Abstract
{
function loginForm() {
$form = new Form_LoginForm();
return $form;
}
}
and you call it like this in your view script :
echo $this->loginForm();
If you call :
echo $this->form();
You are using the view helper Zend_View_Helper_Form
Have your View_Helper extend Zend_View_Helper_Abstract and override the setView()
class Zend_View_Helper_XX extends Zend_View_Helper_Abstract {
public $view;
public function setView(Zend_View_Interface $view)
{
$this->view = $view;
}
Initialise the form in your controller action and set the form reference
// controller action code
$this->view->form = $form;
Then in the view helper you can reference the form via the view
// view helper code
$this->view->form;
class Zend_View_Helper_LoginForm extents Zend_Form {
function getLoginForm(){
$form = new Form_LoginForm();
return $form;
}
}
OR
$this->view->form=$form;
Both are going to return the form. The view form is more specific for view.
Add this into your phtml view file
echo $this->form();
To answer this question - Remove the Parenthetical
Should be
echo $this->form;

set/add view from controller action

I'm trying to set a view script to be executed in addition to the currently requested action view script. I want to do this from the controller action itself in a way that this new view script output will be available from the layout $this->layout()->content helper.
I found the setView() method but don't know how to use it from the controller.
Thanks a lot.
If you just want to render some other view script from a controller just:
$this->render('someotherview');
Wich will render someotherview.phtml.
from: http://framework.zend.com/manual/en/zend.controller.action.html#zend.controller.action.viewintegration.render
class MyController extends Zend_Controller_Action{
public function fooAction()
{
// Renders my/foo.phtml
$this->render();
// Renders my/bar.phtml
$this->render('bar');
// Renders baz.phtml
$this->render('baz', null, true);
// Renders my/login.phtml to the 'form' segment of the
// response object
$this->render('login', 'form');
// Renders site.phtml to the 'page' segment of the response
// object; does not use the 'my/' subirectory
$this->render('site', 'page', true);
}
public function bazBatAction()
{
// Renders my/baz-bat.phtml
$this->render();
}
}
Should get you on the right track!
Also
$this->renderScript('path/to/index.phtml');
Works really well.