2

I'm trying to get Smarty 3 to work as a template engine for Symfony 2. I'm trying to install this bundle to get smarty 3 to work:

https://github.com/noiselabs/SmartyBundle

It installs just fine, but when I add it to the AppKernal as it says in the installation instructions, I get the following error:

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

where line 20 is inside registerBundles(): new NoiseLabs\Bundle\SmartyBundle(),

The second, possibly related problem I have is that in app/config/config.yml, if I add 'smarty' to the templating engine array:

templating:      { engines: ['twig'] }

it throws this error:

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

I realize that twig comes with symfony but for this project I need to use smarty. Am I missing something or is there another solution to this?

Here is the Kernel code:

<?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');
    }   
}

Here is the auto loader code:

<?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');
j0k
  • 22,600
  • 28
  • 79
  • 90
ContextSwitch
  • 2,830
  • 6
  • 35
  • 51

2 Answers2

6

Thanks for using SmartyBundle (I'm the creator).

Damn it, the documentation is wrong. Please replace the line:

new \NoiseLabs\Bundle\SmartyBundle(),

by

new \NoiseLabs\Bundle\SmartyBundle\SmartyBundle(),
noisebleed
  • 1,299
  • 2
  • 12
  • 26
  • Lol, i just debug this a minute ago. I sent a PR on the repository to fix the issue. I was about to answer :) – Matt Feb 04 '12 at 20:14
  • Thanks! That did it, I'm getting a different error now that has to do with the include path, I'm about to look into that. – ContextSwitch Feb 04 '12 at 22:20
  • Got it working, I didn't realize that Smarty3 came separately from the bundle. Put it in the include path and its working fine. Thanks again! – ContextSwitch Feb 06 '12 at 17:21
  • Hi, is there a roadmap to release stable version for Smarty bundle for Symfony 4.4, we currently using dev version – Naveen Kumar Jan 20 '20 at 06:09
1

I had problems with the Manual but i resolved it. Use the following steps:

  1. Download Smarty and unpack it in your htdocs Directory. As result you get a filetree like this: htdocs/Smarty/libs/Smarty.class.php

  2. Download the Smarty Bundle for Symfony 2 on Github, by downloading over the ZIP-Button. Create the following folder-hierarchy: htdocs/yourSymfonyFolder/src/NoiseLabs/Bundle/SmartyBundle and unzip the downloaded zip with Smarty.php, SmartyBundle.php, SmartyEngine.php and all other files to htdocs/Symfony/src/NoiseLabs/Bundle/SmartyBundle.

  3. Go to your Webservers php.ini like c:/xampp/php/php.ini, searching for the line:

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

    and adding c:\yourHtdocsFolder\Smarty\libs\ with the totally result:

    include_path = ".;c:\php\includes;c:\yourHtdocsFolder\Smarty\libs\"
    
  4. Open the File htdocs/yourSymfonyFolder/app/AppKernel.php and add:

    $bundles = array(
        [...]
        new NoiseLabs\Bundle\SmartyBundle\SmartyBundle(),
        [...]
    );
    
  5. That´s it! Use the following Example in your Controller src/Acme/HelloBundle/Controller/DefaultController.php:

    public function indexAction($name)
    {
        return $this->render('AcmeHelloBundle:Default:index.html.tpl', array('name' => $name);
    }
    
  6. Create a Template in your Path src/Acme/HelloBundle/Resources/views/Default/index.html.tpl:

    Hello {$name}
    
  7. call the URL www.yourWebsite/app_dev.php/hello/yourName. Note, that your have to register the URL in your Routing-File.

Florent
  • 12,310
  • 10
  • 49
  • 58