PHPStorm autocomplete twig vars - php

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 :-) -->

Related

show all twig variables in symfony web developer toolbar

is there an easy way to always show the twig vars {{dump()}} in the symfony web developer toolbar? I already have implemented a custom data collector but if I add the following lines in my collector templates:
<div class="sf-toolbar-info-piece">
<b>Twig vars</b> {{ dump() }}
</div>
only the vars given to the toolbar will be shown not the ones I added in to the render function in the controller.
Basically I want something like the output that you see if you add
{% dump %}
in your twig template.

Trans application with Netbeans

I want to do translations in your project, so I need to add tags {% trans %} and {% endtrans %}. How can I in Netbeans select the text to translate and automatically (click or keyboard shortcut) to add these tags? I use Symfony with Twig. I can use also other IDE.
You need to install netbeans symfony plugin.
Btw, for translations in twig, you can use this type of syntax:
{{ 'your.translation'|trans }}

Inherit dynamic template in Phalcon Volt

I need to load a page, that will be "inserted" in a template - as I read it, Volt's Template Inheritance should do the trick and it does... kinda. Hardcoded values, as shown in the examples, work fine - the following example works:
<!-- Template -->
<div id="site_content">
{% block test %}
{% endblock %}
</div>
and the page, that inherits the template:
{% extends "../../templates/de/index.volt" %}
{% block test %}
{{ content() }} {# this is a registered volt function that outputs the generated content #}
{% endblock %}
However, the same page might need to inherit a different template and that must be decided on runtime, so the name of the template must be generated dynamically. Two options occurred to me:
Set the template name to a variable and use it when extending - the problem here is that I don't see a way to use it afterwards. That guy seems to have had the same problem, but there is neither an answer of how to do it, nor a confirmation that it isn't possible at all.
Register another function to generate the complete string (e.g. {% extends "../../templates/de/index.volt" %}) and then compile it, e.g.
$compiler->addFunction('get_template',
function ($resolvedArgs, $exprArgs) use ($volt) {
return $volt->getCompiler()
->compileString('{% extends "../../templates/de/index.volt" %}');
});
and then use that function in the page, e.g.
{{ get_template() }}
{% block test %}
{{ content() }}
{% endblock %}
However, using that approach does not parse the page content (e.g. the content returned by the registered content() function is not shown). I'm also open to other solutions (using Twig instead of Volt is only a last resort, for performance issues), advices of what I'm doing wrong or pointers of useful articles on the topic. Thanks in advance!
Try using partials as documented in the Phalcon doc: Using Partials

Symfony2 Twig Inject additional blocks

Is there a way with Symfony2 and Twig to always make files available which just contain blocks.
For example, if I wanted to make a block named 'cookie' always available in any template in my system, without having to always include or extend it in the template I'm using.
Any ideas?
Clarification
Say I have a generic block which can do something, like:
{% block myBlock %}
ABC Examples
{% endblock %}
I have a class which knows it wants to be rendered with this block. My template itself doesn't necessarily know this though.
{{ block(myObj.blockName) }}
I would then like to have it so my controller/services/etc. could register the file which contains that block, without my template actually needing to know about it directly (so I could have multiple files like that, each working with some common interface).
Similar to registering custom Twig functions with a TwigExtension. My template doesn't need to explicitly know it's there, it just has to be available at run-time.
Does that make sense?
To clarify a bit further, I'm essentially looking to do something just like how there are default Twig blocks for rendering Forms in Symfony2. I don't have to include the default form file every time, just when I want to change something.
I went digging around in the Symfony source code to try and find my answer. It looks like there isn't some fancy, neat way to embed it from a configuration file or controller directly, which is a bit disappointing, but not a huge deal.
So, to solve my situation, I'll be using the "use" keyword to include my block file in my base template, so it will then be available to everything else.
{# widget_blocks.html.twig #}
{# Widgets #}
{% block my_widget %}
ABC Cookies
{% endblock %}
{# base.html.twig #}
{% use widget_blocks.html.twig %}
{{ block(my_widget.block) }}
Not exactly what I wanted, but sufficiently close.
{% render 'MyMainBundle:Default:credits' with {'arg1': $myObj } %}
Thats what I say. What's the difference between the line above or
{{ block(myObj.blockName) }}
You can register a custom filter but as far as I know it returns only string value. http://twig.sensiolabs.org/doc/advanced.html#id2

How to use dump in TWIG?

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

Categories