Twig Loader prependPath not working - symfony

I'm trying to use theme in symfony 2.5.6 following this tutorial : http://www.ahmed-samy.com/symofny2-twig-multiple-domains-templating/
So I tried this in the TemplateListener (declared as service)
$path = dirname(__DIR__) . '/Resources/themes/' . $theme . '/views';
$this->twig->getLoader()->prependPath($path, 'MyWebsiteFront');
But this does not work, the template "MyWebsiteFront:Index:index.html.twig" is not found (but it exists at C:\www\MyWebsiteFront\src\MyWebsiteFront\Resources\themes\mytheme\views\Index\index.html.twig, the path generated by $path)
Any ideas ?

According to the docs, When you render the template you need to reference it with the namespace you aliased to that path e.g.
#MyWebsiteFront/Index/index.html.twig
When using the setPaths(), addPath(), and prependPath() methods,
specify the namespace as the second argument (when not specified,
these methods act on the "main" namespace):
$loader->addPath($templateDir, 'admin'); Namespaced templates can be
accessed via the special #namespace_name/template_path notation:
$twig->render('#admin/index.html', array());
NOTE: The namespace is not required.

Related

Symfony 3 Translations Custom Domain

I'm trying to add custom domain into the project.
I have regions.locale.yaml file.
I'm trying load it in twig:
{{'united.kingdom'|trans|raw}}
But this doesn't work.
I think it has to be somehow declared that this file exists.
I found this in documentation:
// ...
$translator->addLoader('xlf', new XliffFileLoader());
$translator->addResource('xlf', 'messages.fr.xlf', 'fr_FR');
$translator->addResource('xlf', 'admin.fr.xlf', 'fr_FR', 'admin');
$translator->addResource(
'xlf',
'navigation.fr.xlf',
'fr_FR',
'navigation'
);
But where should I put this to declare my regions.locale.yaml files globally?
Thanks
If you are using Symfony Standard, you don't have to declare your translation files, you just put them in app/Resources/translations.
The key is that when you want to translate using your custom domain, you just specify your domain, like this :
{{'united.kingdom'|trans({}, 'regions')|raw }}
or somewhere else in your code :
$translator->trans('united.kingdom', [], 'regions');

Symfony 2.5.6 error InvalidArgumentException: The service definition "event_dispatcher" does not exist

I'm trying to build my first Compiler Pass in Symfony 2. For now, I'm just trying to get the core event_dispatcher service from FrameWorkBundle inside a SampleBundle, but I get this error :
error InvalidArgumentException: The service definition "event_dispatcher" does not exist.
Here is the code for my compiler :
<?php
namespace Me\SampleBunlde\DependencyInjection\Compiler;
use Symfony\Component\DependencyInjection\Compiler;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
class RegisterListenersPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
$definition = $container->getDefinition('event_dispatcher');
}
}
?>
I'm a bit surprised since I'm following step by step a professionnal Symfony book who assures me that I will find this service with that id.
I've done some researches about that, and I discovered that only the debug.event_dispatcher service was avaible. Then I checked for aliases and saw that there was a private Alias named 'event_dispatcher' pointing to debug.event_dispatcher. So I'm really confused about all that. And I'm wondering :
Why is the Alias private ? Do I need to set him Public or is it the wrong way ?
Why Symfony does not automatically interprets my event_dispatcher call ?
Thank you for your help !
Use findDefinition() instead of getDefinition(). findDefinition also looks for aliases.

How to use symfony2 twig extension without symfony2

I want to use some of the builtin symfony2 extensions(e.g:humanize,yaml_dump) for twig for a website not developed in symfony but using a twig engine.how can I do that?
The symfony/twig-bridge package provides the symfony-specific twig extensions.
These include i.e. the YamlExtension that provides the yaml_dump filter and the FormExtension that provides the humanize filter.
The extensions can be found in the Extension folder.
I strongly advise you to install the package via composer to get the package's dependencies automatically.
composer require symfony/twig-bridge:~2.3
Further composer will automatically register the classes in the autoloader (vendor/autoload.php) for you.
Now you just need to add the extensions to twig as described in the documentation.
$twig->addExtension(new \Symfony\Bridge\Twig\Extension\YamlExtension());
// ...
A complete example, with an extension class and a quick extension (a new filter) :
<?php
require_once("vendor/autoload.php");
$loader = new Twig_Loader_String();
$twig = new Twig_Environment($loader);
// here we add the extension class (taken from #nifr answer)
$twig->addExtension(new \Symfony\Bridge\Twig\Extension\YamlExtension());
// here we add a new filter quickly
$filter = new Twig_SimpleFilter('paragraph', function ($argument) {
return "<p>{$argument}</p>";
}, array('pre_escape' => 'html', 'is_safe' => array('html')));
$twig->addFilter($filter);
// demo
echo $twig->render('{{ "hello" | paragraph }}');

Symfony 1.2 to 2.3 migration

I've got a pretty big Symfony 1.2 project to migrate.
First, I modified my .htaccess so I can have some pages handled by Symfony 2.
What I'd like to do, to make the migration smoother, is to be able to render some SF2 action/templates/methods/... inside SF1.
I added the autoloader to the SF1 app, so I can access to twig rendering methods and other stuff.
But how can I call a SF2 action ?
For example, if I want to migrate only the footer first, I would also need some php methods, not only rendering. That was previously in SF1 component, where should it be now ?
If you've got any suggestion about the way of migrating, don't hesitate !
EDIT 1 :
Apparently, the only way to do something like that is to render a full twig template, and/or in this template call some other partial twig templates with render(url, params).
Here is my SF1 code to be able to render twig templates :
public static function getTwig()
{
require_once __DIR__.'SF2_PATH/vendor/twig/extensions/lib/Twig/Extensions/Autoloader.php';
Twig_Autoloader::register();
$loader = new Twig_Loader_Filesystem( __DIR__.'SF2_PATH/sf2/src/VENDOR/BUNDLE/');
$twig = new Twig_Environment($loader, array(
'cache' => __DIR__.'SF2_PATH/sf2/app/cache/dev/twig',
));
return $twig;
}
And so :
$twig->loadTemplate('header.html.twig');
EDIT 2 :
That doesn't seem to work, if in a twig template I try to render an other one with {{render(controller('BUNDLE:CONTROLER:ACTION', {})) }} for example Twig_Error : The function "controller" does not exist. And if I try to render the url Unknown tag name "render".
I guess Symfony 2 twig functionalities are not loaded, how can I do that ?
EDIT 3 :
Ok, now I can do it, but I've got the following message...
Twig_Error_Runtime An exception has been thrown during the rendering
of a template ("Rendering a fragment can only be done when handling a
master Request.") in ...
EDIT : I solved it !
Here is my full bootstrap method to render a Twig template and be able to use some Symfony 2 functionalities, in Symfony 1.
$loader = require_once __DIR__.'/../../../sf2/app/bootstrap.php.cache';
Debug::enable();
require_once __DIR__.'/../../../sf2/app/AppKernel.php';
$kernel = new AppKernel('dev', true);
$kernel->loadClassCache();
Request::enableHttpMethodParameterOverride();
$request = Request::createFromGlobals();
$kernel->boot();
$kernel->getContainer()->enterScope('request');
$kernel->getContainer()->set('request', new \Symfony\Component\HttpFoundation\Request(), 'request');
$this->container = $kernel->getContainer()->get('twig');

Symfony2: class MapClassLoader not found

I'm trying to use MapClassLoader in autoload.php but for some reason I keep getting errors saying
Class 'Symfony\Component\ClassLoader\MapClassLoader' not found in ...\autoload.php
autoload.php:
<?php
use Symfony\Component\ClassLoader\UniversalClassLoader;
use Symfony\Component\ClassLoader\MapClassLoader;
use Doctrine\Common\Annotations\AnnotationRegistry;
$loader = new UniversalClassLoader();
$loader->registerNamespaces(array(
//some values
));
$mapLoader = new MapClassLoader(array(
//some values
));
$mapLoader->register();
I double checked and MapClassLoader.php does exist in Symfony\Component\ClassLoader
Any idea why is it happening? :/
autoload.php is a file that configures autoloading for classes so autoloading isn't available in it and you need to include any files manually:
require_once __DIR__.'/../vendor/symfony/src/Symfony/ClassLoader/MapClassLoader.php';
Why is UniversalClassLoader available without require? Because symfony uses bootstrap file for system files to reduce file loading overhead.

Resources