Unable to find template Bundle:Default:index.html.twig - symfony

I'm having some trouble runnning Symfony. In fact, it can't find the default twig template. I didn't touch the code at all, juste generated my bundle and trying to access /web/app_dev.php.
My template is in
/src/OC/PlatformBundle/Resources/views/Default/index.html.twig.
Symfony looked into these locations, but not where my template actually is.
/app/Resources/views
/vendor/symfony/symfony/src/Symfony/Bridge/Twig/Resources/views/Form
And the DefaultController.php indexAction() looks ok :
public function indexAction(){
return $this->render("OCPlatform:Default:index.html.twig");
}
If any of you have ever faced this kind of issue or have any idea where the problem comes from, I thank you in advance for your help.
Arthur

I've same problem and i resolve it with this route :
public function indexAction(){
return $this->render("#OCPlatform/Default/index.html.twig");
}
Edit ":" with "/" and it work.
Maybe can help other developper

If you want to keep using the 'OCPlatform:Default:index.html.twig' format for templates then edit your app/config/config.yml file:
#app/config/config.yml
framework:
templating:
engines: ['twig']
This fixed my problem. You can check this link
Symfony 3.4 Use view inside my bundle

Following the docs(Symfony >=3.4) you should write the path to the template in the controller like this:
public function indexAction(){
return $this->render("#OCPlatform:Default:index.html.twig");
}

This worked for me:
return $this->render('#HomeBundle/Default/index.html.twig');
Hope it hope :)

Referencing Templates in a Bundle¶
If you need to refer to a template that lives in a bundle, Symfony uses the Twig namespaced syntax (#BundleName/directory/filename.html.twig). This allows for several types of templates, each which lives in a specific location:
#AcmeBlog/Blog/index.html.twig: <br>This syntax is used to specify a template for a specific page. The three parts of the string, each separated by a slash (/), mean the following:<br>
#AcmeBlog: is the bundle name without the Bundle suffix. This template lives in the AcmeBlogBundle (e.g. src/Acme/BlogBundle);<br>
Blog: (directory) indicates that the template lives inside the Blog subdirectory of Resources/views/;<br>
index.html.twig: (filename) the actual name of the file is index.html.twig.

In symfony 4 use this :
return $this->render("#Platform/Default/index.html.twig");

Related

What config when controller is other folder and template path is inadequate

I work with Symfony 4.2
I divided my dir structure, and every part of app is in other folder.
Ex. admin part code is in src/Admin/...
- src/Admin/Controller
etc.
When I set #Template in controller method symfony tell me to situate templates in templates/
But I want to it placed in teplates/Admin/
What I should change in config.
If is other solution than set every action path in #Template(...) ?
Taken from the docs:
# config/packages/twig.yaml
twig:
# ...
paths: ["%kernel.project_dir%/resources/views"]
https://symfony.com/doc/current/configuration/override_dir_structure.html#override-the-templates-directory
This might also be interesting for you:
https://symfony.com/doc/current/templating/namespaced_paths.html#multiple-paths-per-namespace

symfony shortCodes bundle

i have used laravel shortcodes (https://github.com/webwizo/laravel-shortcodes) with success, it is a nice bundle.
the question is: is there any bundle like this for symfony 3.x?
what this essentially do is take formatted string like [myString] with some optional parameters like [myString param1="abc" param2="def" ...] from a rendered blade / twig and looks for a controller / function etc. to resolve "myString". it passes the params to the controller, and the whole [...] stuff is replaced with the returned output. and it is done recursivly, so the result may contain another [myString2 ...] and so on. this is very useful in CMS building.
does anyone know anything like this for symfony?
There is a bundle that looks exactly what you are searching for:
https://github.com/mweimerskirch/MWShortcodeBundle

Where is #Twig defined in symfony

I am new to symfony.While going through an error template I came across value a value like
#Twig/Exception/traces_text.html.twig
Just wanted to know where is #Twig defined in symfony framework
Just google twig traces_text.html.twig and you'll see the file here: https://github.com/symfony/twig-bundle/blob/master/Resources/views/Exception/traces_text.html.twig
symfony/twig-bundle/Resources/views/Exception/traces_text.html.twig
Or use your IDE and search the vendor/ folder for the traces_text.html.twig file until you find it.

Oneupuploaderbundle : change default directory

Can you give best practices in order to change the default directory of uploaded files (web/uploads/gallery) ?
It would be great if you can use for example some form data (album slug if you are uploading photos into an album) or just simple thing like the year or the month because currently all uploaded files are going in a same directory which is hard to maintain..
Thanks
This question was answered a few times in the bug tracker. Basically you create a custom namer and include all your dynamic data to the file path. Sub-directories will be created automatically.
namespace AppBundle\Uploader\Naming;
use Oneup\UploaderBundle\Uploader\File\FileInterface;
use Oneup\UploaderBundle\Uploader\Naming\NamerInterface;
class CatNamer implements NamerInterface
{
public function name(FileInterface $file)
{
$product = //...
return sprintf('%d/%s, $product->getId(), grumpycat.jpg');
}
}
This way you can create your own folder structure.

Drupal 6 preprocess function for custom template

I have a custom template file I'm using for a page.
Say the page is www.mydomain.com/hello-world
The content of this page is taken from a template file called hello-world.tpl.php
Now I need to have a variable prepared in advance for that page, so I took a look at how core modules achieve this and tried to implement in the same way, only the variables are always null..
for hello-world.tpl.php I created in my module file a function called:
function template_preprocess_hello_world(&$variables) {
$variables['test'] = "test";
}
As I said $test doesn't get any value on www.mydomain.com/hello-world (I receive NULL when var dumping it)
I spent an hour or so double checking that I don't have any typos or anything like that.
Is what I'm doing worng??
Try renaming the function to mymodulename_preprocess_hello_world(&$variables). Don't forget to clear the cache as well.

Resources