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

translations + setVar on volt template

Hello,

I have an API that has no views, but I need to send mails from some classes. The mail can be sent in different languages, and I also need to replace strings on the template after I have replaced all the language strings.

Adding bits of code to show what I am doing:

en\activate.php

$messages = array(
    'greetings'     => 'Dear {{ user_name }}',
    'introduction'  => 'An account has been created at {{ site_name }}.',
);

activate.volt

<td>
    <p>{{ greetings }},</p>
    <p></p>
    <p>{{ introduction }}.</p>
    <p></p>
</td>

controller

$di = $this->getDI();
$this->view = new Phalcon\Mvc\View\Simple($di);
$this->view->setViewsDir('/views/');
$this->view->registerEngines([
    '.volt' => function($view, $di) {
        $volt = new \Phalcon\Mvc\View\Engine\Volt($view, $di);
        $cachePath = $this->path . 'cache/';
        $volt->setOptions([
            'compiledPath' => $cachePath,
            'compiledSeparator' => '_',
            'compileAlways' => true
        ]);

        return $volt;
    }
]);

require $l10nFile =  'messages/en/activate.php';
// loads the $message array with all the translations
$this->view->setVar('user_name','Human');
$this->view->setVar('site_name','www.example.com');
$content = $this->view->Render('activate.volt', $messages);

This is what I am getting:

<td>
    <p>Dear {{ user_name }},</p>
    <p></p>
    <p>An account has been created at {{ site_name }}.</p>
    <p></p>
</td>

But this is what I want to get:

<td>
    <p>Dear Human,</p>
    <p></p>
    <p>An account has been created at www.example.com.</p>
    <p></p>
</td>

So, how can I achieve this? maybe the whole approach is wrong. Any ideas?

Note: It's important to mention that I don't have any views registered in the DI object, I am instancing the view only when I need it, so I don't want to have objects in memory that I will use only a few times.

edited Feb '16

P.S. Currently, to achieve this I am doing something like this:

$content = str_replace("{{ user_name }}", "Human", $content);

But I want a cleaner solution.

edited Feb '16

Hello, i can't remember in which project i was in your exact situation.

Please read this section "Hierarchical Rendering" (second example) here: https://docs.phalcon.io/en/latest/reference/views.html#stand-alone-component

I remember i played with those variables: https://docs.phalcon.io/en/latest/reference/views.html#control-rendering-levels

Will get back to you with example if i manage to remember where i used it.

Example as promised!

In this project i needed to render image editing popup with ajax:

        $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);
        });       
        die(json_encode($response));