如何在 Twig 中使用 PHP 模板引擎,而不是在 Silex 中使用 Twig 语法是在、而不、语法、模板

2023-09-07 00:27:21 作者:筑梦小丑

在 Silex 中,我可以使用 Twig 模板,但我想使用 Twig 的 PHP 引擎,而不是 Twig 语法.例如 本指南 描述了如何为 Symfony 而不是 Silex.

In Silex I am able to use Twig templates but I want to use the PHP engine of Twig, instead of the Twig syntax. For example this guide describes how to do it for Symfony but not Silex.

我的 Silex index.php 看起来像:

My Silex index.php looks like:

$app->register(new SilexProviderTwigServiceProvider(), array(
    'twig.path' => __DIR__.'/views',
));

$app->get('/', function() use ($app) {
    return $app['twig']->render('index.html.php', array(
        'name' => 'Bob',
    ));
});

我的 index.html.php 看起来像:

<p>Welcome to the index <?php echo $view->name; ?></p>

当我在浏览器中运行应用程序并查看源代码时,我看到了文字字符串 <?php echo $view->name;?> 没有被执行.

When I run the app in the browser and view the source, I see the literal string <?php echo $view->name; ?> which hasn't been executed.

我怀疑可能有一个 Twig 配置设置告诉它我想使用 PHP 样式模板.澄清一下,如果我改用 Twig 语法,例如:

I suspect there may be a Twig config setting to tell it I want to use the PHP style templates. To clarify, if I use the Twig syntax instead, e.g.:

<p>Welcome to the index {{ name }} </p>

然后它起作用了,我看到了名字 Bob,因此我知道这不是 Web 服务器或 PHP 配置问题.

Then it works and I see the name Bob, therefore I know this is not a web server or PHP config problem.

推荐答案

如果你想在 Silex 中模仿这种行为,你需要通过 Composer 安装 TwigBridge.然后像 Symfony 一样构建 模板 服务.

If you want to mimic this behaviour in Silex, you would need to install the TwigBridge via Composer. Then build the templating service the same way Symfony does.

我已经成功测试过这个解决方案.

This solution works as I have tested it successfully.

<?php

require __DIR__.'/vendor/autoload.php';

use SilexApplication;
use SymfonyComponentTemplatingPhpEngine;
use SymfonyComponentTemplatingTemplateNameParser;
use SymfonyComponentTemplatingLoaderFilesystemLoader;
use SymfonyComponentTemplatingDelegatingEngine;
use SymfonyBridgeTwigTwigEngine;

$app = new Application();

$app['debug'] = true;

// Register Twig

$app->register(new SilexProviderTwigServiceProvider(), array(
    'twig.path' => __DIR__.'/views',
));


// Build the templating service

$app['templating.engines'] = $app->share(function() {
    return array(
        'twig',
        'php'
    );
});

$app['templating.loader'] = $app->share(function() {
    return new FilesystemLoader(__DIR__.'/views/%name%');
});

$app['templating.template_name_parser'] = $app->share(function() {
    return new TemplateNameParser();
});

$app['templating.engine.php'] = $app->share(function() use ($app) {
    return new PhpEngine($app['templating.template_name_parser'], $app['templating.loader']);
});

$app['templating.engine.twig'] = $app->share(function() use ($app) {
    return new TwigEngine($app['twig'], $app['templating.template_name_parser']);
});

$app['templating'] = $app->share(function() use ($app) {
    $engines = array();

    foreach ($app['templating.engines'] as $i => $engine) {
        if (is_string($engine)) {
            $engines[$i] = $app[sprintf('templating.engine.%s', $engine)];
        }
    }

    return new DelegatingEngine($engines);
});


// Render controllers

$app->get('/', function () use ($app) {
    return $app['templating']->render('hello.html.twig', array('name' => 'Fabien'));
});

$app->get('/hello/{name}', function ($name) use ($app) {
    return $app['templating']->render('hello.html.php', array('name' => $name));
});

$app->run();

您至少需要这些依赖项才能在您的 composer.json 中实现这一点

You would need at least these dependencies to achieve this in your composer.json

"require": {
    "silex/silex": "~1.0",
    "symfony/twig-bridge": "~2.0",
    "symfony/templating": "~2.0",
    "twig/twig": "~1.0"
},