Translate data in view sent from controller - zend-framework

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

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

Views from the module won't display (CodeIgniter HMVC)

I'm actually working in an admin panel, and I want to manage all my sliders inside it,
so I created all the functions etc...
In the view file where I want to call the view from the module by running
<?php
echo Modules::run('sliders/_slider_display);
?>
But, The the sliders won't display, I will show you how I did it.
function _slider_display() {
$current_url = current_url();
$query_ads = $this->get_where_custom('target_url', $current_url);
$num_rows_ads = $query_ads->num_rows();
if($num_rows_ads>0){
$this->load->module('slides');
foreach($query_ads->result() as $row)
{
$parent_id= $row->id;
}
$data['query_slides']= $this->slides->get_where_custom('parent_id', $parent_id);
$this->load->view('sliders', $data);
}
}
as I mentioned, I tried to call the view in the homepage, but did not worked, I don't know what is the problem.
Thank you !

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-form in controller and processing this form in specific action - how can I do this?

Im my layout I have search form, so it's displayed on every page.
It looks like this:
<?
$form = new SearchIndexForm();
$form->setAction($this->configuration['BaseUrl']Index/search);
echo $form; ?>
I would like to have sth like that: when someone click submit button, the form redirects to the action "search" on "Index" controller, where there is processing the form values.
So, what should I do, to have equivallent to this, which will be working with data send by post method from form in layout?
if($this->_request->isPost()){
$formValues = $this->_request->getParams();
if ($form->isValid($formValues)){
...
}
}
With the above, when I click submit, it gets me to the /Index/search, but nothing happens...
The form itself worked perfectly when it was in one action.
It seems like you're doing everything right but I think that there is some code that you should change.
Try the following code
$request = Zend_Controller_Front::getInstance()->getRequest();
//or you can try
//$request = $this->getRequest();
if($request->isPost()){
$form = new SearchIndexForm();
if ($form->isValid( $request->getPost() ) ){
echo 'This should output if the form is valid' . PHP_EOL;
}
}
I don't like accessing variables directly as you did in $this->_request because Zend might have to do doing things to the variables to make them 'proper'. I know that sometimes nothing is done but better safe than sorry. Unless you're positive about it, which I'm usually not positive about unless I've really looked at the code.

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.