do_replace

Macro do_replace 

Source
macro_rules! do_replace {
    ($content:expr, $($key:ident = $val:expr),*) => { ... };
}
Expand description

§do_replace!($content, $key, and $val)

Macro Rules

The do_replace macro takes any content string or data (e.g., code, file content, templates) along with key-value pairs and replaces placeholders in the content (formatted as {{key}}) with the corresponding values. This macro is highly versatile and can manipulate any content of any programming language or file by substituting placeholders.

§Parameters

  • $content: The content string containing placeholders for substitution (e.g., "<p>Hello, {{name}}!</p>").
  • $key: The identifier for each placeholder in the content (e.g., name).
  • $val: The value that replaces the corresponding placeholder in the content (e.g., "Alice").

§Examples

// use cans::content::do_replace;
use cans::do_replace;

let template = "<p>Hello, {{name}}! Welcome to {{place}}.</p>";
let result = do_replace!(template, name = "Dear", place = "CANS Template");
assert_eq!(result, "<p>Hello, Dear! Welcome to CANS Template.</p>");

This macro can be used to update any content dynamically by replacing placeholders with actual values.

§Example: Creating a new macro using do_replace!

Macros for specific content types can be defined by wrapping do_replace!. For example, to create a macro for CSS content:

// use cans::content::do_replace;
use cans::do_replace;
 
#[macro_export]
macro_rules! do_html {
    ($content:expr, $($key:ident = $val:expr),*) => {
        $crate::do_replace!($content, $($key = $val),*)
    };
}

Now, do_css! can be used to easily replace placeholders in CSS templates.

End Doc