Custom data collector for profile is visualy broken - symfony

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 %}

Related

Sensiolabs Insight : Twig templates should not contain business logic

I am actually running Sensiolabs Insight analysis on my Symfony 2.8 project.
I have a major issue with some of my Twig templates:
Twig templates should not contain business logic
The associated message is always the same :
Template too complex, depth of 10 is reached but only 5 is allowed.
For example this happens with the following template :
{% extends "FBNGuideBundle::layout.html.twig" %}
{% block title %}
{{ 'fbn.guide.page_title.bookmarks'|trans }}
{% endblock %}
{% block body %}
<div id="bookmarks" data-bookmark-ids="{{bookmarkIds|json_encode()}}">
{% if (restaurants|length > 0) %}
<div class="restaurants">
<h3>MES RESTOS</h3>
{% for bookmark in restaurants %}
<div class="bookmark" id="{{'bookmark-' ~ bookmark.id}}">
{{ bookmark.restaurant.name }}
<br>
<br>
<button>SUPPRIMER DES FAVORIS</button>
<br>
<hr>
</div>
{% endfor %}
</div>
{% endif %}
</div>
{% endblock %}
I tried to include in a separated file the code contained inside <div id="bookmarks"></div> and the depth has been reduced, but it is not a solution. I suppose that the problem is the access to some properties through several objects using getters (i.e bookmark.restaurant.slug).
I have a free plan so I am not able to access the documentation related to this warning. Anyone knows how to solve the problem ?
Thanks.
When you have too much logic in the view, you can put it in a custom Twig Extension. An advantage is that you don't need to duplicate the html if you are reusing that part in another page and of course, the code is more clear :)
In your case, you can write a new Twig Extension that renders all the bookmarks.
If you didn't build somethng similar till now, you can read about it here http://symfony.com/doc/current/templating/twig_extension.html

Symfony/Twig: Extending stylesheet block from an extended bundle

I'd like to extend the stylesheets blocks from inside a bundle I've created to extend FOSUserBundle.
This is how to extend from inside the same bundle, but I want to extend from another bundle that extends a third-party bundle.
This is the code:
{# src/MyNamespace/UserBundle/views/layout.html.twig #}
{% extends '::base.html.twig' %}
{% block stylesheets %}
{{ parent() }}
{% stylesheets filter='scssphp,cssrewrite' output='css/app.css'
'bundles/mynamespace/scss/Profile.scss'
%}
{% endstylesheets %}
{% endblock %}
{% block title %}My title{% endblock %}
{% block body %}
{% block fos_user_content %}{% endblock %}
{% endblock %}
So, basically, I've extended the FOSUserBundle creating another bundle in my src directory as explained here.
The code of my `base.html.twig template is this:
{# app/Resources/views/base.html.twig #}
...
<head>
...
<title>{% block title %}{% endblock %}</title>
<meta name="description" content="{% block metaDescription %}{% endblock %}" />
{% include('::_common/Stylesheets.html.twig') %}
...
And this is the included Stylesheets.html.twig:
{% block stylesheets %}
{% stylesheets filter='scssphp,cssrewrite' output='css/app.css'
'bundles/app/css/bootstrap.css'
...
%}
<link rel="stylesheet" href="{{ asset_url }}" />
{% endstylesheets %}
{% endblock %}
The problem is that the CSS from src/MyNamespace/UserBundle/views/layout.html.twig is never included in the page, also if it is installed with app/console asstes:install and then dumped with app/console assetic:dump.
The CSS is generated and put in web/css/app_Profile_1.css but is never included in the pages (not in home page or other AppBundle pages, not in MyNamespaceUserBundle pages.
Also adding the line <link rel="stylesheet" href="{{ asset_url }}" /> to the src/MyNamespace/UserBundle/views/layout.html.twig block, the CSS isn't included.
Obviously, if I include the stylesheet directly from _common/Stylesheets.html.twig the file is correctly included.
So, it seems there is something break.
Anyone can help me?
Without the <link rel="stylesheet"> in the {% block stylesheets %} in layout.html.twig, it's not going to output the link for Profile.scss.
More importantly, though, is that {% include %} and {% block %} do not work quite the way you think they do in the presence of {% extends %}. The combination is a little weird, so some background first.
A {% block %} is simply a named wrapper around some content. In the absence of {% extends %}, a block is immediately output where it is defined. (Think of it as if you defined a function, and then immediately called it.)
When you extend another template, however, block definitions in the "global scope" of the extending template replace the definitions of the block in the extended template. This is a re-definition only; the block is still being called at the same place it was in the parent template.
{% include %} renders another template and inserts the results of the evaluation in-line. While it has access to the current context's variables, it is otherwise it's own separate renderer. Importantly, it does not substitute itself with the template code as you are expecting.
So what is happening is this:
layout.html.twig extends base.html.twig. layout.html.twig defines a stylesheets block (but does not emit it). base.html.twig includes Stylesheets.html.twig, so base.html.twig gains the result of evaluating Stylesheets.html.twig (so you get the contents of the {% stylesheets %} tag, but importantly, not the fact that there was even a block there at all.
Then, because there is no place in base.html.twig where there actually is a {% block stylesheets %}, the block defined in layout.html.twig never gets called.
The solution is to move the {% block stylesheets %} into base.html.twig. If you want to keep the actual stylesheet definitions in the Stylesheets.html.twig, then you can do it like this:
{% block stylesheets %}
{% include('::_common/Stylesheets.html.twig') %}
{% endblock %}
And remove the block tag (but not the stylesheet content itself) from Stylesheets.html.twig. That should give you the desired result.

FOSUserBundle changing text on page depending on form

I'm very new to symfony2 and twig templating in general.
My question is related to the FOSUserBundle.
I have a base template which has 2 columns, {% block left %} and {% block right %}
I have created a new file: Base5/UserBundle/Resources/views/layout.html.twig
which extends my 2 column layout
{% extends '::base_2col.html.twig' %}
{% block left %}
I want this text to change depending on wether a login form, register form, profile page etc is displayed
{% endblock %}
{% block right %}
{{ block('fos_user_content') }}
{% endblock %}
as the actual form is rendered using {{ block('fos_user_content') }}, how can i change the text in the right panel depending on what form is displayed, ideally i want to use a different include, containing the various descriptions into texts for the diffirent form.
Any pointers would be very much appreciated.
Found out, just a question of duplicating the templates from the fosuserbundle views directory

Overriding FOSUserBundle layout.html.twig when using a custom app path

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.

One django template not picking CSS, other templates are fine

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.

Resources