从 php 访问 twig 模板变量

发布于 2024-12-12 19:09:42 字数 196 浏览 1 评论 0原文

是否可以从 php 访问 twig 模板中定义的每个变量?

例如:

Template:
...
{% set foo = 'foo' %}
...

来自 PHP:

echo $template->foo

或者类似的东西。

Is it possible to access every variable defined in a twig template from php?

Eg:

Template:
...
{% set foo = 'foo' %}
...

And from PHP:

echo $template->foo

Or something like that.

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

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

发布评论

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

评论(3

獨角戲 2024-12-19 19:09:42

访问每个变量非常麻烦,所以我最终所做的是创建一个扩展来保存我需要的数据:

class SampleExtension extends Twig_Extension {
    private $foo;

    function getName() {
        return 'sampleExtension';
    }

    function getFunctions() {
        return array(
            'setFoo' => new Twig_Function_Method($this, 'setFoo')
        );
    }

    function setFoo($value) {
        $this->foo = $value;
    }

    function getFoo() {
        return $this->foo;
    }
}

在我需要数据的类中:

$this->sampleExtension = new SampleExtension();
$twigEnv->addExtension($this->sampleExtension);
...
$html = $twigEnv->render('myTemplate.tpt', ...);

使用此模板:

...
{{ setFoo('bar') }}
...

渲染后:

echo $this->sampleExtension->getFoo(); // Prints bar

Accessing every variable is very cumbersome, so what I did in the end was to create an extension which holds the data that I need:

class SampleExtension extends Twig_Extension {
    private $foo;

    function getName() {
        return 'sampleExtension';
    }

    function getFunctions() {
        return array(
            'setFoo' => new Twig_Function_Method($this, 'setFoo')
        );
    }

    function setFoo($value) {
        $this->foo = $value;
    }

    function getFoo() {
        return $this->foo;
    }
}

And in the class where I needed the data:

$this->sampleExtension = new SampleExtension();
$twigEnv->addExtension($this->sampleExtension);
...
$html = $twigEnv->render('myTemplate.tpt', ...);

Using this template:

...
{{ setFoo('bar') }}
...

After render:

echo $this->sampleExtension->getFoo(); // Prints bar
自在安然 2024-12-19 19:09:42

如果您想访问模板变量,您可以发送此变量作为参考。

$foo = '';
$args['foo'] = &$foo;
$twig->render($template, $args);
...
echo $foo;

示例:(目标是将电子邮件正文和主题放在一个模板中)

Twig_Autoloader::register();
$loader = new Twig_Loader_String();
$twig = new Twig_Environment($loader);
$tl = <<<EOL
{% set subject = "Subject of a letter" %}
Hello, {{ user }}

This is a mail body

-- 
Site
EOL;
$mail['to'] = '[email protected]';
$mail['subject'] = '';
$args = array(
    'user' => 'John', 
    'subject' => &$mail['subject']
);
$mail['message'] = $twig->render($tl, $args);
print_r($mail['subject']);

此代码打印:
信件主题

If you want to access template variable you can send this variable as reference.

$foo = '';
$args['foo'] = &$foo;
$twig->render($template, $args);
...
echo $foo;

Example: (the goal is to make email body and subject in one template)

Twig_Autoloader::register();
$loader = new Twig_Loader_String();
$twig = new Twig_Environment($loader);
$tl = <<<EOL
{% set subject = "Subject of a letter" %}
Hello, {{ user }}

This is a mail body

-- 
Site
EOL;
$mail['to'] = '[email protected]';
$mail['subject'] = '';
$args = array(
    'user' => 'John', 
    'subject' => &$mail['subject']
);
$mail['message'] = $twig->render($tl, $args);
print_r($mail['subject']);

This code prints:
Subject of a letter

难理解 2024-12-19 19:09:42

您在 Twig 中设置的变量将设置到传递给 Twig_Template->display()$context 数组中。该数组按值传递,因此对其进行的任何修改都不会在外部 (PHP) 作用域中看到。

所以,,您不能在 PHP 中使用您在 Twig 中设置的变量。

Variables you set in Twig are set into the $context array you pass to Twig_Template->display(). This array is passed by value so any modifications to it will not be seen in the outer (PHP) scope.

So, no, you can't use the variables you set in Twig in PHP.

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