Correct way of using JINJA template for title in flask

Question:

0

`vectorizer = TfidfVectorizer(analyzer=’word’,norm=None, use_idf=True,smooth_idf=True) tfIdfMat = vectorizer.fit_transform(df[‘Description’]) feature_names = sorted(vectorizer.get_feature_names())

docList=[‘df.Description’] #skDocsTfIdfdf = pd.DataFrame(tfIdfMat.todense(),index=sorted(docList), columns=feature_names) #print(skDocsTfIdfdf)

for col in tfIdfMat.nonzero()[1]: print (feature_names[col], ‘-‘ , tfIdfMat[0, col])`

Asked By: Mr xyz

||

Answers:

The way I would do the title is have something like this in your base.html file

{% if title %}
     <title>Home - {{title}}</title>
{%else%}
    <title>Home</title>
{%endif%}

And then for each of your routes when you render the template you can set a title, if no title is set then the title will just be Home.

For example

retrun render_template("about.html", title = "About")

Would give you the title "Home – About"

If you want to use your method of doing it, I think what you have done should work, but in the base.html file you can change {% endblock title %} to just
{%endblock%}, and do the same in the other html file. Maybe that will solve your issue?

I hope that helps, sorry if I have misunderstood what you wanted.

Answered By: MarcusWilliams

To achieve this, it is better to use jinja macros. here is how:

my_macros.html

{% macro render_title(arg1, arg2) %}

     # write your jinja html here

{% endmacro%}

{% macro render_something(arg1, arg2) %}

     # write your jinja html here

{% endmacro%}

Then in your html where you want to use the macro:

{% extends 'base.html' %}
{% from 'my_macros.html' import render_title %}
{% block content %}

    {{ render_title("something", "something else") }}

    # jinja html anywhere

{% endblock content %}
Answered By: Elie Saad
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.