tera icon indicating copy to clipboard operation
tera copied to clipboard

add case-sensitive arg to sort filter

Open xyzshantaram opened this issue 4 years ago • 1 comments

This PR will fix #587 if merged.

I'm very new to Rust programming, my apologies if I've done something wrong here. I've added a case_sensitive argument that when set to false will make the sorting case insensitive. Here's the code I used to test it.

use serde::{Deserialize, Serialize};
use tera::{Context, Tera};

#[derive(Deserialize, Serialize, Debug, Clone)]
struct Person {
    name: String,
}

fn main() -> Result<(), tera::Error> {
    // Use globbing
    let tera = match Tera::new("templates/**/*.html") {
        Ok(t) => t,
        Err(e) => {
            println!("Parsing error(s): {}", e);
            ::std::process::exit(1);
        }
    };

    let persons: Vec<Person> = vec![
        Person { name: "Robert".into(), },
        Person { name: "paul".into(), },
    ];

    let mut ctx = Context::new();
    ctx.try_insert("persons", &persons)?;

    println!("{}", tera.render("foo.html", &ctx)?);

    Ok(())
}

With the following template

<div>
    {% for person in persons | sort(attribute="name") %}
    <div>
        {{person.name}}
    </div>
    {%endfor%}
</div>

The output is

<div>
    
    <div>
        Robert
    </div>
    
    <div>
        paul
    </div>
    
</div>

With case_sensitive=false

<div>
    {% for person in persons | sort(case_sensitive=false, attribute="name") %}
    <div>
        {{person.name}}
    </div>
    {%endfor%}
</div>

The output becomes:

<div>
    
    <div>
        paul
    </div>
    
    <div>
        Robert
    </div>
    
</div>

Please let me know if there's anything else I need to do before this can be merged.

xyzshantaram avatar Nov 29 '21 08:11 xyzshantaram

Sorry completely missed that. I'll have a look

Keats avatar Aug 08 '22 21:08 Keats

The code looks ok. It's missing some tests so I won't merge it right now, probably for the version after though.

Keats avatar Aug 13 '22 20:08 Keats