Skip to main content

current_page

Function current_page 

Source
pub fn current_page(current: &str, url: &str) -> bool
Expand description

Whether url names the page being shown (Rails’ current_page?), for “you are here” links.

current is the request’s URI as text (uri.to_string() of the Uri or OriginalUri extractor, or just its path and query); url is a link target: a path, a path with a query, or an absolute URL. Paths are compared without a trailing slash (/posts/ is /posts) and without a #fragment. The query only matters when url has one, and the host only when both are absolute. Pure string work, no binding call.

§Examples

use ocre::helpers::current_page;

let current = "https://shop.example.com/posts?page=2";
assert!(current_page(current, "/posts"));
assert!(current_page(current, "/posts/?page=2"));
assert!(!current_page(current, "/posts?page=3"));
assert!(current_page(current, "https://shop.example.com/posts#top"));
assert!(!current_page(current, "https://other.example.com/posts"));

In a template, with current: String set from the Uri extractor: <a href="/posts" class="{{ ocre::helpers::class_names([("active", ocre::helpers::current_page(current, "/posts"))]) }}">.