We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

I need to render multiple views in one action with different translation files

I need to render multiple views in one action with different translation files As i'll send emails for users and each one will include view with different translation files but i found that $this->t can see new translation file normaly but view only see the first loaded file

edited Nov '15

You could use partials. Here is an example code from application of mine:

        $viewParams = array(
            'sizes' => (array) $settings->thumbnails,
            'image' => Uploads::findFirst($this->dispatcher->getParam('id'))
        ); 

        $response = new \stdClass(); 
        $this->view->setRenderLevel(\Phalcon\Mvc\View::LEVEL_NO_RENDER); 
        $response->html = $this->view->getRender('templates', 'editThumbnailsPopup', $viewParams, function($view) {
            $view->setRenderLevel(\Phalcon\Mvc\View::LEVEL_ACTION_VIEW);
        });       

Important functions are setRenderLevel and getRender. You can use those multiple times for all the translations or even use in foreach. In my example getRender will look for template file in \views\templates\editThumbnailsPopup.phtml

More info on view methods here:

https://docs.phalcon.io/en/latest/api/Phalcon_Mvc_View.html

If you just want returned html from rendered view then you can ust it like this:

$view->setVars(array);
$view->start();
$view->render(null, 'template name');
$view->finish();
$html = $view->getContent();

Where $view is just view service from container. And then you can just put $html as body in mail.

No i need to make something like this

$view->setVars(array); $view->start(); $view->render(null, 'template name'); $view->finish(); $html = $view->getContent();

//then change translation file

$view->setVars(array); $view->start(); $view->render(null, 'template name'); $view->finish(); $html2= $view->getContent();

In the seconde view couldn't see the second translation file but see the first one

It didn't solve my problem as from the second iteration volt file use the first translation file and didn't use the new one

You could use partials. Here is an example code from application of mine:

       $viewParams = array(
           'sizes' => (array) $settings->thumbnails,
           'image' => Uploads::findFirst($this->dispatcher->getParam('id'))
       ); 

       $response = new \stdClass(); 
       $this->view->setRenderLevel(\Phalcon\Mvc\View::LEVEL_NO_RENDER); 
       $response->html = $this->view->getRender('templates', 'editThumbnailsPopup', $viewParams, function($view) {
           $view->setRenderLevel(\Phalcon\Mvc\View::LEVEL_ACTION_VIEW);
       });       

Important functions are setRenderLevel and getRender. You can use those multiple times for all the translations or even use in foreach. In my example getRender will look for template file in \views\templates\editThumbnailsPopup.phtml

More info on view methods here:

https://docs.phalcon.io/en/latest/api/Phalcon_Mvc_View.html

Show us whole code.

foreach($users as $user){ $this->getDI()->set('language',$user->getLocale());//to load translation file $this->view->start(); $this->view->render('emails', 'my_voltfile'); $this->view->finish(); $content = $this->view->getContent(); $email = array( 'html' => $content, 'text' => $this->t->('hello'), 'subject' => $this->t->_('hello'), 'from_email' => '[email protected]', 'fromname' => $this->t->('project'), 'to' => $user->getEmail() ); //here hello translated successfully but the content of volt file translated according to first translation file $this->mail->send($email); }

Yes its first file translated cuz you just have still the same $content. I dont know what

$this->getDI()->set('language',$user->getLocale()); 

this mean, you are setting you value in di called language this language, where is it used ? Check this out:

https://forum.phalcon.io/discussion/6503/how-to-use-multi-language-on-volt-template

There is solution how to use translastion in volt templates. You just have to render other $content for each user, not just one content for all users.

How can i still hav the same $content everytime i call render function to render the volt file with different language and $this->t can see the new translation file , but i think that render save translation file somewhere and use it in the next time and didn't use $this->t

Yes its first file translated cuz you just have still the same $content. I dont know what

$this->getDI()->set('language',$user->getLocale()); 

this mean, you are setting you value in di called language this language, where is it used ? Check this out:

https://forum.phalcon.io/discussion/6503/how-to-use-multi-language-on-volt-template

There is solution how to use translastion in volt templates. You just have to render other $content for each user, not just one content for all users.

Maybe show us view then ?