New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for negative length in `truncate` #313

Closed
codesections opened this Issue Aug 3, 2018 · 3 comments

Comments

2 participants
@codesections

codesections commented Aug 3, 2018

Many languages allow negative numbers in string-manipulation functions to index from the end of the string rather than the beginning, and it would be very helpful if the {{ truncate }} filter supported this behavior. For example, it would be very nice to write:

{{ "Hello world!" | truncate(length=-1, end="") }}

and get Hello world.

Right now, you can get something similar with the much more verbose:

{% set len="Hello, world!" | length %}
{{ "Hello, world!" | truncate(length=len-1, end="") }}
@Keats

This comment has been minimized.

Show comment
Hide comment
@Keats

Keats Aug 6, 2018

Owner

I'm not sure it makes sense to have that, it's not that common to want to truncate to a string specific char number

Owner

Keats commented Aug 6, 2018

I'm not sure it makes sense to have that, it's not that common to want to truncate to a string specific char number

@codesections

This comment has been minimized.

Show comment
Hide comment
@codesections

codesections Aug 6, 2018

The use case I was thinking about was more trimming the last few characters off of a string. I encountered the issue when using the Gutenberg current_path variable, which ends with a trailing /. Netlify removes this slash so I also wanted to remove it in my cannonicial links. So I have:

    {%- set len = config.extra.live_base_url~current_path | length -%}
    <link rel="canonical" href="{{ config.extra.live_base_url ~ current_path 
    | truncate(length=len - 1, end="") }}">

That particular use case might be rare, but trimming the last n characters off of a string seems like it's a pretty common need. For example, if structured data always ends with the same info, this could trim off the last field (or maybe trim off an email signature? Or a closing HTML tag?)

But, there is a work-around, so definitely not essential.

codesections commented Aug 6, 2018

The use case I was thinking about was more trimming the last few characters off of a string. I encountered the issue when using the Gutenberg current_path variable, which ends with a trailing /. Netlify removes this slash so I also wanted to remove it in my cannonicial links. So I have:

    {%- set len = config.extra.live_base_url~current_path | length -%}
    <link rel="canonical" href="{{ config.extra.live_base_url ~ current_path 
    | truncate(length=len - 1, end="") }}">

That particular use case might be rare, but trimming the last n characters off of a string seems like it's a pretty common need. For example, if structured data always ends with the same info, this could trim off the last field (or maybe trim off an email signature? Or a closing HTML tag?)

But, there is a work-around, so definitely not essential.

@Keats

This comment has been minimized.

Show comment
Hide comment
@Keats

Keats Sep 2, 2018

Owner

I don't think I'll add that but if anyone also wants it, please comment on that.

Owner

Keats commented Sep 2, 2018

I don't think I'll add that but if anyone also wants it, please comment on that.

@Keats Keats closed this Sep 2, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment