I've followed the instructions for overriding the default layout.html.twig, but although my paths seem correct, the replacement template seems to be ignored.
I think this might be because I'm using a custom app path (/app/web) but as everything else appears to be working correctly, it seems strange that this would be the cause.
I have placed my alternate layout at app/web/Resources/FOSUserBundle/views/layout.html.twig
and the source is as follows:
{% extends 'AcmeWebBundle::base.html.twig' %}
{% block content %}
{% block fos_user_content %}{% endblock %}
{% endblock %}
Is there some way I can check which paths are being checked for an alternate template?
Nevermind - I cleared the dev cache and it's now working, doh.
Related
According to symfony docs, the variable app is available "everywhere" for twig templates. I want to use app.user in a custom error message, but this variable does not actually seem to be available within these templates while in prod mode; it IS available in dev mode.
Example code:
// project/templates/bundles/TwigBundle/Exception/error404.html.twig
{% extends 'base.html.twig' %}
{% block body %}
{{ parent() }}
<div>
<h1>Page not found</h1>
<p>
Hello {{ app.user.name }}, {% <---- DOES NOT WORK %}
The requested page couldn't be located. Checkout for any URL
misspelling.
</p>
</div>
{% endblock %}
This is just a simple example, but illustrates the point. I've also tried to use
{% if is_granted('IS_AUTHENTICATED_FULLY') %}
// do something fancy
{% endif %}
and this also does not work. It also does not matter whether I extend base.html.twig or pull in the parent(), in all cases it does not work. I get no error messages at all. I have cleared by cache.
Any ideas about how to get this information to my template in prod mode?
I found in the documentation that the problem I describe is by design. From the docs:
Security & 404 PagesĀ¶
Due to the order of how routing and security are
loaded, security information will not be available on your 404 pages.
This means that it will appear as if your user is logged out on the
404 page (it will work while testing, but not on production).
The answer is to create my own exception controller (or extend the default ExceptionController).
I'm trying the example in the tutorial. So I created the necessary twig-file in the right directory.
{# app/Resources/views/lucky/number.html.twig #}
{% extends 'base.html.twig' %}
{% block body %}
<h1>Lucky Numbers: {{ luckyNumberList }}</h1>
{% endblock %}
But if I open the page I get the error
Looks like you try to load a template outside configured directories (../base.html.twig) in lucky\number.html.twig at line 2
Anyone who can help me out?
Firstly remove the ".twig" from "base.html.twig".
Then check that the file "base.html.twig" is at the root of views folder.
In case it is in a nested folder then nested folder is also specified in extends command (check documentation).
I have created the custom data collector for profiler as described here http://symfony.com/doc/current/cookbook/profiler/data_collector.html .
Now everything is working correctly except of {% block panel %} in template which defines the content area for my information. I have everything in place but on the screen tha page is almost without styles so its visualy broken.
When I compare my panel page and some other from profiler the mine lacks of cca. 200 lines of styles that Symfony is adding to the page header to style the page. I miss style for #content as an example. I tried to clear cache and refreshed everything but still now way. Any idea what is going to be wrong here?
This is waht I see in the browser ...
EDITED: Template
{% extends 'WebProfilerBundle:Profiler:layout.html.twig' %}
{% block toolbar %}
{% endblock %}
{% block head %}
{% endblock %}
{% block menu %}
<span class="label">
<strong>NET.Notes</strong>
</span>
{% endblock %}
{% block panel %}
panel david
{% endblock %}
It is due to empty block of "head". Rust remove it or include parent block by {{parent()}}.
{% block head %}
{#if the web profiler panel needs some specific JS or CSS files #}
{% endblock %}
I have problem with included files. In my layout.html.twig I have scripts.js at the end body section before {% block script %}{% endblock %}. When file extending layout and use script block, its ok, but when use this block and include file whose use the same block, then is problem. Rendered page has all javascripts, but in different places.
For example:
page.html.twig
...
There is page.html.twig
{% include 'file.html.twig' with {'something': 'something'} %}
After include file.html.twig
..
{% block script %}
<script src="file1.js"></script>
{% endblock %}
file.html.twig
There is file.html.twig
{% block script %}
<script src="file2.js"></script>
{% endblock %}
Then rendered page look like this:
There is page.html.twig
There is file.html.twig
<script src="file2.js"></script>
After include file.html.twig
<script src="file1.js"></script>
I want have all javascript files in one place, one by one.
I think you should try to 'embed' instead of 'include'.
http://twig.sensiolabs.org/doc/tags/embed.html
With embedded, you can choose blocks to include.
Ps : If you override a block, you can get the parent block content in using parent() :
{% block script %}
{{ parent() }}
{% endblock script %}
The {% block %} tag and the inheritance system work only with layouts and {% extends %}. It is not meant to be used with includes, so the script block in your included file.html.twig does not merge with page.html.twig.
One solution would be to set a an argument to you included file.
file.html.twig
{% if get == 'content' %}
There is file.html.twig
{% endif %}
{% if get == 'script' %}
<script src="file2.js"></script>
{% endif %}
page.html.twig
...
There is page.html.twig
{% include 'file.html.twig' with {'something': 'something', 'get': 'content'} %}
After include file.html.twig
..
{% block script %}
<script src="file1.js"></script>
{% include 'file.html.twig' with {'something': 'something', 'get': 'script'} %}
{% endblock %}
You will need to conform to a standard practice when dealing with included templates and inheritance (you can invent your own standard).
Try to compartmentalize your includes, I usually have a directory called 'partials' for includes, and 'fragments' for renders. Each one belonging to a single collection of controller views.
One way I dealt with a similar problem to what you are having was to use a base template which covered the requirements of a specific set of views, each view template would extend it. It may be somewhat wasteful to include the javascripts and stylesheets for the entire collection of views for a specific controller, but it is a) more efficient that including all assets everywhere and, b) I manage the view specific assets under a single base template.
So long as the views have a dependency on that base template the structure wouldn't break.
Think of Twig templates as PHP classes (they compile to classes anyway). A class can inherit from one chain of parents. What you are trying to do is treat two sub-classes as a single child of a super class, overriding the same method at the same time. Simply can't be done. An include is closer to a child property, with is own rules and properties. The included template is less dependent on the includer than vice-versa, so it is impossible for it to inherit from it conventionally.
I have two django templates in my one folder. The template for the url localhost:8000/people picks CSS correctly which is located at /m/css/style.css
The other template for the url localhost:8000/people/some-name in the same folder is trying to retrieve CSS from people/m/css/style.css
Why is this second template not picking CSS like the first one?
My erring second template is like this:
{% extends "base.html" %}
{% block page_title %}{{ entry.name }} | {{ block.super }}{% endblock %}
{% block main %}
<h1>{{ entry.name }}</h1>
{{ entry.body|linebreaks }}
{% endblock main %}
As you can see there's nothing in the template that could cause problem.
It looks to me like your templates are looking for a stylesheet located at ../m/css/style.css. That's why the template in /people works - /people/../m/css/style.css refers to /m/css/style.css. However, /people/some-name/../m/css/style.cssrefers topeople/m/css/style.css`, not the desired address.
Make sure the templates are looking for /m/css/style.css - emphasis on the very first / character.