Skip to content

Commit 61b7422

Browse files
committed
Add |titlecase as alias for |title
1 parent 79be271 commit 61b7422

File tree

4 files changed

+30
-3
lines changed

4 files changed

+30
-3
lines changed

askama/src/filters/alloc.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -820,6 +820,31 @@ fn flush_title(dest: &mut (impl fmt::Write + ?Sized), s: &str) -> fmt::Result {
820820
Ok(())
821821
}
822822

823+
/// Return a title cased version of the value. Alias for the [`|title`](title) filter.
824+
///
825+
/// ```
826+
/// # #[cfg(feature = "code-in-doc")] {
827+
/// # use askama::Template;
828+
/// /// ```jinja
829+
/// /// <div>{{ example|titlecase }}</div>
830+
/// /// ```
831+
/// #[derive(Template)]
832+
/// #[template(ext = "html", in_doc = true)]
833+
/// struct Example<'a> {
834+
/// example: &'a str,
835+
/// }
836+
///
837+
/// assert_eq!(
838+
/// Example { example: "hello WORLD" }.to_string(),
839+
/// "<div>Hello World</div>"
840+
/// );
841+
/// # }
842+
/// ```
843+
#[inline]
844+
pub fn titlecase<S: fmt::Display>(source: S) -> Result<Title<S>, Infallible> {
845+
title(source)
846+
}
847+
823848
/// A prefix usable for indenting [prettified JSON data](super::json::json_pretty) and
824849
/// [`|indent`](indent)
825850
///

askama/src/filters/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ mod urlencode;
2525
#[cfg(feature = "alloc")]
2626
pub use self::alloc::{
2727
AsIndent, capitalize, fmt, format, indent, linebreaks, linebreaksbr, lower, lowercase,
28-
paragraphbreaks, title, trim, upper, uppercase, wordcount,
28+
paragraphbreaks, title, titlecase, trim, upper, uppercase, wordcount,
2929
};
3030
pub use self::builtin::{PluralizeCount, center, join, pluralize, truncate};
3131
pub use self::escape::{

askama_derive/src/generator/filter.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -918,6 +918,7 @@ const BUILTIN_FILTERS_ALLOC: &[&str] = &[
918918
"lower",
919919
"lowercase",
920920
"title",
921+
"titlecase",
921922
"trim",
922923
"upper",
923924
"uppercase",

book/src/filters.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -488,8 +488,8 @@ Output:
488488
<p>I'm Safe</p>
489489
```
490490

491-
### title
492-
[#title]: #title
491+
### title | titlecase
492+
[#title]: #title--titlecase
493493

494494
<blockquote class="right" style="padding:0.5ex 1ex; margin:0 0 1ex 1ex; font-size:80%">
495495
enabled by <code>"alloc"</code><br/>
@@ -498,6 +498,7 @@ enabled by <code>"default"</code>
498498

499499
```jinja
500500
{{ text_to_convert | title }}
501+
{{ text_to_convert | titlecase }}
501502
```
502503

503504
<hr style="clear:both; border:0; border-bottom:1pt solid currentColor">

0 commit comments

Comments
 (0)