2

I have a twig date field template that has a mysterious extra blank space that I want to remove.

In my twig template, field--comerce-product--field-ppromotion-enddate.html.twig, I have the following line:

  <div{{ item.attributes.addClass('field--item') }}><span class="green">{{ item.content }}{{ 'TEXT'|trans }}</span></div>

Unfortunately, the printed output looks like this:

2017-12-25 TEXT

I don't know where the space is coming from.

I am using a similar template for an integer field that has this line:

  <div{{ item.attributes.addClass('field--item') }}><span class="discount">{{ item.content }}{{ 'OFF!'|trans }}</span></div>

This prints the discount percentage (integer field) and then the word "off" (example: 20%off)-- there is no space between the percentage and off, which is what I want.

(Why no space? My site actually uses an Asian language that doesn't use spaces.)

So where is this space coming from on the date field? When the date field content is printed, it uses the template time.html.twig, but the content of that template is this single line (skipping the comments):

<time{{ attributes }}>{{ text }}</time>

EDIT: Based on the suggested answers, I have tried {{- foo -}} to remove the whitespace around the date and text, as well as {%spaceless%} to remove other whitespace, but the extra space is still there between the date and the text):

  {% spaceless %}
    <div{{ item.attributes.addClass('field--item') }}><span class="green">{{- item.content -}}{{- 'TEXT'|trans -}}</span></div>
  {% endspaceless %}

Actual output of this code: 2017年12月25日 TEXT

Output should be: 2017年12月25日TEXT

3

2 Answers 2

1

You can also use Trim, if doesn't work please check this answer.

0

Surround your code in spaceless tags.

{% spaceless %}

....

{% endspaceless %}

https://twig.symfony.com/doc/2.x/tags/spaceless.html

1
  • I tried this but the space is still there. Nov 15, 2017 at 2:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.