From 738b6014629495643759590a531cf05134b5ed53 Mon Sep 17 00:00:00 2001 From: vabene1111 Date: Tue, 5 Jan 2021 22:07:46 +0100 Subject: [PATCH] templating working --- cookbook/helper/template_helper.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/cookbook/helper/template_helper.py b/cookbook/helper/template_helper.py index 7cabc7d8a..c0928b079 100644 --- a/cookbook/helper/template_helper.py +++ b/cookbook/helper/template_helper.py @@ -7,10 +7,29 @@ from cookbook.helper.mdx_attributes import MarkdownFormatExtension from cookbook.helper.mdx_urlize import UrlizeExtension +class IngredientObject(object): + amount = None + unit = None + food = None + + def __init__(self, ingredient): + self.amount = f'[[calculateAmount({ingredient.amount})]]' + self.unit = ingredient.unit + self.food = ingredient.food + + def __str__(self): + return f'{self.amount} {self.unit} {self.food}' + + def render_instructions(step): # TODO deduplicate markdown cleanup code + ingredients = [] + + for i in step.ingredients.all(): + ingredients.append(IngredientObject(i)) + template = Template(step.instruction) - instructions = template.render(ingredients=step.ingredients.all()) + instructions = template.render(ingredients=ingredients) tags = markdown_tags + ['pre', 'table', 'td', 'tr', 'th', 'tbody', 'style', 'thead'] parsed_md = md.markdown(instructions, extensions=['markdown.extensions.fenced_code', 'tables', UrlizeExtension(), MarkdownFormatExtension()])