CANS
An Elegant Rust-based Literal Template Engine
Overview
The CANS templating engine is an elegant and efficient solution developed in Rust, designed for developers who prioritize simplicity and type safety. This crate allows you to create dynamic web pages and applications with minimal boilerplate code, ensuring that your templates are easy to read and maintain.
While CANS is optimized for seamless integration with the GetPost framework, it also provides the flexibility to work with any framework or content type, making it a versatile choice for a wide range of projects.
Features
- Simplicity: Easy-to-read syntax that minimizes boilerplate.
- Type Safety: Leverages Rust's type system for safe template rendering.
- Dynamic Content: Supports dynamic insertion of values, loops, and conditionals.
- Integration: Works seamlessly with various web frameworks.
Getting Started
Installation
Run the following Cargo command in your project directory:
cargo add cans
or add cans to your Cargo.toml file:
[]
= "MAJOR.MINOR.PATCH" # Replace with the latest version
Usage
-
CANS Template and HTML
CANS provides robust support for templating, including support for handling HTML, looping through collections, and rendering text. Below are some examples demonstrating how to use the do_html macro, the do_forloop macro, and the do_text function.
Example: Using the do_html Macro
use ;
pub const HEAD: &str = r#"<head>
<meta charset="UTF-8">
<title>{{page_title}} Page</title>
</head>"#;
pub const HOME_TEMPLATE: &str = r#"<!DOCTYPE html>
<html>
{{HEAD}}
<body>
Home Page
</body>
</html>"#;
pub const ABOUT_TEMPLATE: &str = r#"<!DOCTYPE html>
<html>
{{HEAD}}
<body>
<p>About Page</p>
{{component_if}}
</body>
</html>"#;
<!-- home page output -->
Home Page
Home Page
<!-- about page output -->
About Page
About Page
x ≠ 1 & x ≠ 2. The 'x' value is ( 3 )
Example: Using the do_forloop Function
use do_forloop;
-
CANS Template and JSON (Parsing and Handling Examples)
CANS provides robust support for handling JSON-like structures. Below are some examples demonstrating how to use the ParseJson struct and the do_json! macro.
Example 1: Creating and Setting Attributes
You can create a new ParseJson instance and set attributes using JSON-like syntax.
use ParseJson;
Output:
key1: 42
key2: hello
key3: true
Example 2: Getting Attributes
You can retrieve attributes by their keys and check the type.
use ParseJson;
Output:
Found key1: 42
Example 3: Updating Attributes
You can update existing attributes using a HashMap.
use Any;
use HashMap;
use ParseJson;
Output:
key1: 100
Example 4: Using the do_json Macro
The do_json! macro allows you to easily generate JSON strings with placeholders.
Output:
Generated JSON: {"name": ""Mido"", "age": 30, "is_member": true}
JSON Profile (in raw string format): r#{"name": ""Mido"", "age": 30, "is_member": true}#
Example 5: Creating JSON Arrays
You can also create JSON arrays using the macro.
use do_json;
Output:
["Rust", "C++", "Matlab", "Python", "Go", "JavaScript"]
{ "hobbies": ["reading", "archery", "hiking"] }
Documentation
For a detailed API reference, visit the CANS Documentation.
Contributing
Contributions are welcome! If you have suggestions or improvements, feel free to submit an issue or a pull request.
License
This project is licensed under the MIT or Apache 2.0 License - see the LICENSE file for details.
Conclusion
Whether you're building a small project or a large application, CANS provides the tools you need to create dynamic and flexible templates with ease. Unlock the potential of CANS to elevate your Rust web development experience!
Donations
If you appreciate the work on CANS and would like to support its development, you can make a donation using USDT (TRC-20). Your contributions will help us continue to improve the project and maintain its features.
Donate with USDT (TRC-20)
- Wallet Address:
TFtKw3aExk5fXvdmTobiVDBbkbJB66wc6D
Donate with USDC (TRC-20)
- Wallet Address:
TFtKw3aExk5fXvdmTobiVDBbkbJB66wc6D
Donate with TRON (TRX)
- Wallet Address:
TFtKw3aExk5fXvdmTobiVDBbkbJB66wc6D
Thank you for your support!
README Structure
- Overview
- Features
- Getting Started
- Examples
- Documentation
- Contributing
- License
- Conclusion
- Donations