Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Flask Basics Character Builder Starting the Builder

Carlos Rengifo
Carlos Rengifo
6,968 Points

Best practices when embedding python with HTML

I've read several times about how you shouldn't combine HTML code (model) with server-side code (controller), and according to Kenneth this is even encouraged when working with Flask. Could someone help me understand when should I do it and when not to?

3 Answers

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

You can't really embed Python into HTML. No browser processes Python and I don't know of any server that'll process and inject Python automatically. You can, of course, use Python to generate HTML. This is a big part of what Django and Flask (and other web frameworks) do.

I'm not 100% certain exactly what you're asking, though. Can you give an example?

Carlos Rengifo
Carlos Rengifo
6,968 Points

Yes, I think I didn't express myself correctly. My question is more about separating the view from the controller when coding with Jinja2. What we do with Jinja2 looks so much like inline scripting that I wonder if there's a way to have "clean" HTML and the templating engine stuff somewhere else.

Sorry if I sound confusing, my english isn't the best!

Mustafa Bayramov
Mustafa Bayramov
4,778 Points

Django templates. Django restrict what you can use inside a template so most of the logic has to be in a view (in other terminology a controller).

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Like Mustafa Bayramov said, Django restricts a lot more than Jinja2 does.

Jinja2, though, isn't inline scripting. Any code you use in Jinja2 has to exist in Jinja2's parser, so it's guaranteed to be valid, good Python. You can't do 100% of Python in there, either, so you're limited to a sane set of functionality. The only variables that Jinja2 has access to are the ones you provide to the renderer, so scoping isn't really dangerous either.

Why not just play around with Jinja2 directly?

As for having "clean" HTML, that's...pretty hard to do. At some point, you're going to want to get a value from your backend to your client. So your choices, then, are to generate the client's output by piecing together strings (ew), using a templating library like Jinja2, or only fetching data from the backend through JavaScript (works but CSRF is a pain and might require a lot of roundtrips).

Using a template library, especially one that sanitizes output, controls variables, and has a limited scope of functions, is miles (solar systems even) away from the days of embedded PHP in templates.

Carlos Rengifo
Carlos Rengifo
6,968 Points

Oh I see that I was a bit biased against Jinja and its functionality, thank you!