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
Related
I'm creating a command to generate some boiler code using the MakerBundle and a twig template (template.twig.php ).
I went throught the MakerBundle code on github and i saw that they only use php for templating.
Is it possible to use a twig template to generate my boiler code with MakerBundle ? if so, how to render it ?
IS it possible to change the template location ? because I saw comment saying that the templates should only live in resources/skeleton in the make bundle folder.
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"]
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 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?