Phalcon PHP NativeArray使用Volt模板访问多维数组

时间:2014-12-05 23:58:10

标签: php arrays multidimensional-array phalcon volt

我能够使用伏特模板在Phalcon中添加多语言支持。

但我无法在伏特中访问phalcon多维NativeArray。

这是我的getTranslation函数:

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

然后我setvar:

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

将使用以下内容加载翻译文件:

$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",
    )
);

在伏特模板中,我想访问类似

的内容
$t['pages']['index']['call_to_action']

以伏特为单位:

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

但这不起作用!

有没有办法在伏特模板中访问NativeArray多维元素?

1 个答案:

答案 0 :(得分:0)

试试这个..

有关更多信息,请参阅twig documentation

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

输出示例:

string 'Join Now!' (length=9)