8000 Example to include templates doesn't work by jxmallett · Pull Request #5318 · symfony/symfony-docs · GitHub
[go: up one dir, main page]

Skip to content

Example to include templates doesn't work #5318

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed

Example to include templates doesn't work #5318

wants to merge 1 commit into from

Conversation

jxmallett
Copy link
Contributor

The PHP example for including templates within templates doesn't work.

Without the colons, the engine looks for article_details.html.php in app\Resources\ instead of app\Resources\views\article

The PHP example for including templates within templates doesn't work.

Without the colons, the engine looks for `article_details.html.php` in `app\Resources\` instead
8000
 of `app\Resources\views\article`
@jxmallett
Copy link
Contributor Author

Honestly, I think the example should be re-written as something like MyBundle:Article:article_details.html.php but I'm very new to Symfony so I'm not confident doing a more serious update of the documentation, sorry.

@javiereguiluz
Copy link
Member

@jxmallett thanks for opening this issue to improve the Symfony docs.

In Symfony, there are three main ways to refer to a template:

// namespaced syntax
@MyBundle/Article/article_details.html.php

// dotted syntax
MyBundle:Article:article_details.html.php

// simple syntax
article/article_details.html.php

These three syntaxes work right and you can even mix all of them in the same application (obviously this is not recommended). However, in the documentation we decided a few months ago to use the simple syntax and forget about the dotted syntax used traditionally in the Symfony world.

The simple syntax complies with the Symfony Best Practices and we reserve the dotted syntax just for third-party bundles.

@jxmallett
Copy link
Contributor Author

Thanks for the info, @javiereguiluz. I was getting a little confused seeing the different syntax. Also interesting to know that Symfony Best Practices recommends putting all application templates in app/ rather than in the bundle.

I still think there's an issue though, and it's possibly a bug in Symfony itself. The templating and Best Practice docs say that I should, for example, put my template in app/Resources/views/article/article_details.html.php and refer to it by it's simple name Article/article_details.html.php.

Firstly, there's a difference in the capitalization of Article but it seems I can ignore that (on Windows at least).

My real issue is that the simple name Article/article_details.html.php actually resolves to app/Resources/article/article_details.html.php but all the documentation says it should resolve to app/Resources/views/article/article_details.html.php. I think it should actually resolve to the views directory as this would make it consistent with the dotted and namespaced syntax. This would make this a bug in Symfony itself, no?

@javiereguiluz
Copy link
Member

Regarding the capitalization, you are right. We should replace Article/article_details.html.twig by article/article_details.html.twig. A few months ago we still used capitalized directories, but lately we are using lowercased directories. The reasoning is that anything related to templates is always lowercased (e.g. in PHP you use $firstName but in the template you always use first_name). Mor 8000 eover, the directory name doesn't always correspond to the name of a controller (article doesn't necessary represents the ArticleController).

Regarding the location of the templates, it's correct that they are stored in app/Resources/views/. If you don't store them there, Symfony won't be able to find them.

@xabbuh
Copy link
Member
xabbuh commented May 28, 2015

@jxmallett So, if I do understand you correctly, this is only an issue when using the PHP templating engine? When using Twig placing the files under app/Resources/views works as expected?

@jxmallett
Copy link
Contributor Author

I think you're right that this is a bug in the PHP templating engine, @xabbuh - I tried this in a Twig template and it worked fine.

To reproduce:
In a PHP template, add the line echo $view->render('article/article_details.html.php')
The engine will look for app/Resources/article/article_details.html.php
It should look for app/Resources/views/article/article_details.html.php

@jxmallett
Copy link
Contributor Author

As a side note, it would be really nice if the 'not found' error message included a list of locations it checked. So instead of saying could not find my_template.twig it would say could not find my_template.twig. Tried /path/to/app/resources/my_template.twig, path/to/app/resources/views/my_template.twig.

Certainly beyond the scope of this thread, and it looks like it would require non-trivial source code changes, but it would have saved me a lot of debug time!

@stof
Copy link
Member
stof commented May 29, 2015

@jxmallett this should be requested to the Twig project

@javiereguiluz
Copy link
Member

@jxmallett I really like the idea of displaying all the template paths tested before failing.

@xabbuh
Copy link
Member
xabbuh commented May 29, 2015

A possible change to the PHP templating engine should be discussed on the symfony/symfony repository.

@wouterj
Copy link
Member
wouterj commented May 31, 2015

I created 2 issues for the bugs and ideas suggested in this topic: symfony/symfony#14806 and symfony/symfony#14804

I'm going to close this topic, as the bug should be fixed in the code instead of adopting the docs to the bug.

Thanks for bringing up these things, @jxmallett!

9813

@wouterj wouterj closed this May 31, 2015
@jxmallett
Copy link
Contributor Author

Thanks for your help @wouterj

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants
0