M
M
Michael2015-03-01 15:33:19
Flask
Michael, 2015-03-01 15:33:19

How is it more convenient to pass a bunch of arguments to the template engine?

Hello, in general, the problem is, how to pass a bunch of arguments to the template more conveniently? That is, for prime there is a function that does something and everything ends in cozza

return render_template("index.html",arg1 = '1 arg',arg1000000 = 'arg100000000' )

here's how to make it more convenient to pass all this one billion arguments, except for a stupid enumeration after the template name?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Ivan Velichko, 2015-03-01
@Ostrovski

Do this:

tpl_params = {...}  # сколько угодно строк, заполняющих dict.
return render_template("index.html", **tpl_params)

In general... There is such a practice - not to transfer a sea of ​​​​variables to the template. It seems in Rails, if you write code in RubyMine, there will even be warnings if you passed more than 1 or 2 objects to the template. There is such a pattern - View Object. This is about the fact that you need to collect all the data required by the current page into one more or less consistent (according to the interface) object and pass it to the template. With this approach, the code becomes cleaner, and it is easier to write tests for templating.

O
OnYourLips, 2015-03-01
@OnYourLips

Passing a lot of data to a view is a very bad practice.
Limit yourself to a maximum of 2 variables.
Most likely you are writing code in the controller that should be in the model, hence the bunch of extra variables.
Get into refactoring and give up this idea of ​​yours.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question