line_from_file

Function line_from_file 

Source
pub fn line_from_file(args: &HashMap<String, Value>) -> Result<Value>
Expand description

A Tera function to sample a specific value from a line-delimited file of strings. The filepath should be passed in as an argument to the path parameter. The 0-indexed line number should be passed in as an argument to the line_num parameter.

Note that the contents of the filepath is read only once and cached.

§Example usage

use tera::{Context, Tera};
use tera_rand::line_from_file;
let mut tera: Tera = Tera::default();
tera.register_function("line_from_file", line_from_file);
let context: Context = Context::new();
let rendered: String = tera
    .render_str(
        r#"{{ line_from_file(path="resources/test/addresses.txt", line_num=2) }}"#,
        &context
    )
    .unwrap();