Giter VIP home page Giter VIP logo

Comments (5)

angeldollface avatar angeldollface commented on June 16, 2024 1

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.

angeldollface avatar angeldollface commented on June 16, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.