noiselabs / SmartyBundle

Smarty3 template engine bundle for Symfony
http://smartybundle.readthedocs.io/
GNU Lesser General Public License v3.0
51 stars 36 forks source link

Using of string resource creates ./template_c directory #45

Open belka-ew opened 9 years ago

belka-ew commented 9 years ago

If I don't use a file resource but render a template from the string, smarty creates template_c not in the var/cache/prod/smarty/template_c but in the current directory. Minimal example:

<?php
namespace AppBundle\Tests\Controller;

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class TemplatingResourceTest extends WebTestCase
{
    public function testString()
    {
        $client = static::createClient();

        $smarty = $client->getContainer()->get('smarty');

        $template = $smarty->fetch('string:{assign var="var1" value="testValue"}{$var1}');

        $this->AssertFileNotExists('template_c');
    }
}

Running this will fail and will create template_c/ in the symfony root directory. Running similar code from the controller will create template_c/ in the web/ directory (so web/template_c).

What is the problem? 'compile_dir' is set in NoiseLabs\Bundle\SmartyBundle\DependencyInjection\Configuration and it happens in the constructor of NoiseLabs\Bundle\SmartyEngine. So if I call $this->get('smarty') before $this->render(...) in controller I get unconfigured smarty instance. My suggestion would be to create a smarty service around the \Smarty and set smarty configuration inside of this service. If this solution is acceptable I can create a pull request.

belka-ew commented 9 years ago

Ok, I see. There is already such "wrapper": SmartyEngine and I should use the SmartyEngine service instead of Smarty. $this->get('templating.engine.smarty')->getSmarty() should give the expected result.

Another thing about services: Can it be that parameters "smarty.class_file" and "smarty.cache_warmer.class" aren't in use and can be removed from smarty.xml?