我在php中有这个数组:
$pageTemplate = [
'pageHeader' => [
'pageMetatags' => $this->_metatags->getKeywords('PAGE_TXT', 1),
'messageBox' => $messageBox,
'developerName' => $this->_config->developer_name,
'developerUrl' => $this->_config->developer_name,
'baseHref' => $this->_config->app_path
,
'pageBody' =>
[
'interfaceCMS' => '',
'other' => ''
]
]];
此数组来自php的结果是:
Array
(
[pageHeader] => Array
(
[pageMetatags] => Array
(
[kwTitle] => title 1
[kwDescription] => desc 1
[kwKeywords] => key 1
[kwContentType] => xontent
[kwRobots] => robots
[kwRevisit] => revisir
[kwCopyrights] => 1
[kwGooglebot] => googlebot
[kwPublisher] => 2
)
[messageBox] => Array
(
[messageType] => SUCCESS_STATUS
[html] => kod html Pierwszy administrator i firma zostały utworzone! success error
[description] => Pierwszy administrator i firma zostały utworzone!
)
[developerName] => byname
[developerUrl] => byname
[baseHref] => http://localhost/admin
[pageBody] => Array
(
[interfaceCMS] =>
[other] =>
)
)
)
如何在.twig文件中下载并显示以下数据:
- kwTitle
- kwRevisit
- all from: pageBody
我尝试了类似的方法,但是它不起作用:
<base href="{{ arrayName['baseHref'] }}" />
<base target="_blank" href="{{ ['pageHeader']['baseHref'] }}" />
有人知道怎么做吗? 我尝试了不同的变体,但没有一个能给我任何东西:(我在寻求帮助
更新 我的VIEWCLASS:
namespace Core\Views;
use Core\Libraries\PsException;
use Twig;
use Twig_Environment;
use Core\Libraries\Registry;
class View
{
public static function render(string $view, array $args = [])
{
extract($args, EXTR_SKIP);
$config = Registry::register("Core\Libraries\Config");
$viewFile = null;
if (ADMIN_MODE === true) {
$viewFile = "../" . $config->backend_view_path . "/" . $view;
} else {
$viewFile = "../" . $config->frontend_view_path . "/" . $view;
}
if (is_readable($viewFile)) {
require_once $viewFile;
} else {
//$psException->registerError("$viewFile not found");
throw new \Exception("$viewFile not found");
}
}
public static function renderTemplate(string $template, array $args = [])
{
static $twig = null;
if ($twig === null) {
$config = Registry::register("Core\Libraries\Config");
$viewDirectory = null;
$cacheDirectory = null;
if (ADMIN_MODE === true) {
$viewDirectory = $config->backend_view_path;
$cacheDirectory = $config->backend_cache;
} else {
$viewDirectory = $config->frontend_view_path;
$cacheDirectory = $config->frontend_cache;
}
$loader = new \Twig_Loader_Filesystem('../' . $viewDirectory);
$twig = new \Twig_Environment($loader, ['cache' => $cacheDirectory]);
}
$template = str_replace("<h1>Welcome</h1>", "WELCOME!!!!!", $template);
echo $twig->render($template, $args);
}
}
答案 0 :(得分:1)
使用$twig->render();
将变量发送到视图时,第二个参数需要是一个关联数组,该数组的键是twig中的变量名称。
例如,针对您的情况:
echo $twig->render($template, array('pageTemplate' => $args));
您必须像对象一样调用发送到模板的数组的内容。
{{ someArray.someSubArray.SomeValue }}
我假设该数组被命名为树枝模板的pageTemplate
。
<base href="{{ pageTemplate.pageHeader.baseHref }}" />
否则,如果您只是按原样发送数组:
<base href="{{ pageHeader.baseHref }}" />
如果您有一个与http://localhost/admin
匹配的路由(假设您将其命名为admin
),则可以简单地调用路由名称以这种方式建立链接:
<base href="{{ path('admin') }}" />