[][src]Trait rusty_express::prelude::EngineContext

pub trait EngineContext {
    fn display(&self, field: &str) -> Result<String, String>;
}

In order to streamline the way to supply rendering context information to the underlying template engines, the EngineContext trait is required to be implemented by the ViewEngine framework's data model objects. However, the framework can choose other means to obtain context info, under which case, the display function can be no-op (i.e. always return empty string wrapped in Ok() as the return value)

Examples

use rusty_express::prelude::*;

pub struct RenderModel {
    id: String,
    user: String,
    email: String,
}

impl EngineContext for RenderModel {
    fn display(&self, field: &str) -> Result<String, String> {
        match field {
            "User" => Ok(self.user.to_owned()),
            "ID" => Ok(self.id.to_owned()),
            "Email" => Ok(self.email.to_owned()),
            _ => Err(&format_err!("Unable to provide information for the key: {}", field)[..]),
        }
    }
}

Required methods

fn display(&self, field: &str) -> Result<String, String>

display function should be implemented to provide the rendering context information for the template engine. For example, if the template engine encounters something like <p>{{msg}}</p>, and the context.display("msg") returns Ok(String::from("A Secret Message!")), then the rendered content could be <p>A Secret Message!</p>

Loading content...

Implementors

Loading content...