1

I have a Drupal site and I want to format the address in TWIG.

How do I delete spaces before and after the address?

How to replace the tags "<br>" by " - " ?

Here is the TWIG code :

<strong>Adresse postale :</strong> {{ store.address }}

Here is the rendering :

<pre class="text-left"><strong>Raison sociale :</strong> Poupette &amp; Cacahuète.
<strong>Adresse postale :</strong> 
            <div class="field field--name-address field--type-address field--label-hidden field--item"><p class="address" translate="no"><span class="address-line1">rue du vermont</span><br>
<span class="postal-code">14600</span> <span class="locality">Honfleur</span><br>
<span class="country">France</span></p></div>

<strong>Téléphone :</strong> .
<strong>Adresse électronique :</strong> poupette.cacahuete14@gmail.com
<strong>Boutique :</strong> https://www.s1biose.com/fr/boutique/poupette-cacahuete
Immatriculé au  de  sous le numéro .
</pre>

enter image description here

2 Answers2

1

If you would like complete control over the render of this, you can access the individual address 'pieces' by using store.address.my_value. So for instance, your address as you described it in a comment would look like:

<span class="my-class">
{{ store.address.address_line1 }} - {{ store.address.postal_code }} {{ store.address.locality }} - {{ store.address.country }}
</span>

If this is the way you always want this to appear, consider adding a theme override for the address-plain.html.twig template to render this as you would like consistently. You can start by copying the module's template from docroot/modules/contrib/address/templates/address-plain.html.twig and altering it within your theme to be as you like.

0

You can use replace and trim functions like this:

{{ store.address|trim|replace({'<br>': '-'})|raw  }}
doydoy44
  • 5,720
  • 4
  • 29
  • 45
  • The output of `replace` is not marked as safe, so u'd need to apply the filter `raw` as well – DarkBee Jan 14 '20 at 14:08
  • @DarkBee, your comments are better than my answer, so create your answer and I delete mine and I vote for yours :) – doydoy44 Jan 14 '20 at 16:06
  • @doydoy44 My comments are not correct. In the first, the markup is rendered in plain text, in the second there is no rendering. Here is what I want to render : `rue du vermont - 14600 Honfleur - France` –  Jan 14 '20 at 16:35
  • @doydoy44 I want to make the address on one line, without markup + by replacing `
    ` with `" - "` + by deleting the spaces before and after.
    –  Jan 14 '20 at 16:37
  • @Mathieu do you test the rim function to remose spaces ? – doydoy44 Jan 14 '20 at 16:42
  • @doydoy44 what should be the code ? I'm on Drupal with Drupal Commerce in the stores template –  Jan 14 '20 at 17:19
  • @Mathieu, sorry for bad typo. I would say if you test {{ store.address|trim }} to remove spaces ? – doydoy44 Jan 14 '20 at 17:23
  • @doydoy44 If I use `{{ store.address }}` the rendering is the same as in my question. If I insert additional code, it doesn't display anything. –  Jan 14 '20 at 17:40
  • Ok it works with this code `{{ store.address|render|trim|replace({'
    ': ' - '})|raw }}` but I have to remove all the tags to put on a single line (like raw text but with "-").
    –  Jan 14 '20 at 17:48
  • `{{ store.address|render|trim|replace({'
    ': ' - '})|striptags|raw }}` I found but there is still a problem, you must put the address on one line https://ibb.co/zfC5Rn8
    –  Jan 14 '20 at 17:54
  • Why does it make the field on 3 lines ? –  Jan 14 '20 at 18:54
  • I guess you have 3 lines in your database. you can use `nl2br` twig function to check that and try something like `|replace({'

    ': ' - '})` or `|replace({'

    ': ' - '})` to solve it after `nl2br`
    – doydoy44 Jan 14 '20 at 21:13
  • @doydoy44 Thanks but it doesn't work. I may have a solution here https://stackoverflow.com/questions/59741391/how-to-make-the-address-code-on-one-line-with-twig –  Jan 15 '20 at 14:46