你如何让Smarty3与Symfony2合作?

时间:2012-02-03 20:17:45

标签: symfony smarty smarty3

我试图让Smarty 3作为Symfony 2的模板引擎。我试图安装这个软件包以让smarty 3工作:

https://github.com/noiselabs/SmartyBundle

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

  

致命错误:Class' NoiseLabs \ Bundle \ SmartyBundle'在第20行的&home; /home/kevin/workspace/Symfony/app/AppKernel.php中找不到

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

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

templating:      { engines: ['twig'] }

它会抛出此错误:

  

ServiceNotFoundException:服务"模板"依赖于不存在的>服务" 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');

2 个答案:

答案 0 :(得分:6)

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

该死的,文档错了。请更换一行:

new \NoiseLabs\Bundle\SmartyBundle(),

通过

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

答案 1 :(得分:1)

我手册有问题,但我解决了。使用以下步骤:

  1. Download Smarty并将其解压缩到您的htdocs目录中。结果你得到这样的filetree:htdocs / Smarty / libs / Smarty.class.php

  2. 通过ZIP-Button下载,在Github下载Smarty Bundle for Symfony 2。 创建以下文件夹层次结构:htdocs / yourSymfonyFolder / src / NoiseLabs / Bundle / SmartyBundle并将下载的zip解压缩为Smarty.php,SmartyBundle.php,SmartyEngine.php以及所有其他文件到htdocs / Symfony / src / NoiseLabs / Bundle / SmartyBundle。

  3. 转到您的Web服务器php.ini,如c:/xampp/php/php.ini,搜索该行:

    include_path = ".;c:\php\includes;"
    

    并添加c:\ yourHtdocsFolder \ Smarty \ libs \,结果如下:

    include_path = ".;c:\php\includes;c:\yourHtdocsFolder\Smarty\libs\"
    
  4. 打开文件htdocs / yourSymfonyFolder / app / AppKernel.php并添加:

    $bundles = array(
        [...]
        new NoiseLabs\Bundle\SmartyBundle\SmartyBundle(),
        [...]
    );
    
  5. 就是这样!在Controller src / Acme / HelloBundle / Controller / DefaultController.php中使用以下示例:

    public function indexAction($name)
    {
        return $this->render('AcmeHelloBundle:Default:index.html.tpl', array('name' => $name);
    }
    
  6. 在路径src / Acme / HelloBundle / Resources / views / Default / index.html.tpl中创建模板:

    Hello {$name}
    
  7. 调用URL www.yourWebsite / app_dev.php / hello / yourName。请注意,您必须在路由文件中注册URL。