Symfony twig nl2br - php

I have twig template and rendering some data in this template, I have two variant
$datatest = "test" . chr(13) . "test"; //nl2br work good
$datatest = "test\ntest";//nl2br dosn't work, have string in template test\ntest
and second I have query builder, with select some field
$qb
->select('
CONCAT(TRIM(s.streetAddress), \'\\n\', s.postal, s.postOffice) as company_address
')
I try
$qb
->select('
CONCAT(TRIM(s.streetAddress), CHAR(13), s.postal, s.postOffice) as company_address
')
but query builder have error, don't find function CHAR(13), how to create custom DQL for CHAR(13) ?
and in template when dump(data) have string but nl2br not work
I try another filter twig, like this
<div style="font-size: 8px;">
{{ data.company_name|upper }}<br>
{% set address = data.company_address|nl2br %}
{{ address|upper|raw }}
</div>
<br>
<div style="font-size: 9px;">
{{ datatest|nl2br }}
</div>
what need to do in select for nl2br work fine??

You shouldn't format the output inside the SQL query. You're using an MVC framework, so you should definitely stay within it's concepts - let the presentation be done in the view (a.k.a. template).
I recommend select and present tha data this way:
Model:
$qb->select('s.streetAddress, s.postal, s.postOffice')->from ...
Template:
{{ streetAddress }}<br>
{{ postal }} {{ postOffice }}

Related

Translating a DB variable on a laravel Blade

I'm trying to display a translated word on the blade in a laravel application.
my language variable is "texts"
and I have the following on my blade
#foreach($permission as $value)
<li><label>{{ Form::checkbox('permission[]', $value->id, false, array('class' => 'name')) }}
{{ ('$value->name') }}</label></li>
<br/>
#endforeach
I'm trying to translate this
{{ ('$value->name') }}
This should give a result like, user-edit, user-view...
In my language file, I have the translated texts for those outputs.
I've tried this on my blade
{{ __('texts.$value->name') }}
But it just only printing
texts.$value->name
What is the correct way of translating this,
{{ ('$value->name') }}
You are using single quotes in {{ __('texts.$value->name') }}. Variables don't expand inside single quotes. Concatenate the two strings instead:
{ __('texts.' . $value->name) }}

How to iterate over many properties in Twig loop?

I have a problem with properties in (probably) Twig. I have controller in Symfony where getCategories(), getWords(), getTranslations() methods (from Doctrine) return the objects (relations). Every property in the controller is an array because I call findAll() method (from Doctrine again) which returns the array. Finally I return all the properties from controller to view (Twig file) where I try display the results by Twig for loop.
The problem is the Twig loop only iterates over flashcards property (I know why ;)) and I have no idea how to make many-properties iterating. I'd like the loop to iterate over all properties returned by the controller.
In the controller foreach loop I tried update the flashcards array with new associative keys such as: category, word and translation so that all the results returned by Doctrine (including relations) are stored in one flashcards property but then Symfony throws exceptions.
I wondered if create one array in the controller to which I would push the flashcards, cateogry, word and translation arrays and then return this one array to the view but I don't think this is good practice.
Here's the controller method code:
public function showAllCards()
{
$flashcards = $this->getDoctrine()->getRepository(Flashcards::class)
->findAll();
foreach ($flashcards as $flashcard) {
$category = $flashcard->getCategories()->getName();
$word = $flashcard->getWords()->getWord();
$translation = $flashcard->getTranslations()->getWord();
}
return $this->render('try_me/index.html.twig', [
'flashcards' => $flashcards,
'category' => $category,
'word' => $word,
'translation' => $translation
]);
}
Here's the Twig loop code:
{% for flashcard in flashcards %}
{{ word }}
<br>
{{ flashcard.pronunciation }}
<br>
{{ flashcard.exampleSentence }}
<br>
{{ category }}
<br>
{{ translation }}
<br>
{% endfor %}
I tried to execute the following controller code...
public function showMeAll()
{
$flashcards = $this->getDoctrine()->getRepository(Flashcards::class)
->findAll();
foreach ($flashcards as $flashcard) {
$flashcards['categories'] = $flashcard->getCategories()->getName();
$flashcards['words'] = $flashcard->getWords()->getWord();
$flashcards['translations'] = $flashcard->getTranslations()->getWord();
}
return $this->render('try_me/index.html.twig', [
'flashcards' => $flashcards,
]);
}
...with the following Twig loop...
{% for flashcard in flashcards %}
{{ flashcard.words }}
<br>
{{ flashcard.pronunciation }}
<br>
{{ flashcard.exampleSentence }}
<br>
{{ flashcard.categories }}
<br>
{{ flashcard.translations }}
<br>
{% endfor %}
...but then Symfony says:
An exception has been thrown during the rendering of a template ("Catchable Fatal Error: Object of class Proxies__CG__\App\Entity\Words could not be converted to string").
Could you give me some tips how to solve this problem, please? I'd like the Twig loop to iterates over many properties (flashcard, word, category, translation). Or write if there's a better solution, please.
Thank you in advance for every answer!
According to your snippets, I'm guessing you want something like the following:
{% for flashcard in flashcards %}
{% for word in flashcard.getWords() %}
{{ word }}<br />
{% endfor %}
{{ flashcard.getPronunciation() }}<br>
{{ flashcard.getExampleSentence() }}<br>
{% for category in flashcard.getCategories()() %}
{{ category.getName() }}<br />
{% endfor %}
{% for translation in flashcard.getTranslations() %}
{{ translation.getWord() }}<br />
{% endfor %}
{% endfor %}
Have a look at this section of the documentation. Basically if you had foo.bar, twig will test if bar is a public property of foo and if not test if there is a public getter, getBar, to fetch bar.
Some sidenotes in both of your loops, the values category, word and translation will only hold the last value of your flashcards, because you are overwriting the value each time.

Laravel - bug with echoing var in view

This code outputs exactly what I want to type on page
{{ dd($users->where('id', $user_id)->first()->avatar) }}
So exactly:
It looks like:
<div class="col-lg-1 section-count text-center my-auto small-text p-margin0">
<b>{{ $topic->views }}</b>
<p>WYŚWIETLEŃ {{ dd($users->where('id', $user_id)->first()->avatar) }}
</div>
But when I remove laravel's "dd" to look it like:
<p>WYŚWIETLEŃ {{ $users->where('id', $user_id)->first()->avatar }}
Suddenly... there is crash splash bum error...
Before I tried to do it even like:
<p>WYŚWIETLEŃ {{ dd($users->where('id', $user_id)->values()[0]->avatar) }}
Works again.
On Page
Okay, so let's delete dd :)!
<p>WYŚWIETLEŃ {{ $users->where('id', $user_id)->values()[0]->avatar }}
crash splash bum error... :<
Now this;
What is this??? It's everything correct when I dump it by dd, and without errors :<.
edit
$users var is collection, looks like this
Okay the first thing you need to do, is that you have to make a check if the user actually exists, so always before calling this, make something like:
#if($user = $users->where('id', $user_id)->first())
<p>WYŚWIETLEŃ {{ $user->avatar }}</p>
#endif
Otherwise, would be good if you can check before this what is this query actually giving you ( might give you null when you use this on collection and not Builder ), so like this:
{{ dd($users->where('id', $user_id)->first()) }}

Twig include : use translation in the passed argument

This works fine
{% include 'site/snippet.html.twig'
with {'description': 'Some text'}
%}
But how to get this to work? Using a translation as argument
{% include 'site/snippet.html.twig'
with {'description': '{{ 'solutions.description' | trans }}'}
%}
The snippet.html content is:
<p>
{{ description }}
</p>
And calling the translation {{ 'solutions.description' | trans }} alone shows the content as expected.
What syntax would it be?
You don't need to wrap the string in an extra set of {{ }}. Actually it should work like:
with {'description': 'solutions.description'|trans}

Concatenation with dynamic variables for Twig symfony

I have a specific problem with concat of twig.
When I trying to concatenate dynamic variables showing the error.
Here is my code :
{% set i = 0 %}
{% set nbLignes = codeEvt.nb_lignes_~i %}
{% set nbLignesRef = codeEvt.nb_lignes_ref_~i %}
But I have this error message :
Method "nb_lignes_" for object "\DTO\SuiviJourFonc" does not exist in XXXXXXXXX.html.twig at line 211
I would like to take codeEvt.nb_lignes_0 , but i would like build a "for" for others variables like nb_lignes_1, nb_lignes_2 , nb_lignes_3 ...
How can i do this ?
attribute can be used to access a dynamic attribute of a variable:
The attribute function was added in Twig 1.2.
{{ attribute(object, method) }}
{{ attribute(object, method,arguments) }}
{{ attribute(array, item) }}
Try like this,
{{ attribute(codeEvt, 'nb_lignes_ref_' ~ i) }}
You can try the array-like notation:
{{ codeEvt['nb_lignes_ref_' ~ i] }}
Or even use string interpolation:
{{ codeEvt["nb_lignes_ref_#{i}"] }}

Categories