pub struct Shortcodes {
pub functions: HashMap<String, fn(&HashMap<String, Value>) -> String>,
}Expand description
A struct that manages shortcode functions for use in Tera templates.
§Fields
functions: AHashMapwhere the key is the shortcode display name (aString), and the value is a function pointer that takes a reference to aHashMapof arguments and returns aStringrepresenting the generated content.
Fields§
§functions: HashMap<String, fn(&HashMap<String, Value>) -> String>Implementations§
Source§impl Shortcodes
impl Shortcodes
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new Shortcodes instance with an empty set of registered functions.
§Returns
A Shortcodes struct with an empty functions map.
Sourcepub fn register(
self,
display: &str,
shortcode_fn: fn(&HashMap<String, Value>) -> String,
) -> Self
pub fn register( self, display: &str, shortcode_fn: fn(&HashMap<String, Value>) -> String, ) -> Self
Registers a new shortcode function in the Shortcodes struct.
§Parameters
display: The shortcode display name as a&str, which will be used as the key in thefunctionsmap.shortcode_fn: A function pointer that takes aHashMapof arguments and returns aString.
§Returns
An updated instance of Shortcodes with the newly registered shortcode function.
§Example
use tera_shortcodes::Shortcodes;
let shortcodes = Shortcodes::new().register("example", |args| {
"Shortcode output".to_string()
});Trait Implementations§
Source§impl Function for Shortcodes
impl Function for Shortcodes
Source§fn call(&self, args: &HashMap<String, Value>) -> Result<Value>
fn call(&self, args: &HashMap<String, Value>) -> Result<Value>
Invokes a registered shortcode function by its display name.
§Parameters
args: A reference to aHashMapcontaining the arguments passed to the shortcode function.
§Returns
A Result<tera::Value> that contains the generated content as a String or an error message if the display name is missing or unknown.
§Error Handling
- If the
displayattribute is missing, it returns an error message"Missing display attribute". - If no function is registered for the given display name, it returns an error message
"Unknown shortcode display name: <display>".
Auto Trait Implementations§
impl Freeze for Shortcodes
impl RefUnwindSafe for Shortcodes
impl Send for Shortcodes
impl Sync for Shortcodes
impl Unpin for Shortcodes
impl UnsafeUnpin for Shortcodes
impl UnwindSafe for Shortcodes
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more