How to use dump in TWIG? - php

I add for template (index.html.twig) simply:
{{ dump(product) }}
and i have error:
The function "dump" does not exist in AcmeStoreBundle:Default:index.html.twig at line 2
Why this function is not enable, and how can i enable this?

You need to configure the debugging extension:
# app/config/config.yml
services:
acme_hello.twig.extension.debug:
class: Twig_Extension_Debug
tags:
- { name: 'twig.extension' }
Per the link mentioned above, Twig debugging is set to work by default in Symfony 2.5+ running Twig 1.16+, and the custom service definition is not necessary. See this answer for more details.

When you configure it like #meze said, you can display all custom variables:
<h1>Variables passed to the view:</h1>
{% for key, value in _context %}
{% if key starts with '_' %}
{% else %}
<pre style="background: #eee">{{ key }}</pre>
{{ dump(value) }}
{% endif %}
{% endfor %}
You can use my simple plugin to convenient inspect your variables:
Twig Dump Bar

Symfony 2.7+ UPDATE:
The DebugBundle allows greater integration of the component into the Symfony full-stack framework. It is enabled by default in the dev and test environment of the Symfony Standard Edition.
Check the VarDumper component and its integration on Twig.
OLD ANSWER:
I would like to suggest a non-native solution. You'll need a third-party bundle, but the final result will be great!
Improvements to the dump version:
all dumps are styled
you can provide the max nesting level to avoid memory issues with large objects
very useful helpers like ldd(), which is an alias for the classic "dump and die"
it has a console dumper (eg. php app/console ladybug:dump "Symfony\Component\HttpFoundation\Request")
it provides an integration with the Symfony Profiler
it automatically detects Symfony, Doctrine, Twig and Silex classes, and links them to the official documentation
Here the links:
knpbundles
github

Related

I need to change display error off in Symfony framework

I want to change symfony display error reporting to off because right now when error occurred it not display good it always showing my code and i think it's not good. I tried several ways but nothing works I want to tell you i don't have access to ssh terminal. i need to do this task with coding.
Assuming you have taken care of those errors, but need to to override the error page, or in other words you want to customise the error page, you will need to do following things.
Create a Template to display errors
{% extends 'base.html.twig' %}
{% block body %}
<h1>Page not found</h1>
<p>The requested page couldn\'t be located. Checkout for any URL
misspelling or return to the homepage.
</p>
{% endblock %}
Override the default controller
twig:
exception_controller: App\Controller\ExceptionController::showException
Or if you want to create a new exception controller from the scratch,
# config/services.yaml
services:
_defaults:
# ... be sure autowiring is enabled
autowire: true
# ...
App\Controller\CustomExceptionController:
public: true
arguments:
$debug: '%kernel.debug%'
I have extracted this from the Symfony documentation. I recommend you to throughly read the Symfony documentation and familiarise yourself in this area. https://symfony.com/doc/current/controller/error_pages.html
Thank you.

Twig "trans with" not working

I'm using the twig trans tag in my templates, and I want to pass variables in it like so:
{% trans with {
'%link_start%': '<a href="http://www.google.nl/">',
'%link_end%': '</a>'
} %}
This %link_start%disclaimer%link_end% applies to all of our messages.
{% endtrans %}
But this gives me the following exception which points to the twig template at the {% trans with line:
PHP Fatal error: Uncaught exception 'Twig_Error_Syntax' with message 'Unexpected token "punctuation" of value "{" ("end of statement block" expected).' in [twig-template:line]
Even when I copy & paste the examples from the Symfony documentation I get the same exception. So I'm at a loss, what am I doing wrong here?
FYI: I'm using Twig 1.33 with the i18n extension enabled (and I'm not using the Symfony framework)
Twig doesn't support trans with out of the box. It is part of the Symfony translation extension. That explains why even the official Symfony documentation doesn't work - you are not using Symfony.
See this issue: https://github.com/twigphp/Twig-extensions/issues/74. There is a pull request to support trans with, but it hasn't been merged.
You might want to use the Symfony Translation Component in your application. You can use Symfony Components in your application, even without using the full Symfony (Framework) stack.
I haven't tried it, but you can try to use jhogervorst/Twi18n instead.
As a workaround you can use the filter tag with replace.
{% filter replace({'%foo%': 'blue', '%bar%': 'red'}) %}
{% trans %}
I like %foo% and %bar% messages.
{% endtrans %}
{% endfilter %}

Twig include performance with and without passing variables

I am wondering what is best perfomance-wise, using
{% include "_inc/template" %}
or
{% include "_inc/template" with {'foo': bar %}
assuming foo is the only variable used in _inc/template and it's also available in global context.
Will there be significant difference in performance between two approaches, if the include is placed within for having ~50 loops?
I don't have direct answer to your question, but you can use the profiler in dev toolbar to see the timeline of the calls.
Maybe try and tell us...
Which is the version of symfony you use?
I know that in symfony > 2.7 the timeline Graph exist.
Ps : Maybe try it with this too:
{% include 'template.html' with {'foo': 'bar'} only %}

PHPStorm autocomplete twig vars

I'm using PHPStorm 8.1 as IDE in a php project (laravel 4.1 project with twig on view side)
Will be great to have an autocomplete hint on vars on "twig side" like you can see here on this phpstorm plugin for symfony2:
http://symfony2-plugin.espend.de/languages/twig/index.html#phptypes
Basically in my twig view I declare the var "type" using a comment, then I would see hints on my model (farmaco):
{# farmaco \Farmaco #}
{% farmaco. %} <-- I would see hints -->
Note: I'm not using Symfony, however I've the symfony plugin installed but the autocomplete doesn't work for me on twig files for my laravel project.
You seem to be missing the #var annotation.
The correct form would be:
{# #var foo \FooService #}
{{ foo. }} <!-- press CTRL+SPACE for typehints -->
I got this working for a non-symfony project without issues. Be sure to check that the symfony plugin is actually enabled for the project (it isn't by default) and restart the IDE after you enable it.
Also make sure that the type-hint point to the fully qualified class name.
As a sidenote, I have not (yet) gotten this to work for properties, i.e. foo.bar.* when the property is accessed through magic (using __get). For those instances the property needs to be put into a variable of its own:
{# #var foo \FooService #}
{{ foo.bar. }} <!-- no typehints for bar :-( -->
{% set bar = foo.bar %}
{# #var bar \BarService #}
{{ bar. }} <!-- press CTRL+SPACE for typehints :-) -->

Symfony2 - Include template from current bundle

I've not been able to find anything useful about this in the Twig or Symfony2 documentation, so thought I would ask here.
Does anybody know if it's possible to include a Twig template in Symfony2 relative to the current bundle, without specifying the name? Something along these lines:
{% include .:Foo:bar.html.twig %}
I'm just a bit fed up of having to enter the long, ugly bundle name when they're all in the same bundle. Also means if the bundle name ever changed for whatever reason, I'd have to find & replace every single include.
Back in the days when I was using bundles, I came up with a quick solution that you could base upon:
{% set bundle = app.request.get('_template').get('bundle') %}
{% set controller = app.request.get('_template').get('controller') %}
{% include bundle ~ ':' ~ controller ~ ':foo.html.twig' %}

Categories