Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jinja2 Default Page Title

I'm attempting to set a default page title for all pages in a Flask application.

I want to insert this into the default layout template. I have a site name in my settings.

<title>{{title}}</title}}

The problem is, I cannot seem to import the site name from the settings to the layout template.

<title>{{settings.SETTING}}</title}}

and similar variations do not work.

Is there an easy way to do this; I don't want to have to set the title with every action in my controller, and the logical way is go from my settings to the template. I just don't see a way right now, any input appreciated.

edit:

I'd prefer not to write an extension, I see another part of my app is using an extension to pull variables into the template, but writing an entire extension is a bit beyond my time investment at the time.

edit:

def page_title(title):
    return settings.SITE_NAME

app.jinja_env.filters['page_title'] = page_title

in template:

{{ title | page_title}} 

which is something close, i want to set title OR override with a default and that is a first step that works

like image 758
blueblank Avatar asked Dec 21 '22 16:12

blueblank


1 Answers

Use a block:

<title>{% block title %}{{ settings.SETTING }}{% endblock %}</title>

Then you can simply override that block in a template inheriting from your base template if you want to change the title.

{% block title %}your custom title{% endblock %} 
like image 160
ThiefMaster Avatar answered Dec 28 '22 09:12

ThiefMaster