Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions src/render/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ use std::collections::HashMap;

use pyo3::prelude::*;

use super::types::{Content, Context};
use super::{Render, RenderResult, Resolve, ResolveResult};
use crate::error::RenderError;
use crate::parse::{TagElement, TokenTree};
use crate::render::types::PythonTypes;
use crate::types::Argument;
use crate::types::ArgumentType;
use crate::types::TemplateString;
use crate::types::Text;
use crate::types::Variable;
use super::{Render, RenderResult, Resolve, ResolveResult};
use super::types::{Content, Context};


impl Resolve for Variable {
fn resolve<'t, 'py>(
Expand Down Expand Up @@ -43,7 +43,7 @@ impl Resolve for Variable {
key_at: key_at.into(),
object_at: Some(object_at.into()),
}
.into())
.into());
}
};
match variable.get_item(int) {
Expand All @@ -55,7 +55,14 @@ impl Resolve for Variable {
};
object_at.1 += key_at.1 + 1;
}
Ok(Some(Content::Py(variable)))
let python_type: PythonTypes = variable.extract()?;
let content = match python_type {
PythonTypes::Int(data) => Content::Int(data),
PythonTypes::Float(data) => Content::Float(data),
PythonTypes::String(data) => Content::String(Cow::Owned(data)),
PythonTypes::CatchAll(data) => Content::Py(data),
};
Ok(Some(content))
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/render/filters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -446,8 +446,9 @@ mod tests {

Python::with_gil(|py| {
let engine = EngineData::empty();
let template_string = "{{ var|default:1|slugify }}".to_string();
let template_string = "{{ var|slugify }}".to_string();
let context = PyDict::new(py);
context.set_item("var", 1).unwrap();
let template = Template::new_from_string(py, template_string, &engine).unwrap();
let result = template.render(py, Some(context), None).unwrap();

Expand Down
11 changes: 11 additions & 0 deletions src/render/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,17 @@ fn resolve_python<'t>(value: Bound<'_, PyAny>, context: &Context) -> PyResult<Co
))
}

#[derive(Debug, FromPyObject)]
pub enum PythonTypes<'py> {
#[pyo3(transparent)]
Int(BigInt),
#[pyo3(transparent)]
Float(f64),
#[pyo3(transparent)]
String(String),
CatchAll(Bound<'py, PyAny>),
}

#[derive(Debug, IntoPyObject)]
pub enum Content<'t, 'py> {
Py(Bound<'py, PyAny>),
Expand Down
Loading