I searched everywhere but unfortunately without result. The case is as following:
I'm using a general Symfony 4 codebase with basic TWIG template files. Next to that i have multiple domains who refer to the codebase, because of that reason I want to set the path to my TWIG files in the controller from my codebase:
return $this->render("path/to/domain/and/twig/temp", "domain variables");
This is not working and I can't find how to change my TWIG path withing my controller. Can somebody help with this?
Thanks!
Symfony uses app/Resources/views/ directory for templates. If you want to change you can configure it in config.yml as below.
twig:
# ...
paths: ["%kernel.project_dir%/templates"]
Related
ok this might be a very strange thing.
it's not the first time I work a symfony project but:
I used the symfony generate:bundle command and after that I created a bundle. Lets call it "CrimeBundle".
I saw it made a folder inside the src/
It also made automatically a DefaultController and an index.html.twig file.
Now whenever I use:
return $this->render('CrimeBundle:Default:index.html.twig');
it doesn't work: I get the error:
Unable to find template "CrimeBundle:Default:index.html.twig" (looked into: /Users/admin/sites/solve/app/Resources/views, /Users/admin/sites/solve/vendor/symfony/symfony/src/Symfony/Bridge/Twig/Resources/views/Form).
however it works whenever I use the namespaced twig path like:
return $this->render('#Crime/Default/index.html.twig');
I like the first option, because on my other projects I use it too. None of them are the same version, currently I use: 3.4.1
Again the file is there, because it works with namespaced twig paths.
I can't understand why return $this->render('CrimeBundle:Default:index.html.twig'); wouldn't work as symfony generated this code.
According to this - https://symfony.com/doc/3.4/templating.html#referencing-templates-in-a-bundle
That's the only reference type the support for bundle templates
#BundleName/directory/filename.html.twig
If you go to docs for symfony 3.1 you'll see that was the last version that supported old reference
AcmeBlogBundle:Blog:index.html.twig
Maybe you can create a pull request for this file
SensioGeneratorBundle/bundle/DefaultController.php.twig
I have a problem with Symfony Flex and FOSUserBundle. I can't override default FOSUserBundle templates. I tried to do everything following the Symfony documentation, tutorials and nothing works. It's just like Twig won't take my layout.html.twig to render instead of default FOSUserBundle.
Templates dir tree looks like it should following new Symfony Flex structure:
- templates
- default
- FOSUserBundle
- views
- layout.html.twig
- base.html.twig
Maybe anyone has encountered similar problem with Symfony Flex.
Pull request that was merged in Symfony 3.4 at https://github.com/symfony/symfony/pull/24179 and available since 3.4-BETA1 and 4.0-BETA1 specifies that you need to place the template at templates/bundles/FOSUserBundle/layout.html.twig. PR description mentions exactly this usecase as its example.
In Symfony 3.3, it should work by placing the template in src/Resources/FOSUserBundle/views/layout.html.twig
templates
default
FOSUserBundle
layout.html.twig
it works for me
I don't anderstand why that doesn't work for me with symfony 4.0.8 in:
- templates
- FOSUserBundle
- layout.html.twig
but it works in
src
- Resources
- FOSUserBundle
- views
- layout.html.twig
I have a similar problem with symfony4 and fosUserBundle per to https://symfony.com/doc/current/templating/overriding.html,
for registration_content overriding i put my template file in templates\bundles\FOSUserBundle\Registration\register_content.html.twig
and it ready to works
hope work for you
I'm going to drop my answer here, because I am in a constant uphill battle with this bundle.
I copied the desired templates from the bundle in vendor and at first put them in,
project_root/src/Resources/FOSUserBundle
However after reading the documentation, it seems that it should be placed in project_root/templates/bundles/name_of_bundle/*.
However that didn't work because FOSUserBundle file structure is not adhering to symfonys conventions, you have to move things out of the views folder.
So rather than project_root/templates/bundles/name_of_bundle/views, move everything out of the views folder and into the parent name_of_bundle folder.
In your custom User bundle you must decalre the parent bundle
use Symfony\Component\HttpKernel\Bundle\Bundle;
class YourNameSpaceUserBundle extends Bundle
{
public function getParent()
{
return 'FOSUserBundle';
}
}
Note: Symfony Flex is alpha software; use it at your own risk
- templates
- FOSUserBundle
- layout.html.twig
Work for me too in Symfony 4.0.8 !
I'm using Symfony2.8 and when ever I use the command line for generating controllers and twig templates the templates are created under
MyBundle/Resources/views/home/home.html.twig
I want to follow the best practices suggested by symfony docs and have it inside of the
app/Resources/views/home/home.html.twig
I could just cut and paste the twig file and then change the {% extends %} if necessary, but then I would have lesser hair on my head because I would be pulling it out.
So what do I type in the prompt so that it generates the controller in the MyBundle as usual BUT the twig files would be under the global app/Resources/views folder
Thanks!
Edit: new extended question
After playing a little with the path for template generation i succeeded in placing the twig file inside the app/Resources/views/ folder.
Assuming that you have the standard architecture of a symfony2 app:
You could write the following path for the template when generator asks to specify the Templatename
Templatename (optional) [AppBundle:Post:get.html.twig]: ::../../../../app/Resources/views/Post/get.html.twig
i want to override the Friendsofsymfony Userbundle Templates. I found already the documentation:
http://symfony.com/doc/master/bundles/FOSUserBundle/overriding_templates.html
But i guess its not working in symfony3. The structur of the bundle is diffrent.
I think in the original its bundle/FOS/UserBundle/Ressources/views.
By me its vendor/friendsofsymfony/user-bundle/Resources/views.
I already try to copy this structur to the app folder. But it still not working.
thx for reading and maybe helping
See the Symfony documentation about overriding bundle templates:
When the AcmeBlogBundle:Blog:index.html.twig is rendered, Symfony actually looks in two different locations for the template:
app/Resources/AcmeBlogBundle/views/Blog/index.html.twig
src/Acme/BlogBundle/Resources/views/Blog/index.html.twig
To override the bundle template, just copy the 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.
I'm new to CakePHP and I need to integrate Twig for a project. First I installed the TwigView plugin (https://github.com/predominant/TwigView) and tried to follow the small set of instructions.
Installation
Plugin sources
I cloned the plugin repository in /app/Plugin/. A folder TwigView is created.
Twig sources
I placed Twig sources under /app/Plugin/TwigView/Vendor/ in a folder named Twig
Configuration
I added this to my /app/Config/bootstrap.php
CakePlugin::loadAll();
and this to AppController
public $viewClass = 'TwigView.Twig';
I also granted write privileges to everybody in /app/Plugin/TwigView/tmp/views
Problem 1
The application keeps asking for .ctp files, and I need to use my .tpl templates. I tried adding this in AppController
public $layout = 'default.tpl';
But it will complain, saying that it can't find default.tpl.ctp
Problem 2
How can I pass parameters to the twig templates from Cake controllers?