Twig "trans with" not working - php

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

Related

Symfony 3 - FOSUserBundle - register.html.twig - template not found

I'm pretty new to Symfony development and i just tried to use the FOSUserBundle. I followed the guide Getting Started With FOSUserBundle on the official Symfony website.
Afterwards i wanted to test the functionality of the installation and visited the link www.linktomytestproject.dev/login which worked flawlessly. But when i tried to visit www.linktomytestproject.dev/register to test the registration functionality, i got the following errormessage:
Unable to find template "register_content.html.twig" (looked into: /home/vagrant/mytestproject/app/Resources/views, /home/vagrant/mytestproject/vendor/symfony/symfony/src/Symfony/Bridge/Twig/Resources/views/Form) in #FOSUser/Registration/register.html.twig at line 4.
500 Internal Server Error - Twig_Error_Loader
I checked the file register.html.twig, which contains the following code:
{% extends "#FOSUser/layout.html.twig" %}
{% block fos_user_content %}
{% include "register_content.html.twig" %}
{% endblock fos_user_content %}
When I change the third line into:
{% include "FOSUserBundle:Registration:register_content.html.twig" %}
The file is found correctly and i can use the registration functionality as intended.
But the change i made is inside the vendor folder, which obviously isn't affected by any means of version control. So i guess there has to be a place somewhere in the configuration files, where this file/foldermapping is affected.
I would really appreciate it, if someone could help me out with this question, even though this is probably a really basic problem.
First override the FOSUserBundle http://symfony.com/doc/current/bundles/FOSUserBundle/overriding_controllers.html
After that, you can create view folders like in vendor and you can use views like that;
{% include "YourBundle:Registration:register_content.html.twig" %}
It's a bug a issue is open on github
Is a bundle issue, in my project I change on the file
\vendor\friendsofsymfony\user-bundle\Resources\views\Registration\register.html.twig
this
{% include "register_content.html.twig" %}
for this
{% include "#FOSUser/Registration/register_content.html.twig" %}

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

A NULL check in Twig Template is not working which leads to an error (very strange behavior)

I’m currently facing a very strange problem in a Twig template (Symfony v2.8.9; Twig v1.24.1). The basic outline is: I’m trying to overwrite a block in a child template when a specific condition is met.
Here’s a snippet of my template:
{% extends "#App/search/resultList.html.twig" %}
{% if category.teaser %}
{% block description %}{{category.teaser.doSomething()}}{% endblock %}
{% endif %}
This code leads to the following error:
Impossible to invoke a method ("doSomething") on a null variable in #App/search/categoryResult.html.twig
The teaser property of the category indeed is null, which is okay and what the check is for.
I also tried an explicit {% if category.teaser is not null %} which also didn’t work and resulted in the same error.
Now for the really weird part (or maybe I’m just not seeing it why this wouldn’t be weird):
If I change the code to
{% block description %}
{% if category.teaser %}
{{category.teaser.doSomething()}}
{% endif %}
{% endblock %}
it works as intended (mostly, at least, because the parent block will always be overwritten).
This error seems to happen only in the Symfony dev environment.
It seems:
# Twig Configuration
twig:
debug: true
strict_variables: true
has something to do with it, which explains the dev environment only. If both values are set to false it works correctly.
Has anybody ever encountered a problem like this and solved it? Any help in solving this issue would be much appreciated.
As far as I know, it is not possible to conditionally override a block in twig, see also How can I conditionally override a TWIG layout block?
You have done the best practice by testing whether the variable is null or not.
In many other languages you can’t do something on a null. You can use the dump() function to check your variables.

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

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