template twig not found - symfony

I have a problem with a render function in my controller.
I have:
a bundle, named CoreBundle
a controller named DefaultController
in which I have an action named loginAction
in which I am trying to render a template twig with this line
return $this->render('FooCoreBundle::Default:layout.html.twig', array());
I am always having the same not found error for the template. I have tried with an other template twig it is the same problem. In my views folder I have a Accueil folder in which i have put the login.html.twig template I want to render.
I tried to replace it directly in views folder but no changes. Sorry for my english hope you can help. Thank You.

A worth-while reading piece of info from the documentation:
Symfony uses a bundle:directory:filename string syntax for
templates that live inside a bundle. This allows for several types of
templates, each which lives in a specific location.
Supposing:
FooCoreBundle is your bundle -> src/Foo/CoreBundle
Default is the template directory -> src/Foo/CoreBundle/Resources/views/Default
layout.html.twig is your file -> src/Foo/CoreBundle/Resources/views/Default/layout.html.twig
Then the route you should use would be 'FooCoreBundle:Default:layout.html.twig' without the :: you're using in 'FooCoreBundle::Default:layout.html.twig'
The :: refers to a base template specific to the bundle. If you used FooCoreBundle::layout.html.twig then your template should live at Resources/views/layout.html.twig inside FooCoreBundle.
Hope this clarifies things.

You should use
return $this->render('NeobeCoreBundle:Default:Accueil:layout.html.twig', array());
No double colon, add the Accueil subfolder

It is not really an answer, but it could be handy for those who arrives here with symfony 4.
In documentation authors says do not organize your business logic into bundles.
In modern Symfony applications, it's no longer recommended to organize your business logic using bundles.
Of course you can, just not recommended. See here.

Related

How would I go extending timber to be able to use handles as references in includes?

We use fractal.js with a twig engine to prototype websites. Fractal.js brings a handy functionality to be able to use handles in includes.
So instead of writing {% include 'templates/components/teaser/basicTeaser.twig' %}, you can just write {% include '#basicteaser' %}. See: https://fractal.build/guide/core-concepts/naming.html#referencing-other-items
Of course that relies on unique components names, which I am happy to have anyway.
Could anybody point me in the right direction on how to extend timber or twig to use such handles?
Many thanks!
Twig already offers the option to load templates from an array of directories rather than just one.
$loader = new \Twig\Loader\FilesystemLoader([$templateDir1, $templateDir2]);
Plus, before passing the loader to the environment object, you can add paths for custom namespaces, i.e.:
$loader->addPath($templateDir, 'admin');
Then you can use you namespace like this:
$twig->render('#admin/index.html', []);
https://twig.symfony.com/doc/3.x/api.html#built-in-loaders
I know this is an old question and this might not be relevant to the OP anymore, but we needed the same thing and developed a WordPress plugin to bridge Timber + Fractal. You can export your Fractal components to the components-map.json file, then the loader will read the map and convert those component names to file paths.

Drupal 8 add javascript and pass data in a hook with custom module

I am building a custom module with Drupal 8. One of the requirements of the module is that it sets up a javascript file based on the configuration settings, which the user of the module sets up in module configuration.
The javascript which needs to be added to the page depends on the configuration settings. Hence I can not add it using Libraries as mentioned in this article: Adding assets to Drupal module.
I first implemented it using a block. I use Twig templates to pass the configuration variables in PHP to the twig file, and in the twig file, I have a tag to add the javascript based on the config variables. Refer Using twig templates.
The problem with this approach is that user needs to add the block on the page, and there is no UI facing element on that block. I also find it very messy.
Is there a cleaner way to add my javascript using hook and pass variables to it? I looked around and found hook_install and hook_page_attachments. So I can add Javascript, but don't know how I can pass any php variables to it.
I am new to Drupal development. Any help with this is really appreciated.
TL;DR I need to find a way to add Javascript using Drupal hook and pass some PHP variables to it.
Use in hook_page_attachments:
$attachments['#attached']['drupalSettings']['myvar'] = $myVariable;
Then in your js (I assume you already know how to attach js library):
(function ($, Drupal, drupalSettings) {
Drupal.behaviors.my_custom_behavior = {
attach: function (context, settings) {
var strings = drupalSettings.myvar;
...
}
}
})(jQuery, Drupal, drupalSettings);
See nice Acquia tutorial for more examples.

Correct way to override default template in Sonata

I'm a bit confused as I've seen a few varying methods posted online.
I have a bundle created with easyextends in src/Application/Sonata/SonataMediaBundle, which extends the SonataMediaBundle in the vendors.
The default template displays
This is the gallery index template. Feel free to override it.
This file can be found in SonataMediaBundle:Gallery:index.html.twig.
so I've added src/Application/Sonata/SonataMediaBundle/Resources/views/Gallery/index.html.twig in my bundle
and this works and overrides, so why all these various other ways like How to override Sonata Media Bundle templates?
You are using the correct way. To quote from symfonys How to Override Templates from Third-Party Bundles
To override the bundle template, just copy index.html.twig template from the bundle to app/Resources/AcmeBlogBundle/views/Blog/index.html.twig (the app/Resources/AcmeBlogBundle directory won't exist, so you'll need to create it). You're now free to customize the template.
And for more detailed/ complex overriding behaviour have a look at How to Use Bundle Inheritance to Override Parts of a Bundle
I wouldn't worry to much about other solutions unless you are not able to get the results you require using this method but I cant think of one.

Symfony include static html in template

I just started to learn how to use Symfony which i believe is straight forward but i have a little problem with the templating engine. I want to include a static HTML fragment in one of my twig templates in Symfony (2.5.6). For now i created a static subfolder inside the resources directory (this might change but it definitely won't be inside the view folder). However i can't get this done, i always end up with an unable to find template error. I find the documentation on this subject a bit sparse (see http://symfony.com/doc/current/book/templating.html#including-other-templates) also the twig docs can't help me out on this one. I'm not even sure if i can use the magic #Bundle notation or if i have to reside in the view folder as ../ notation is not allowed within the include tag.
I tried the following (and a couple of variations):
{{ include('#FashionMediaBundle/Resources/static/pricing.html') }}
I guess symfony cannot handle raw html in include but i could use a php template without any template tags as well, so the question is just how to specify a location outside the view folder.
I know of http://github.com/kgilden/KGStaticBundle which will probably solve my issue but i can't believe that is not achievable with the default configuration.
EDIT: i just tried to include a regular template file from the very same directory specifying just the name of the template file (as done in the docs, see link above). Still i get an error also complaining it expects bundle:section:template.format.engine as its format. Is there an error in the docs?
Looks like the poster found the solution while I was typing. I guess I'll leave this here for just a bit.
Creating and using twig namespaces is discussed here: http://symfony.com/doc/current/cookbook/templating/namespaced_paths.html
By default, each bundles get's it's own namespace pointing to the views directory. You can add additional directories by adjusting app/config.yml
twig:
debug: %kernel.debug%
strict_variables: %kernel.debug%
paths:
"%kernel.root_dir%/../../cerad2/src/Cerad/Bundle/FashionMediaBundle/Resources/static": FashionMedia
Load the template with: '#FashionMedia/pricing.html'
I won't go into all the details but you can also use a compiler pass(http://symfony.com/doc/current/cookbook/service_container/compiler_passes.html) to add additional paths from inside the bundle itself without having to adjust config.yml:
class Pass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
$bundleDirAction = $container->getParameter('cerad_api01__bundle_dir') . '/Action';
$twigFilesystemLoaderDefinition = $container->getDefinition('twig.loader.filesystem');
$twigFilesystemLoaderDefinition->addMethodCall('addPath', array($bundleDirAction, 'CeradApi01'));
}
}
Try this:
{{ include('FashionMediaBundle:Resources:static:pricing.html') }}
If you want to put this file in another directory I suggest you to use a symbolic link (I don't know if Twig can include a file which is not in the Resources directory).
I found the Solution!
In the twig configuration you can set paths and assign a name to them.
twig:
[... twig options here ...]
paths:
"%kernel.root_dir%/../[path to static]": static_pages
And then you can include them in
{% include "#static_pages/pricing.html.twig" %}

define custom filesystem path for twig templates

I've already read How does Symfony2 detect where to load templates from? and another ton of articles on the web
The question was quite similar to mine, but the answer wasn't comprehensive.
I need to override a bundle-defined template inside another custom bundle. In other words I want specify another path where symfony have to look before looking in app/Resources when loading templates.
Workflow should be something like this:
first: /src/My/Bundle/Resources/views/example.html.twig (or /src/My/Bundle/OriginalBundle/views/example.html.twig)
then: /app/Resources/OriginalBundle/views/example.html.twig
finally: /src/Original/Bundle/Resources/views/example.html.twig
The standard app/Resources -> src/Original/Bundle isn't enough.
sorry for my poor english and thank you
There's a native feature to do exactly what you want in a nice way. Escentially you can add a twig namespace to the twig configuration in app/config.yml like this:
twig:
# ...
paths:
"%kernel.root_dir%/../vendor/acme/foo-bar/templates": foo_bar
This creates an alias to the folder vendor/acme/foo-bar/templates and then you can use it to render your templates either from the controllers:
return $this->render(
'#foo_bar/template.html.twig',
$data
);
or from other twig templates
{% include '#foo_bar/template.html.twig' %}
Source: official cookbook http://symfony.com/doc/current/cookbook/templating/namespaced_paths.html
To add a directory for twig templates I do this:
chdir(__DIR__);
$this->container->get('twig.loader')->addPath('../../../../web/templates/', $namespace = '__main__');
this allows me to put twig templates in a folder called 'templates' in the web folder and symfony 2.3 has no issues loading them.
The class responsible for loading twig templates is stored at the service id twig.loader, which by default is an instance of Symfony\Bundle\TwigBundle\Loader\FilesystemLoader, and the class for locating templates is stored at the service id templating.locator and by default is an instance of the class Symfony\Bundle\FrameworkBundle\Templating\Loader\TemplateLocator (which itself is injected as the first parameter to twig.loader)
So, in theory, all you would need to do is write your own class that implements Symfony\Component\Config\FileLocatorInterface (or extends Symfony\Bundle\FrameworkBundle\Templating\Loader\TemplateLocator) and then inform the configuration to use your class (which I was going to look up how to do this for you, but Symfony's websites are down right now)

Resources