Comments (5)
Sitrep: I figured it out. But y'all should really document this!
Here's my Rust experiment for this that yields the wanted results:
- My main Rust file (
src/main.rs
):
use coutils::read_file;
use liquid::ObjectView;
use liquid::ValueView;
use liquid::ParserBuilder;
use std::fmt::Debug;
use liquid::Template;
use liquid::object;
use coutils::clean_split;
use coutils::create_file;
use coutils::write_to_file;
use std::collections::HashMap;
use liquid::partials::EagerCompiler;
use liquid::partials::InMemorySource;
#[derive(ObjectView, ValueView, Clone, Debug)]
pub struct SiteContext {
title: String,
content: String,
}
impl SiteContext {
pub fn new(title: &str, content: &str) -> SiteContext {
return SiteContext{
title: title.to_owned(),
content: content.to_owned(),
}
}
}
pub fn render_template(template_path: &String, ctx: &SiteContext) -> Result<(), String> {
let template_string: String = read_file(template_path);
type Partials = EagerCompiler<InMemorySource>;
let mut new_ctx: HashMap<String, SiteContext> = HashMap::new();
let src: &String = &String::from("header.liquid");
let src_contents: &String = &read_file(src);
let mut partial_source = Partials::empty();
partial_source.add(src, src_contents);
new_ctx.insert(String::from("site"), ctx.to_owned());
let mut template: Template = match ParserBuilder::with_stdlib().partials(partial_source)
.build().unwrap()
.parse(&template_string){
Ok(template) => template,
Err(e) => {
return Err(e.to_string());
}
};
let mut globals = object!(new_ctx);
let mut html_string = match template.render(&globals) {
Ok(html_string) => html_string,
Err(e) => {
return Err(e.to_string());
}
};
let path_items: Vec<String> = clean_split(template_path, &String::from("/"));
let last_index: usize = path_items.len() -1 ;
let fname_components: Vec<String> = clean_split(&path_items[last_index], &String::from("."));
let base: &String = &fname_components[0];
let new_name: &String = &format!("{}.html", base);
create_file(new_name);
write_to_file(new_name, &html_string);
return Ok(());
}
fn main(){
let ctx: SiteContext = SiteContext::new(
&"TEST",
&"TEXT TEXT TEXT TEXT TEXT TEXT\nTEXT TEXT TEXT TEXT TEXT TEXT"
);
let template_path = &String::from("test.liquid");
match render_template(template_path, &ctx) {
Ok(_x) => {},
Err(e) => {
println!("{}", e);
}
}
}
- My
Cargo.toml
:
[package]
name = "liquid-exp"
version = "0.1.0"
edition = "2021"
[dependencies]
liquid = "0.26.4"
coutils = { git = "https://github.com/angeldollface/coutils", version = "1.3.0" }
- My Liquid template (
test.liquid
):
<!DOCTYPE html>
<html>
{% include "header.liquid" %}
<body>
<h1>{{ site.title }}</h1>
<p>{{ site.content }}</p>
</body>
</html>
- My partial (
header.liquid
):
<head>
<title>{{ site.title }}</title>
</head>
- The result (
test.html
):
<!DOCTYPE html>
<html>
<head>
<title>TEST</title>
</head>
<body>
<h1>TEST</h1>
<p>TEXT TEXT TEXT TEXT TEXT TEXT
TEXT TEXT TEXT TEXT TEXT TEXT</p>
</body>
</html>
- Directory and file structure:
.
├── Cargo.toml
├── header.liquid
├── src
│ └── main.rs
├── test.html
└── test.liquid
from liquid-rust.
Please add the label documentation needed
. Also, I should add that Mandy now has the functionality of using partials. The link to the Liquid template processor is here.
from liquid-rust.
Related Issues (20)
- How to use the "Template" class. HOT 33
- Allow accessing renderer state/runtime before rendering HOT 6
- with_time unimplemented HOT 3
- Change `master` branch to `main` HOT 3
- could not compile kstring HOT 1
- Render Tag HOT 3
- EmptyDrop and Object indexing HOT 7
- Make struct InnerError public HOT 1
- Open to the idea of supporting less strict parsing/rendering as an option? HOT 3
- Can't render dates with millisecond precision HOT 1
- No way to return a reference for the values method of trait ObjectView
- Dependency Dashboard
- Question: supporting object literals with unknown keys on custom filters HOT 8
- Thoughts on implementing `From<serde_json::Value> for liquid_core::Value`? HOT 2
- Liquid Tag and Echo Tag
- How would you go about implementing a Drop HOT 3
- Add support for u64 numbers?
- Inline comments are not supported HOT 1
- Hash access to globals produces parse error
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from liquid-rust.