简体   繁体   中英

How to print values in an array in a twig?

i have this array in 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' => ''
                        ]
                    ]];

the result of this array from php is:

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] => 
                )

        )

)

how can I download and display the following data in the .twig file:

- kwTitle
- kwRevisit
- all from: pageBody 

I tried something like this but it does not work:

<base href="{{ arrayName['baseHref'] }}" />
<base target="_blank" href="{{ ['pageHeader']['baseHref'] }}" />

Does anyone know how to do it? I tried different variants, but none of them shows me anything :( I am asking for help

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

When you are sending variables to the view using $twig->render(); , the second parameter needs to be an associative array, the keys of that array being the variables names in twig.

In example, for your case :

echo $twig->render($template, array('pageTemplate' => $args));

You have to call the content of the array sent to the template like an object.

{{ someArray.someSubArray.SomeValue }}

I assume the array is named pageTemplate to the twig template.

<base href="{{ pageTemplate.pageHeader.baseHref }}" />

Else, if you just sent the array as is :

<base href="{{ pageHeader.baseHref }}" />

If you have a route matching for http://localhost/admin (let's suppose you called it admin ), you can simply call the route name to build a link this way :

<base href="{{ path('admin') }}" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM