title: Flask: Template in Blueprint Inherit from Template in App?
body: <p>I'm a total Flask/Jinja2 newbie, so maybe I'm overlooking something obvious, but:</p>
  
  <p>Shouldn't Flask, out of the box, allow a template that exists in a blueprint's <code>templates/</code> folder to extend a base template defined by my app's <code>templates/</code> folder? Shouldn't this work even if the blueprint also includes a "default" base template, which I override by defining my own base template of the same name?</p>
  
  <p>The answer to <a href="https://stackoverflow.com/q/11233959/1337498">this other SO question</a> makes me think that both of those things should absolutely be the case. Specifically the part of the answer that says:</p>
  
  <blockquote>
    <p>If there are two templates with same name[, one] in app's templates folder and [the other in] blueprint's template folder, then template in app's templates folder will get priority.</p>
  </blockquote>
  
  <p>But it's not working that way at all for me. In fact, it seems to work the <em>opposite</em> way, i.e., the <code>base.html</code> from the <strong>blueprint</strong> is being pulled in by pages defined in my app, even though my app defines its own <code>base.html</code> (which should "get priority" if the above answer is correct).</p>
  
  <p>In my app I have:</p>
  
  <pre><code>myapp/
     templates/
         base.html
         pages/
             page_base.html
             home_page.html
  </code></pre>
  
  <p>where <code>pages/home_page</code> extends <code>pages/page_base</code>, which in turn extends <code>base</code>.</p>
  
  <p>I'm also using the <code>flask_user</code> package from PyPI, which was installed (by <code>pip</code>) at <code>/usr/local/lib/python2.7/dist-packages/flask_user/</code>. Its templates folder is arranged as follows:</p>
  
  <pre><code>flask_user/
      templates/
          base.html
          flask_user/
              [templates that extend base.html]
  </code></pre>
  
  <p>This package makes its templates available to applications that use it via a Blueprint which it establishes with the following calls in the <code>init_app</code> function of its <code>UserManager</code> class (<a href="https://github.com/lingthio/Flask-User/blob/master/flask_user/__init__.py#L154" rel="nofollow noreferrer"><code>__init__.py</code>, line 154</a>):</p>
  
  <pre><code>    # Add flask_user/templates directory using a Blueprint                  
      blueprint = Blueprint('flask_user', 'flask_user', template_folder='templates')
      app.register_blueprint(blueprint)
  </code></pre>
  
  <p>My initial thinking was that by defining my own <code>myapp/templates/base.html</code> I'd be able to customize pages rendered from templates in <code>flask_user/templates/flask_user/</code> to look like other pages in my app, because  (per the referenced answer) my <code>base.html</code> should take precedence over <code>flask_user</code>'s <code>base.html</code>. </p>
  
  <p>But that's not working, and what's worse -- and much more surprising -- is that <em>my</em> app's pages are being given the default look of <code>flask_user</code>'s pages.</p>
  
  <p><strong>Digging Deeper...</strong></p>
  
  <p>Looking into @Pralhad Narsinh Sonar's suggestion that there may be a problem with the ordering of the template search paths, possibly caused by non-deterministic behavior of <code>DispatchingJinjaLoader._iter_loaders()</code> as suggested in the <a href="http://fewstreet.com/2015/01/16/flask-blueprint-templates.html" rel="nofollow noreferrer">fewstreet.com article</a> he cited, I did a quick experiment to see what ordering <code>_iter_loaders()</code> would produce for my app:</p>
  
  <pre><code>&gt;&gt;&gt; from myapp.top import app, db
  &gt;&gt;&gt; from myapp.startup import init_app.init_app
  &gt;&gt;&gt; init_app(app, db)
  &gt;&gt;&gt; app.jinja_env.loader
  &lt;flask.templating.DispatchingJinjaLoader object at 0x7f233e396dd0&gt;
  &gt;&gt;&gt; for loader in app.jinja_env.loader._iter_loaders('pages/home_page.html') :
  ...   print loader, loader.searchpath
  ... 
  &lt;jinja2.loaders.FileSystemLoader object at 0x7f233eb08490&gt; ['/var/www/python/myapp/templates']
  &lt;jinja2.loaders.FileSystemLoader object at 0x7f233e36ef10&gt; ['/usr/local/lib/python2.7/dist-packages/flask_user/templates']
  </code></pre>
  
  <p>As expected, the iterator yields the loader for my app's <code>templates/</code> folder <em>first</em>, before yielding the loader for <code>flask_user/templates/</code>. In fact, the <code>_iter_loaders()</code> function is quite deliberately structured to return the app's loader before returning <em>any</em> Blueprints' loaders. (If I'm reading the fewstreet.com article correctly, the problem it's concerned with is non-deterministic ordering among <em>multiple</em> Blueprints, which -- since there's only one Blueprint being used by my app -- isn't my current problem.)</p>
  
  <p>This result makes it even harder for me to understand why <code>flask_user</code>'s <code>base.html</code> template is being used to resolve my template's <code>{% extends "base.html" %}</code> statement*. Given that I have my own <code>base.html</code> file in <code>myapp/templates</code>, I see no reason whatsoever for the templating system to look at anything in <code>flask_user/templates</code> to render <code>myapp/templates/pages/home_page.html</code>.</p>
  
  <p><sub>* For testing purposes I got rid of the indirection through <code>pages/page_base.html</code> mentioned above.</sub></p>
  
  <p><em>So:</em> Obviously something else is going wrong, but <em>what</em>? </p>
  
  <p>I haven't yet grokked enough of the relevant code in <code>flask/templating.py</code> or <code>jinja2/loaders.py</code> to understand why and how this might be happening. This being my first foray into Flask, I would have hoped I wouldn't need to.</p>