Вопрос

I've installed TwigView inside my CakePHP application and I'm trying loading the elements header.twig.tpl and footer.twig.tpl inside the default template called default.twig.tpl.

It works fine with this:

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>Hello world!</title>

</head>
<body>
{% element 'header.twig' %}

    {{ test_var_from_controller }} <!-- this works and echoes "Hello world!" as expected -->

{% element 'footer.twig' %}
</body>
</html>

The problem starts when I try to use CakePHP default Helpers, they are simply ignored.

I'm not sure why, if I try:

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>Hello world!</title>
    {{ html.css('styes.min') }}
</head>

The method is ignored without throw errors and the page still working like it was never called.

I've followed all the TwigView explanation but it still not working, what I'm missing?

I'm also trying using Twig functions and they seems to be not loaded for some reason:

{{ 'FOO'|low }}

Throws the error:

Fatal error: Call to undefined function low() in .../app/Plugin/TwigView/tmp/views/2d/4b/65accc...92.php on line 45
Это было полезно?

Решение

I've found the problem, i needed to load the Helpers inside AppController first

class AppController extends Controller {
    public $viewClass = 'TwigView.Twig';
    public $ext = '.twig.tpl';
    public $helpers = array('Html', 'Form');
}

Другие советы

In CakePHP 3, if you are using the WyriHaximus plugin, you can put this in src/View/AppView.php

namespace App\View;
use WyriHaximus\TwigView\View\TwigView;

/**
 * Application View
 */
class AppView extends TwigView
{
  /**
   * Initialization hook method.
   * Use this method to add common initialization code like loading helpers.
   * e.g. `$this->loadHelper('Html');`
   * @return void
   */
  public function initialize()
  {
    parent::initialize();
    $this->loadHelper('Html');
    $this->loadHelper('Flash');
    $this->loadHelper('Url');
    ...
  }
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top