Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Template Enum support #1104

Open
RylanYancey opened this issue Oct 20, 2024 · 4 comments
Open

Template Enum support #1104

RylanYancey opened this issue Oct 20, 2024 · 4 comments

Comments

@RylanYancey
Copy link

I'd like to use Askama as follows:

#[derive(Template)]
#[derive(Serialize, Deserialize)]
pub enum QuestionData {
	#[template(path = "./templates/question/editor/multiple-text.html")]
	MultipleChoiceText {
		numbering: Numbering,
		text: Vec<String>,
	},

	#[template(path = "./templates/question/editor/multiple-image.html")]
	MultipleChoiceImage {
		numbering: Numbering,
		images: Vec<i32>,
	},

	#[template(path = "./templates/question/editor/truefalse.html")]
	TrueOrFalse,

	#[template(path = "./templates/question/editor/fillblank.html")]
	FillInTheBlank {
		sections: Vec<String>,
		starts_with_blank: bool,
	},

	#[template(path = "./templates/question/editor/short-answer.html")]
	ShortAnswer {
		answers: Vec<String>,
	},

	#[template(path = "./templates/question/editor/ranking-text.html")]
	RankingText {
		numbering: Numbering,
		items: Vec<String>,
	},

	#[template(path = "./templates/question/editor/ranking-image.html")]
	RankingImages {
		numbering: Numbering,
		items: Vec<i32>,
	},
}

#[derive(Serialize, Deserialize)]
pub enum Numbering {
	Letters,
	Numbers,
	Bullets,
}

Is this a possibility in the future?

@djc
Copy link
Owner

djc commented Oct 21, 2024

I'm not convinced. Why do you need this? Alternatively, could you do MultipleChoiceText(MultipleChoiceText) with struct MultipleChoiceText { numbering: Numbering, text: Vec<String> } instead? That should get you pretty close.

@klownie
Copy link

klownie commented Nov 5, 2024

it would allow us to load different html files using a match statement without having to write something that clusters code.
for example i have a big match statement on my website for different posts, because i want each post to be unique without following a specific template.
this is how I coded it for now:

fn render_project_template(project: &str, high_res: bool) -> String {
    match project {
        "SamuConceptCharacter" => {
            debug!("loaded: SamuConceptCharacter");
            SamuConceptCharacter {
                project_name: project.into(),
                high_res,
            }
            .render()
            .unwrap()
        }
        "Saint-John" => {
            debug!("loaded: Saint-John");
            SaintJohn {
                project_name: project.into(),
                high_res,
            }
            .render()
            .unwrap()
        }
        "HomardRojas" => {
            debug!("loaded: HomardRojas");
            HomardRojas {
                project_name: project.into(),
                high_res,
            }
            .render()
            .unwrap()
        }
        "CarbonixWorkerSuit" => {
            debug!("loaded: CarbonixWorkerSuit");
            CarbonixWorkerSuit {
                project_name: project.into(),
                high_res,
            }
            .render()
            .unwrap()
        }
        "ClimbingExoSuit" => {
            debug!("loaded: ClimbingExoSuit");
            ClimbingExoSuit {
                project_name: project.into(),
                high_res,
            }
            .render()
            .unwrap()
        }
        "ClimbingExoSuit3d" => {
            debug!("loaded: ClimbingExoSuit3d");
            ClimbingExoSuit3d {
                project_name: project.into(),
                high_res,
            }
            .render()
            .unwrap()
        }
        "Intru" => {
            debug!("loaded: Intru");
            Intru {
                project_name: project.into(),
                high_res,
            }
            .render()
            .unwrap()
        }
        "TeamBlue" => {
            debug!("loaded: TeamBlue");
            TeamBlue {
                project_name: project.into(),
                high_res,
            }
            .render()
            .unwrap()
        }
        "TribalYellowDemon" => {
            debug!("loaded: TribalYellowDemon");
            TribalYellowDemon {
                project_name: project.into(),
                high_res,
            }
            .render()
            .unwrap()
        }
        "UrbanWhiteCrowMan" => {
            debug!("loaded: UrbanWhiteCrowMan");
            UrbanWhiteCrowMan {
                project_name: project.into(),
                high_res,
            }
            .render()
            .unwrap()
        }
        _ => {
            error!("loaded: MissingProject");
            MissingProject {}.render().unwrap()
        }
    }
}

Introducing enums instead would make it easier to organise espaclialy when declaring the templates

@djc
Copy link
Owner

djc commented Nov 6, 2024

So this runs into a bunch of issues where the enum media type must match each of the variant template's media type, which I think is tricky to enforce correctly. I think you could do this with newtype variants for a bunch of template types and then having a simple manual Display or Template implementation for the enum itself.

@Kijewski
Copy link
Contributor

Kijewski commented Nov 9, 2024

There is prior discussion to this feature request in issue #666. askama-enum should still work fine for askama 0.12, if someone wants to fork the project.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants