Render_Template

--

So we are back on the track. We started with flask framework in our last technical blog and if you need to recall some of the concepts, you can go through the last blog (https://medium.com/@sharma.ashish.1996/next-step-into-the-web-3a23f2c298d6). I also posted a video regarding the same on my facebook page Unofficial: Ashish Sharma, if you want you can also check that (https://www.facebook.com/ashishsharma260696/videos/192283848010860/). Okay so enough of the promotion and let’s dive straight into our topic for today which is the templates. Today’s topic would be short and crisp, so let’s start.

If you remember, in our last blog we passed a string/integer with our address so that w.r.t that our display changes. But that was only a one liner and usually we will have a lot of content, so that is not an accurate way of using our html code. To get us out of this trouble, templates come to rescue. Basically, we will be storing our html files in a folder named templates and then we have to just call these files from their location. So instead of lots of lines in our function, we will just have a single line to call our template. Let the name of our file be medium.html.

We will be using ATOM as our text editor to write our html code as it has a lot of inbuilt functions. For example, we just need to write “html” and hit enter and then a bunch of basic lines would appear which are mandatory for all the codes. It is like our header files which we need to include in all of our code. So, I would recommend to use ATOM text editor. We will be discussing HTML in detail in the upcoming blogs, for now just understand that we will be using the html code inside this file with the help of templates. Below you can see our html file in the atom editor,

The paragraph is a random text which can be printed by typing “lorem”, we will discuss everything later.

After saving this file in our templates folder, we can move on to our python script. To use this template, we need to import the render_template package from flask. After importing, we can just return our template inside the function. Our python script will have the following code:

Our html code should be inside templates folder only, else it would not run.

Now if we visit our address/medium, we can see your webpage,

At last, we will see how to display the content based on our input as we saw earlier, but this time using templates. For both integer and strings, we just have to call the function and pass the parameter in the same manner as follows:

The only difference is that, here our parameter will be inside the {{ }} in our html code and we have to use the same name for our parameter in both our script and html code, we can do it as follows:

Similarly we can write for our profile.

That’s all for today, we will discuss more about html and flask framework in our next blog, till then learn everyday and keep coding…

Thank you for reading, if you liked the article please click on the clap button and show your support.

--

--