symfony - 如何让 Smarty3 与 Symfony2 一起工作?

标签 symfony smarty smarty3

我正在尝试让 Smarty 3 作为 Symfony 2 的模板引擎工作。我正在尝试安装此 bundle 以使 smarty 3 工作:

https://github.com/noiselabs/SmartyBundle

它安装得很好,但是当我按照安装说明中的说明将其添加到 AppKernal 时,出现以下错误:

Fatal error: Class 'NoiseLabs\Bundle\SmartyBundle' not found in >/home/kevin/workspace/Symfony/app/AppKernel.php on line 20

其中第 20 行位于 registerBundles() 内部: 新的 NoiseLabs\Bundle\SmartyBundle(),

我遇到的第二个可能相关的问题是,在 app/config/config.yml 中,如果我将“smarty”添加到模板引擎数组中:

templating:      { engines: ['twig'] }

它抛出这个错误:

ServiceNotFoundException: The service "templating" has a dependency on a non-existent >service "templating.engine.smarty".

我意识到 twig 带有 symfony,但对于这个项目我需要使用 smarty。我是否遗漏了什么或者有其他解决方案吗?

这是内核代码:

<?php

use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\Config\Loader\LoaderInterface;

class AppKernel extends Kernel
{
    public function registerBundles()
    {   
        $bundles = array(
            new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
            new Symfony\Bundle\SecurityBundle\SecurityBundle(),
            new Symfony\Bundle\TwigBundle\TwigBundle(),
            new Symfony\Bundle\MonologBundle\MonologBundle(),
            new Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle(),
            new Symfony\Bundle\DoctrineBundle\DoctrineBundle(),
            new Symfony\Bundle\AsseticBundle\AsseticBundle(),
            new Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle(),
            new JMS\SecurityExtraBundle\JMSSecurityExtraBundle(),
//new NoiseLabs\Bundle\SmartyBundle(),
            new Blog\EntryBundle\BlogEntryBundle(),
        );  

        if (in_array($this->getEnvironment(), array('dev', 'test'))) {
            $bundles[] = new Acme\DemoBundle\AcmeDemoBundle();
            $bundles[] = new Symfony\Bundle\WebProfilerBundle\WebProfilerBundle();
            $bundles[] = new Sensio\Bundle\DistributionBundle\SensioDistributionBundle();
            $bundles[] = new Sensio\Bundle\GeneratorBundle\SensioGeneratorBundle();
        }   

        return $bundles;
    }   

    public function registerContainerConfiguration(LoaderInterface $loader)
    {   
        $loader->load(__DIR__.'/config/config_'.$this->getEnvironment().'.yml');
    }   
}

这是自动加载器代码:

<?php

use Symfony\Component\ClassLoader\UniversalClassLoader;
use Doctrine\Common\Annotations\AnnotationRegistry;

$loader = new UniversalClassLoader();
$loader->registerNamespaces(array(
    'Symfony'          => array(__DIR__.'/../vendor/symfony/src', __DIR__.'/../vendor/bundles'),
    'Sensio'           => __DIR__.'/../vendor/bundles',
    'JMS'              => __DIR__.'/../vendor/bundles',
    'NoiseLabs'        => __DIR__.'/../vendor/bundles',
    'Doctrine\\Common' => __DIR__.'/../vendor/doctrine-common/lib',
    'Doctrine\\DBAL'   => __DIR__.'/../vendor/doctrine-dbal/lib',
    'Doctrine'         => __DIR__.'/../vendor/doctrine/lib',
    'Monolog'          => __DIR__.'/../vendor/monolog/src',
    'Assetic'          => __DIR__.'/../vendor/assetic/src',
    'Metadata'         => __DIR__.'/../vendor/metadata/src',
));
$loader->registerPrefixes(array(
    'Twig_Extensions_' => __DIR__.'/../vendor/twig-extensions/lib',
    'Twig_'            => __DIR__.'/../vendor/twig/lib',
));

// intl
if (!function_exists('intl_get_error_code')) {
    require_once __DIR__.'/../vendor/symfony/src/Symfony/Component/Locale/Resources/stubs/functions.php';

    $loader->registerPrefixFallbacks(array(__DIR__.'/../vendor/symfony/src/Symfony/Component/Locale/Resources/stubs'));
}

$loader->registerNamespaceFallbacks(array(
    __DIR__.'/../src',
));
$loader->register();

AnnotationRegistry::registerLoader(function($class) use ($loader) {
    $loader->loadClass($class);

// Swiftmailer needs a special autoloader to allow
// the lazy loading of the init file (which is expensive)
require_once __DIR__.'/../vendor/swiftmailer/lib/classes/Swift.php';
Swift::registerAutoload(__DIR__.'/../vendor/swiftmailer/lib/swift_init.php');

最佳答案

感谢您使用 SmartyBundle(我是创建者)。

该死,文档是错误的。请替换该行:

new \NoiseLabs\Bundle\SmartyBundle(),

new \NoiseLabs\Bundle\SmartyBundle\SmartyBundle(),

关于symfony - 如何让 Smarty3 与 Symfony2 一起工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9134946/

相关文章:

php - Drupal 8 - 以编程方式禁用或启用主导航上的链接

symfony - 多个模板中的 Twig Assetic 样式表

php - Sylius:如何在自定义 ProductRepository 中注入(inject)(请求)参数?

php - PHP 中的 mvc+smarty 是如何工作的?

php - 错误 : Database not selected

php - Symfony2/Doctrine2 (MySQL) 更新后未立即显示在选择上

php - Smarty 中的动态变量名称

php - Smarty,页面/部分页面没有缓存

php - smarty -sql 查询 - 其中 id 子句是另一个查询的结果

php - 如何在 Smarty 中使用一组 PHP 函数