是否可以在树枝3中制作全球宏?

发布于 2025-01-22 15:17:44 字数 1493 浏览 3 评论 0原文

我想将树枝从非常旧版本( 2.x 甚至 1.x )升级到 3.3 。在顶级模板中导入的旧版本宏中,可以在所有扩展和包含的模板中使用。而且我有一堆模板,需要我的宏(100+和许多嵌入块)。我不想在Evety模板中手动导入宏。

因此,根据类似问题 twig auto Importo acto 我试图实施建议的解决方案,但行不通。 实际上,我尝试了一下:

$tpl = $this->Twig->load('inc/function.twig');
$this->Twig->addGlobal('fnc', $tpl);

我也尝试了:

$tpl = $this->Twig->load('inc/function.twig');
$this->Twig->addGlobal('fnc', $tpl->unwrap());

但是我也有同样的结果。在模板中fnc是定义的,但它是一个模板对象,我无法访问宏。当我尝试这样做时,我会遇到致命错误:

Fatal error: Uncaught Twig\Error\RuntimeError: Accessing \Twig\Template attributes is forbidden

据我了解,在Twig 3中,您不能仅使用addglobal来包含宏。

旧的树枝被添加到我们的存储库中(不忽略),我们也可能会添加到存储库的新树枝,因此可以修改Twig的源代码。

upd: 当我尝试addglobal用宏我的模板我得到的

Fatal error: Uncaught LogicException: Unable to add global "fnc" as the runtime or the extensions have already been initialized.

模板我使用此解决方案(我已经扩展了环境 class)。

I want to upgrade my Twig from very old version (2.x or even 1.x) to 3.3. In old version macros imported in top level template are available in all extened and included templates. And I have a bunch of templates where I need my macros (100+ and many embed blocks). I don't want to import macros manually in evety template.

So, according to similar question Twig auto import macros by environment I tried to implement suggested solution, but it doesn't work.
Actually I tried this:

$tpl = $this->Twig->load('inc/function.twig');
$this->Twig->addGlobal('fnc', $tpl);

I also tried this:

$tpl = $this->Twig->load('inc/function.twig');
$this->Twig->addGlobal('fnc', $tpl->unwrap());

but I have same result. In templates fnc is defined but it's a Template object and I can not access macros. I get a fatal error when I try to do it:

Fatal error: Uncaught Twig\Error\RuntimeError: Accessing \Twig\Template attributes is forbidden

As I understand, in Twig 3 you can not just include macros using addGlobal.

Old Twig was added to our repository (was not ignored) and we probably will add to repository new Twig too, so it's possible to modify Twig's source code.

UPD:
When I try just to addGlobal my template with macros I get

Fatal error: Uncaught LogicException: Unable to add global "fnc" as the runtime or the extensions have already been initialized.

I've solved this problem using this solution (I've extended Environment class).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

森林迷了鹿 2025-01-29 15:17:44

在某些测试中,我发现您仍然可以在使用纯php的宏中定义的“函数”,

<?php
    $wrapper = $twig->load('macro.html');
    $template = $wrapper->unwrap();
    echo $template->macro_Foo().''; //return a \Twig\Markup

然后将其固定到位,您可以在宏上写一个包装器,并尝试将它们自动加载到容器中。


首先,我们需要扩展名来启用和访问

<?php
    class MacroWrapperExtension extends \Twig\Extension\AbstractExtension
    {
        public function getFunctions()
        {
            return [
                new \Twig\TwigFunction('macro', [$this, 'macro'], ['needs_environment' => true,]),
            ];
        }

        protected $container = null;

        public function macro(\Twig\Environment $twig, $template) {
            return $this->getContainer($twig)->get($template);
        }

        private function getContainer(\Twig\Environment $twig) {
            if ($this->container === null) $this->container = new MacroWrapperContainer($twig);
            return $this->container;
        }

    }

下一容器本身的容器。该容器负责在内存中加载和存储/保存(自动加载)宏。该代码将尝试在您的视图文件夹中找到并加载地图宏中的任何文件。

template
|--- index.html
|--- macros
|------- test.html
|
<?php
    class MacroWrapperContainer {
        const FOLDER = 'macros';

        protected $twig = null;
        protected $macros = [];

        public function __construct(\Twig\Environment $twig) {
            $this->setTwig($twig)
                 ->load();
        }

        public function get($macro) {
            return $this->macros[$macro] ?? null;
        }

        protected function load() {
            foreach($this->getTwig()->getLoader()->getPaths() as $path) {
                if (!is_dir($path.'/'.self::FOLDER))  continue;
                $this->loadMacros($path.'/'.self::FOLDER);
            }
        }

        protected function loadMacros($path) {
            $files = scandir($path);
            foreach($files as $file) if ($this->isTemplate($file)) $this->loadMacro($file);
        }

        protected function loadMacro($file) {
            $name = pathinfo($file, PATHINFO_FILENAME);
            if (!isset($this->macros[$name])) $this->macros[$name] = new MacroWrapper($this->getTwig()->load(self::FOLDER.'/'.$file));
        }

        protected function isTemplate($file) {
            return in_array(pathinfo($file, PATHINFO_EXTENSION), [ 'html', 'twig', ]);
        }

        protected function setTwig(\Twig\Environment $twig) {
            $this->twig = $twig;
            return $this;
        }

        protected function getTwig() {
            return $this->twig;
        }

        public function __call($method_name, $args) {
            return $this->get($method_name);
        }
    }

最后,我们需要模仿我在问题开头发布的行为。因此,让我们在宏观模板上创建一个包装器,该包装器将负责调用宏内部的实际功能。

如所看到的宏内部的功能带有宏_,因此,只需让Auto-Prefix使用Macro _

<?php
    class MacroWrapper {
        protected $template = null;

        public function __construct(\Twig\TemplateWrapper $template_wrapper) {
            $this->template = $template_wrapper->unwrap();
        }

        public function __call($method_name, $args){
            return $this->template->{'macro_'.$method_name}(...$args);
        }
    }

现在将扩展名注入twig中

$twig->addExtension(new MacroWrapperExtension());

这将启用函数acro在每个模板中,这使我们可以访问宏文件夹中的任何宏文件

{{ macro('test').hello('foo') }}

{{ macro('test').bar('foo', 'bar', 'foobar') }}

During some testing I found out you can still call the "functions" defined inside a macro with pure PHP

<?php
    $wrapper = $twig->load('macro.html');
    $template = $wrapper->unwrap();
    echo $template->macro_Foo().''; //return a \Twig\Markup

With this in place you could write a wrapper around the macro and try to auto load them in a container.


First off we need an extension to enable and access the container

<?php
    class MacroWrapperExtension extends \Twig\Extension\AbstractExtension
    {
        public function getFunctions()
        {
            return [
                new \Twig\TwigFunction('macro', [$this, 'macro'], ['needs_environment' => true,]),
            ];
        }

        protected $container = null;

        public function macro(\Twig\Environment $twig, $template) {
            return $this->getContainer($twig)->get($template);
        }

        private function getContainer(\Twig\Environment $twig) {
            if ($this->container === null) $this->container = new MacroWrapperContainer($twig);
            return $this->container;
        }

    }

Next the container itself. The container is responsible to load and store/save the (auto loaded) macros in the memory. The code will try to locate and load any file in the map macros in your view folder.

template
|--- index.html
|--- macros
|------- test.html
|
<?php
    class MacroWrapperContainer {
        const FOLDER = 'macros';

        protected $twig = null;
        protected $macros = [];

        public function __construct(\Twig\Environment $twig) {
            $this->setTwig($twig)
                 ->load();
        }

        public function get($macro) {
            return $this->macros[$macro] ?? null;
        }

        protected function load() {
            foreach($this->getTwig()->getLoader()->getPaths() as $path) {
                if (!is_dir($path.'/'.self::FOLDER))  continue;
                $this->loadMacros($path.'/'.self::FOLDER);
            }
        }

        protected function loadMacros($path) {
            $files = scandir($path);
            foreach($files as $file) if ($this->isTemplate($file)) $this->loadMacro($file);
        }

        protected function loadMacro($file) {
            $name = pathinfo($file, PATHINFO_FILENAME);
            if (!isset($this->macros[$name])) $this->macros[$name] = new MacroWrapper($this->getTwig()->load(self::FOLDER.'/'.$file));
        }

        protected function isTemplate($file) {
            return in_array(pathinfo($file, PATHINFO_EXTENSION), [ 'html', 'twig', ]);
        }

        protected function setTwig(\Twig\Environment $twig) {
            $this->twig = $twig;
            return $this;
        }

        protected function getTwig() {
            return $this->twig;
        }

        public function __call($method_name, $args) {
            return $this->get($method_name);
        }
    }

Last off we need to mimic the behavior I've posted in the beginning of the question. So lets create a wrapper around the macro template which will be responsible to call the actual functions inside the macro.

As seen the functions inside a macro get prefixed with macro_, so just let auto-prefix every call made to the macro wrapper with macro_

<?php
    class MacroWrapper {
        protected $template = null;

        public function __construct(\Twig\TemplateWrapper $template_wrapper) {
            $this->template = $template_wrapper->unwrap();
        }

        public function __call($method_name, $args){
            return $this->template->{'macro_'.$method_name}(...$args);
        }
    }

Now inject the extension into twig

$twig->addExtension(new MacroWrapperExtension());

This will enable the function macro inside every template, which lets us access any macro file inside the macros folder

{{ macro('test').hello('foo') }}

{{ macro('test').bar('foo', 'bar', 'foobar') }}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文