简体   繁体   中英

Check length of string, then pad with space or splice with twig

I would be like to know if exists a twig function that check aa length of a tring, then, limit the string to the desired length or pad it with spaces.

I known how to make this using str_pad and substr and writing a new Twig filter . But I want to known if somebody can do it with existents twig filter/functions

For example (string as var name for commodity)

{{ "123123123123123"|filter(5) }}

returns "12312"

{{ "12"|filter(5) }}

returns "12_____" (with _ as space)

You can use the format filter with a padding & precision modifier:

This example pads up to 10 characters or trims the string to 10 characters:

Default, left padding:

Cuts to 10...
Arbitrary character:        {{ "%'_10.10s"|format("123123123123123") }}
Space-Padded:               {{ "% 10.10s"|format("123123123123123") }}
Zero-Padded:                {{ "%010.10s"|format("123123123123123") }}

... or expands to 10:
Arbitrary character:        {{ "%'_10.10s"|format("123") }}
Space-Padded:               {{ "% 10.10s"|format("123") }}
Zero-Padded:                {{ "%'010.10s"|format("123") }}

Right padding:

Cuts to 10...
Arbitrary character:        {{ "%'_-10.10s"|format("123123123123123") }}
Space-Padded:               {{ "% -10.10s"|format("123123123123123") }}
Zero-Padded:                {{ "%0-10.10s"|format("123123123123123") }}

... or expands to 10:
Arbitrary character:        {{ "%'_-10.10s"|format("123") }}
Space-Padded:               {{ "% -10.10s"|format("123") }}
Zero-Padded:                {{ "%'-010.10s"|format("123") }}

Output:

Default, left padding:

Cuts to 10...
Arbitrary character:        1231231231
Space-Padded:               1231231231
Zero-Padded:                1231231231

... or expands to 10:
Arbitrary character:        _______123
Space-Padded:                      123
Zero-Padded:                0000000123

Right padding:

Cuts to 10...
Arbitrary character:        1231231231
Space-Padded:               1231231231
Zero-Padded:                1231231231

... or expands to 10:
Arbitrary character:        123_______
Space-Padded:               123       
Zero-Padded:                0000000123

Note that 0-padding only works from the left, even if you add the alignment modifier

Edit: TwigFiddle

To get the count in Twig you may attempt this:

{% if users|length > 100 %}
    {{ users | truncate(50, true) }}
{% endif %}

where users is the variable. In the aforementioned scenario, if the length of users is greater than 100, limit it to 50 characters.

If you want to do that in just one line, try ternary like this:

{{ (users|length > 100) ? truncate(50, true) : users }}

Note: In order to use truncate, the Text extension may need to be activated. You may be able to activate it, like this:

# app/config/config.yml
  services:
    twig.extension.text:
        class: Twig_Extensions_Extension_Text
        tags:
            - { name: twig.extension }

This is the answer with "standard" Twig functions as requested:

{% for i in 0..4 %}
    {{- var|slice(i,1)
    ? var|slice(i,1)
    : '-' -}}
{% endfor %}

You can replace '-' with a space ' ' as needed.

Here is a Twigfiddle to show it working. I have two samples to see the difference. https://twigfiddle.com/ioxeh0

Make sure you use the minus signs in the ternary, that removes addtional whitespace. Enjoy!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM