erocha erocha - 1 month ago 16
PHP Question

Phalcon PHP NativeArray accessing multidimensional array using Volt Template

I was able to add multi language support in Phalcon using the volt template.

But I can't access phalcon multi dimensional NativeArray in volt.

Here is my getTranslation function:

private function _getTranslation()
{
global $config;

if ( isset($config[$_SERVER['HTTP_HOST']]['language']) ) {
$language = $config[$_SERVER['HTTP_HOST']]['language'];
} else if ( $this->session->get('auth') ) {
$language = "pt";
} else {
//Ask browser what is the best language
$language = $this->request->getBestLanguage();
}

//Check if we have a translation file for that lang
if (file_exists(__DIR__ . "/../translations/".$language.".php")) {
require __DIR__ . "/../translations/".$language.".php";
} else {
// fallback to some default
require __DIR__ . "/../translations/en-US.php";
}

//Return a translation object
return new \Phalcon\Translate\Adapter\NativeArray(array(
"content" => $messages
));
}


Then I setvar:

$this->view->setVar("t", $this->_getTranslation());


Which will load a translation file with something like this:

$messages = array(
"pages" => array(
"index" => array(
"title" => "CREATE YOUR ACCOUNT TODAY",
"call_to_action" => "Join Now!",
"search_title" => "Find:",
"search_option" => "I Want To:",
"search_type" => "Type:",
"search_where" => "Located On:",
"search_button" => "GO",
)
);


In the volt template I would like to access something like

$t['pages']['index']['call_to_action']


Which in volt could be:

<div class="call_to_action">{{ t._('pages').('index').('call_to_action') }}</div>


But this doesn't work!

Is there any way to access the NativeArray multi dimensional elements within volt template?

Raj Raj
Answer

Try this..

For more information refer twig documentation

{{ dump(t['pages']['index']['call_to_action']) }}

Output Example:

string 'Join Now!' (length=9)