Hi, I am trying to get my head around blueprint templates. I have read the documentation but the result is kinda different from what I expect. For instance, let's say we are going to implement an admin blueprint. This admin blueprint will have its own templates, so I should set the template_folder setting and create a folder named templates under the blueprint folder. This setup works great except when you have templates with the same name which I think is fairly common. For my application I have blogs.html template for the actual application and I want to create another blogs.html template for the admin blueprint. I can easily rename it as admin_blog.html, but I think this issue should be addressed. What do you guys think about this? Thanks... Utku Utkan
Hi, On 9/17/11 7:09 AM, Utku Utkan wrote: > I am trying to get my head around blueprint templates. I have read the > documentation but the result is kinda different from what I expect. For > instance, let's say we are going to implement an admin blueprint. This > admin blueprint will have its own templates, so I should set the > template_folder setting and create a folder named templates under the > blueprint folder. Yes. But it's added to the searchpath. The proper solution is this: project_name/ admin/ templates/ admin/ index.html And then do render_template('admin/index.html'). This is also what the documentation says. Regards, Armin
> Yes. But it's added to the searchpath. The proper solution is this: > > project_name/ > admin/ > templates/ > admin/ > index.html > > And then do render_template('admin/index.html'). This is also what the > documentation says. I think the confusing part (this took me a while to figure out that there was an inconsistency, btw), was that if you don't specify the template_folder parameter to the Blueprint() ctor, it won't search in admin/templates/. i.e.: this won't work as expected: > admin = Blueprint('admin', __name__) but this does: > admin = Blueprint('admin', __name__, template_folder='templates') If you look at lib/python2.7/site-packages/flask/blueprints.py around line 81: > def __init__(self, name, import_name, static_folder=None, > static_url_path=None, template_folder=None, template_folder is set to none, but the docs say it's default value is set to 'templates'. -sc -- Sean Chittenden sean@chittenden.org
Hi,
On 9/20/11 7:02 AM, Sean Chittenden wrote:
> template_folder is set to none, but the docs say it's default value is
set to 'templates'. -sc
Ah. That was changed before the release of 0.7. Maybe something in the
docs was not updated. I will investigate that.
Regards,
Armin
Ok, I got it now. I missed that part in the documentation. Thanks for pointing that out. I thought we were supposed to add admin folder under application's templates folder not blueprint's. I wasn't expecting to repeat blueprints name there. Again, thanks for the help. On Mon, Sep 19, 2011 at 10:02 PM, Sean Chittenden <sean@chittenden.org>wrote: > > Yes. But it's added to the searchpath. The proper solution is this: > > > > project_name/ > > admin/ > > templates/ > > admin/ > > index.html > > > > And then do render_template('admin/index.html'). This is also what the > > documentation says. > > > I think the confusing part (this took me a while to figure out that there > was an inconsistency, btw), was that if you don't specify the > template_folder parameter to the Blueprint() ctor, it won't search in > admin/templates/. > > i.e.: this won't work as expected: > > > admin = Blueprint('admin', __name__) > > > but this does: > > > admin = Blueprint('admin', __name__, template_folder='templates') > > > If you look at lib/python2.7/site-packages/flask/blueprints.py around line > 81: > > > def __init__(self, name, import_name, static_folder=None, > > static_url_path=None, template_folder=None, > > template_folder is set to none, but the docs say it's default value is set > to 'templates'. -sc > > > > -- > Sean Chittenden > sean@chittenden.org > >
I would create an "admin" directory in your templates folder, and store the relevant templates there. Zach On Fri, Sep 16, 2011 at 10:09 PM, Utku Utkan <utku.utkan@gmail.com> wrote: > Hi, > > I am trying to get my head around blueprint templates. I have read the > documentation but the result is kinda different from what I expect. For > instance, let's say we are going to implement an admin blueprint. This admin > blueprint will have its own templates, so I should set the template_folder > setting and create a folder named templates under the blueprint folder. This > setup works great except when you have templates with the same name which I > think is fairly common. For my application I have blogs.html template for > the actual application and I want to create another blogs.html template for > the admin blueprint. I can easily rename it as admin_blog.html, but I think > this issue should be addressed. What do you guys think about this? > > Thanks... > > Utku Utkan >
I agree with Zach and do the same thing in my projects.
I have an admin blueprint setup like this (
http://flask.pocoo.org/snippets/59/), and also use the template decorator (
http://flask.pocoo.org/docs/patterns/viewdecorators/#templating-decorator).
So in my admin blueprint views as well as the "root" views, I can do the
same thing
@templated()
def blogs():
...
the root blogs view will use the "blogs.html" template, while the admin
blueprint blogs view with use "admin/blogs.html".
If you don't want to use the decorator, you will need to explicitly specify
the correct template for each view
# views.py
def blogs():
return render_template('blogs.html')
# admin/views.py
def blogs():
return render_template('admin/blogs.html')
Hope that helps.
On Sat, Sep 17, 2011 at 3:47 AM, Zach Williams <hey@zachwill.com> wrote:
> I would create an "admin" directory in your templates folder, and store the
> relevant templates there.
>
> Zach
>
>
> On Fri, Sep 16, 2011 at 10:09 PM, Utku Utkan <utku.utkan@gmail.com> wrote:
>
>> Hi,
>>
>> I am trying to get my head around blueprint templates. I have read the
>> documentation but the result is kinda different from what I expect. For
>> instance, let's say we are going to implement an admin blueprint. This admin
>> blueprint will have its own templates, so I should set the template_folder
>> setting and create a folder named templates under the blueprint folder. This
>> setup works great except when you have templates with the same name which I
>> think is fairly common. For my application I have blogs.html template for
>> the actual application and I want to create another blogs.html template for
>> the admin blueprint. I can easily rename it as admin_blog.html, but I think
>> this issue should be addressed. What do you guys think about this?
>>
>> Thanks...
>>
>> Utku Utkan
>>
>
>
Yeah, Zach's suggestion and this template decorator will help a lot. I will
be using them. One comment though... While generating URLs, it is possible
to give a namespace (blueprint name) and generate the URL for that
namespace. Consider this:
url_for('admin.index')
This code will generate the index URL for the admin namespace. I think there
needs to be a similar support for the render_template function. Maybe a new
parameter can be introduced so that when you say:
render_template('blogs.html', namespace='admin')
It can render the app/admin/templates/blogs.html and not
app/templates/blogs.html. I know, this problem can be solved with Zach's
suggestion, but with this way you can separate admin templates from
application templates and place them in blueprint's template folder. So
that, I can provide a blueprint with some templates and release it as a
reusable blueprint. Just an idea...
On Sat, Sep 17, 2011 at 9:34 AM, Sean Lynch <techniq35@gmail.com> wrote:
> I agree with Zach and do the same thing in my projects.
>
> I have an admin blueprint setup like this (
> http://flask.pocoo.org/snippets/59/), and also use the template decorator
> (http://flask.pocoo.org/docs/patterns/viewdecorators/#templating-decorator
> ).
>
> So in my admin blueprint views as well as the "root" views, I can do the
> same thing
>
> @templated()
> def blogs():
> ...
>
> the root blogs view will use the "blogs.html" template, while the admin
> blueprint blogs view with use "admin/blogs.html".
>
> If you don't want to use the decorator, you will need to explicitly specify
> the correct template for each view
>
> # views.py
> def blogs():
> return render_template('blogs.html')
>
> # admin/views.py
> def blogs():
> return render_template('admin/blogs.html')
>
> Hope that helps.
>
> On Sat, Sep 17, 2011 at 3:47 AM, Zach Williams <hey@zachwill.com> wrote:
>
>> I would create an "admin" directory in your templates folder, and store
>> the relevant templates there.
>>
>> Zach
>>
>>
>> On Fri, Sep 16, 2011 at 10:09 PM, Utku Utkan <utku.utkan@gmail.com>wrote:
>>
>>> Hi,
>>>
>>> I am trying to get my head around blueprint templates. I have read the
>>> documentation but the result is kinda different from what I expect. For
>>> instance, let's say we are going to implement an admin blueprint. This admin
>>> blueprint will have its own templates, so I should set the template_folder
>>> setting and create a folder named templates under the blueprint folder. This
>>> setup works great except when you have templates with the same name which I
>>> think is fairly common. For my application I have blogs.html template for
>>> the actual application and I want to create another blogs.html template for
>>> the admin blueprint. I can easily rename it as admin_blog.html, but I think
>>> this issue should be addressed. What do you guys think about this?
>>>
>>> Thanks...
>>>
>>> Utku Utkan
>>>
>>
>>
>
The template folder can be specified relative to the blueprint directory by
specifying template_folder on the Blueprint constructor:
http://flask.pocoo.org/docs/blueprints/#templates
From the docs, it appears the blueprint's templates will be at the same
template root as the application's template folder, but with lower priority,
so you'll have an issue with templates with the same name (in your original
email). You maybe do something like
admin = Blueprint('admin', __name__, template_folder='admin/templates')
That way your blueprint is all self contained, but still not worry about
overlapping template names.
On Mon, Sep 19, 2011 at 8:02 PM, Utku Utkan <utku.utkan@gmail.com> wrote:
> Yeah, Zach's suggestion and this template decorator will help a lot. I will
> be using them. One comment though... While generating URLs, it is possible
> to give a namespace (blueprint name) and generate the URL for that
> namespace. Consider this:
>
> url_for('admin.index')
>
> This code will generate the index URL for the admin namespace. I think
> there needs to be a similar support for the render_template function. Maybe
> a new parameter can be introduced so that when you say:
>
> render_template('blogs.html', namespace='admin')
>
> It can render the app/admin/templates/blogs.html and not
> app/templates/blogs.html. I know, this problem can be solved with Zach's
> suggestion, but with this way you can separate admin templates from
> application templates and place them in blueprint's template folder. So
> that, I can provide a blueprint with some templates and release it as a
> reusable blueprint. Just an idea...
>
> On Sat, Sep 17, 2011 at 9:34 AM, Sean Lynch <techniq35@gmail.com> wrote:
>
>> I agree with Zach and do the same thing in my projects.
>>
>> I have an admin blueprint setup like this (
>> http://flask.pocoo.org/snippets/59/), and also use the template decorator
>> (
>> http://flask.pocoo.org/docs/patterns/viewdecorators/#templating-decorator
>> ).
>>
>> So in my admin blueprint views as well as the "root" views, I can do the
>> same thing
>>
>> @templated()
>> def blogs():
>> ...
>>
>> the root blogs view will use the "blogs.html" template, while the admin
>> blueprint blogs view with use "admin/blogs.html".
>>
>> If you don't want to use the decorator, you will need to explicitly
>> specify the correct template for each view
>>
>> # views.py
>> def blogs():
>> return render_template('blogs.html')
>>
>> # admin/views.py
>> def blogs():
>> return render_template('admin/blogs.html')
>>
>> Hope that helps.
>>
>> On Sat, Sep 17, 2011 at 3:47 AM, Zach Williams <hey@zachwill.com> wrote:
>>
>>> I would create an "admin" directory in your templates folder, and store
>>> the relevant templates there.
>>>
>>> Zach
>>>
>>>
>>> On Fri, Sep 16, 2011 at 10:09 PM, Utku Utkan <utku.utkan@gmail.com>wrote:
>>>
>>>> Hi,
>>>>
>>>> I am trying to get my head around blueprint templates. I have read the
>>>> documentation but the result is kinda different from what I expect. For
>>>> instance, let's say we are going to implement an admin blueprint. This admin
>>>> blueprint will have its own templates, so I should set the template_folder
>>>> setting and create a folder named templates under the blueprint folder. This
>>>> setup works great except when you have templates with the same name which I
>>>> think is fairly common. For my application I have blogs.html template for
>>>> the actual application and I want to create another blogs.html template for
>>>> the admin blueprint. I can easily rename it as admin_blog.html, but I think
>>>> this issue should be addressed. What do you guys think about this?
>>>>
>>>> Thanks...
>>>>
>>>> Utku Utkan
>>>>
>>>
>>>
>>
>