Symfony Translations - twig

i'm using the last version of symfony (3.1.3)
i want to trans multi vars.
but i didn't find and good solution
right now i'm doing:
{% block h1 %}{{ 'service.create'|trans }} {{ ('service'|trans) }}{% endblock %}
I try :
{% block h1 %}{{ 'service','service.create'|trans }} }} { %endblock %}
but no luck.
I also try with
{% trans %}service.create|service{% endtrans %}
thanks

You can use parameters in your translation like this:
{{ 'service.create'|trans }}
{{ 'service.create'|trans({'%separator%': 'any text'}) }}
And in your messages.ru.yml
...
service:
create: ... %separator% ...
...
If your separator is in html, add raw like this :
{{ 'service.create'|trans({'%separator%': '<br>'})|raw }}
You have more details in symfony doc here

You can translate strings in backend:
public function indexAction($name)
{
$translated = $this->get('translator')->trans('Hello '.$name);
return new Response($translated);
}
Or in twig templates:
<h1>{{ 'service.create'|trans }}</h1>
Each time you create a new translation resource (or install a bundle that includes a translation resource), be sure to clear your cache so that Symfony can discover the new translation resources:
php bin/console cache:clear
More info: http://symfony.com/doc/current/translation.html

I needed to just trans multi vars.
twig not support that...

If you have a "service.yml.en" file or something like that :
{{ service.create|trans({}, "service") }}
Here the translator will search in your service.yml.en file and load the correct translation key.
If you want to do advance translations, you can use vars like that (for example) :
{{ (className|lower ~ "." ~ field)|trans({}, className|lower) }}
Here if you have a "user.yml.en" and you want to load something like "user.width", it'll work.
Good luck :)

Related

What is the version of #component (from .blade) for twig file?

I don't know if this is right version of #component for twig? Is it render?
{{ render ('mail::message') }}
{{ render ('mail::button', ['url' => '']) }}
or maybe I am not able to write the right syntax here?
Check out Twig's macro.
For the Blade
<!-- /resources/views/demo.blade.php -->
{{ $slot }}
<!-- component use -->
#component('demo')
content
#endcomponent
the basic Twig syntax would be
{# macro definition #}
{% macro demo(param) %}
{{ param }}
{% endmacro %}
{# macro use #}
{# first import and then you can #}
{{ demo('content') }}
You'll need to import the macro before calling it, either with from or import. To import (with either from or import) from the current file, use _self instead of a path. See the docs for more explanation
Your example
{{ render ('mail::button', ['url' => '']) }}
looks like it's coming from Blade's #include, something like
#include('mail::button, ['url' => ''])
For that, Twig's include might be a better fit. The relationship between macros and includes in Twig is similar to the relationship between components and includes in Blade. The basic Twig would be
{# partial.html #}
{{ url }}
{# partial use #}
{% include 'partial.html' with {url: ''} %}

Gutenberg Blocks with Timber and ACF Flexible Content

We're creating a block.twig template as per:
https://timber.github.io/docs/guides/gutenberg/#how-to-use-acf-blocks-with-timber
to use ACF Flexible Content Field Types to create Gutenberg Blocks.
For reference the following sample code would be for Repeater Field Types:
{% for field in fields.repeater %}
Title: {{ field.title }} <br/>
Url: {{ field.url }}
{% endfor %}
which we tested and seems to work fine.
What would be the equivalent using Flexible Content fields?
Thanks.
#Rafael they're a little more complicated, just need to handle them like arrays. Let's pretend your Flexible Content field is called my_fc_field inside of it. You created 2 layouts, each has a single text field, one called tf_one, the other fc_two
{% for subfield in fields.my_fc_field %}
Layout name: {{ subfield.acf_fc_layout }}
Text Field Value: {{ subfield.tf_one ? subfield.tf_one : subfield.tf_two }}
{% endfor %}
doing a {{ dump(fields.my_fc_field) }} should make it clear what's inside there that you can manipulate/output

html_entity_decode for twig (opencart)

im trying to output an attribute of a product on my product page (opencart v3).
The attribute is called 'technicaldetails' and it works just fine using this code:
{% if attribute_groups %}
{% for attribute_group in attribute_groups %}
{% if attribute_group.name == 'technicaldetails' %}
{% for attribute in attribute_group.attribute %}
{{ attribute.text }}
{% endfor %}
{% endif %}
{% endfor %}
{% endif %}
but the technical details field have unstyled list stored in it.. and this outputs the complete html instead of rendering the list.
ive tried using {{ attribute.text|e }} and {{ attribute.text|raw }} and many other alternatives i could find.. but each time is just throws out the html and not render it..
in php this used to work.
<?php echo html_entity_decode($attribute['text']); ?>
so how can i decode the html now as i cant use php in twig and there is no html_entity_decode in twig either :(
looking forward for somehelp :)
much appreciated
thanks.
Just register the html_entity_decode function in twig.
The most simple way is to look where twig is loaded and add the following code,
$twig->addFilter(new \Twig_Simple_Filter, 'html_entity_decode', 'html_entity_decode');
After that you can just do the following in your twig templates
{{ attribute.text|html_entity_decode }}
UPDATE: For Opencart 3.0.3.7 version filter should be like this:
$twig->addFilter(new \Twig\TwigFilter('html_entity_decode','html_entity_decode'));
Find file
document_root/system/library/template/twig.php
Just after
$this->twig = new \Twig_Environment($loader, $config);
add following code
$twig->addFilter(new \Twig_SimpleFilter('html_entity_decode', 'html_entity_decode'));
After doing this, you must went to admin to reload all modifications in menu Extensions -> modifications.
After that you can do the following in all twig files *.twig
{{ attribute.text|html_entity_decode }}

Symfony3: Enable aptoma twig-markdown extension

How can I enable the aptoma twig-markdown extension? I have installed it using composer but when I use {% markdown %} in my twig file, I get an error message saying:
Unexpected "markdown" tag (expecting closing tag for the "block" tag
defined near line 8).
I had a look in the symfony documentation but was not able to find the solution.
Edit:
I tried to add the following code to services.yml but got another error:
twig.markdown:
class: Aptoma\Twig\Extension\MarkdownExtension
arguments: []
tags:
- { name: twig.extension }
Type error: Argument 1 passed to
Aptoma\Twig\Extension\MarkdownExtension::__construct() must be an
instance of Aptoma\Twig\Extension\MarkdownEngineInterface, none given
[...]
Upon request my .twig-file:
{% extends 'XYZBundle::layout.html.twig' %}
{% block title %}
{{ parent() }} – Eintrag anzeigen
{% endblock %}
{% block platform_body %}
<ul>
<li>
Back to the list
</li>
<li>
Edit
</li>
</ul>
<h1>{{ work.title }}</h1>
<div class="work-content">
{% markdown %}
{{ work.content }}
{% endmarkdown %}
</div>
{% endblock %}
According to the docs you need to install the markdown engine of your choice like:
composer require michelf/php-markdown
You need to create a service for the twig extension and the markdown engine, add the engine to the extension and register it as twig extension, f.e like this in your services.yml or config.yml:
services:
markdown.engine:
class: Aptoma\Twig\Extension\MarkdownEngine\MichelfMarkdownEngine
twig.markdown:
class: Aptoma\Twig\Extension\MarkdownExtension
arguments: ['#markdown.engine']
tags:
- { name: twig.extension }
Symfony will then automatically register it as twig extension through the usage of 'tags' or tagged services.
According to the new autowire system, you only have to declare the mardown engine implementing the Aptoma markdown engine interface.
Aptoma\Twig\Extension\MarkdownEngineInterface:
class: the markdown engine of your choice!
twig.extension.markdown:
class: Aptoma\Twig\Extension\MarkdownExtension
tags:
- { name: twig.extension }
If you do not change Aptoma\Twig\Extension\MarkdownEngineInterfaceby something like markdown.engine you do not need to declare arguments in your twig extension service declaration.
If you want to use the recommended markdown engine, install it via:
composer require michelf/php-markdown
Then, declare it:
Aptoma\Twig\Extension\MarkdownEngineInterface:
class: Aptoma\Twig\Extension\MarkdownEngine\MichelfMarkdownEngine
twig.extension.markdown:
class: Aptoma\Twig\Extension\MarkdownExtension
tags:
- { name: twig.extension }
You can see in vendor\aptoma subdirectories the available engine.
But you can use yours. It only have to implement MarkdownEngineInterface.

Swig (node.js) Include a File and pass an object

I am facing this problem.
I've got a page with an include of another like this:
index.html
{{ set pets = { pets : petsObject } }}
{{ include pets.html }}
petsObject is an object like this
petsObjects: [
{ name : "cat" },
{ name : "dog" }
]
When I try to render the page I get a blank page with only this:
[object Object]
I have no clue about what is going on :(
Thanks in advance!
Seems you'll need to use:
{% include pets.html with pets %}
According to docs for include:
Locally declared context variables are not passed to the included template by default.
It is also recommended for performance to use the only keyword after the included terms, like this:
{% include pets.html with pets only %}
Beyond that, it depends on the contents of pets.html, which you haven't included here. But, make sure that you're attempting to output the name:
{% for pet in pets %}
{{ pet.name }}
{% endfor %}
Or use a filter like json_encode() to format it:
{% for pet in pets %}
{{ pet|json_encode }}
{% endfor %}
Trying to output the Objects themselves will simply produce [object Object]:
new Object().toString() === "[object Object]"

Resources