Vote count:
0
Say I have a simple model with Title, Price, Description and Image fields. I would like 3 html outputs/blocks that I can reuse for each object of this model. For added 'complexity', 2 of the outputs will be combined to make the 3rd. So, here is what these 3 output might look like in a template.
output_image
<span class="output_image"><img src="{{ object.image.url }}" alt="{{ object.title}}></span>
output_info
<ul class="output_info">
<li>{{ object.title }}</li>
<li>Price: {{ object.price }}</li>
<li>{{ object.description }}</li>
</ul>
output_all (wraps up the previous 2)
<div class="output_all">
<span class="output_image">...</span>
<ul class="output_info">...</ul>
</div>
I might want to use output_image on a search page, and output_all on listing and advanced search, for example. Hence I only want to write this output once, and reuse it. What is the best way to achieve this?
Initially I thought to add methods/properties to the model class, which would return the formatted html. This worked, and in my template I could just call {{ object.output_all }}
, for example. Is it bad practice to output html like this?
I have briefly looked at other ways too. Inclusion tags seems to work, but I have only tested with a 'complete' version of the output_all case (ie have not 'nested' the other 2 output tags within output_all yet). Is this the correct method, and is it possible to 'nest' the tags?
How to make reuseable blocks of HTML based upon a model object
Aucun commentaire:
Enregistrer un commentaire