librelist archives

« back to archive

Per request, change template folders

Per request, change template folders

From:
Sjoerd Huisman | Congressus
Date:
2013-07-30 @ 09:26
Hi,

I'm running Flask to host multiple websites. We have a common template
folder as a fallback, and if specific websites have specific requirements,
we want to overwrite these with specific templates.
Our folder structures looks like;
- templates
- - common
- - - a.html
- - - b.html
- - website1
- - - a.html

I have implemented a ChoiceLoader, that detects the current website and
calls the FileSystemLoader for that site first. Unfortunately, this
ChoiceLoader is initiated only on application startup. Is there a way to
redirect between multiple template folders per request?

Thanks

Re: [flask] Per request, change template folders

From:
Armin Ronacher
Date:
2013-07-30 @ 20:10
Hi,

On 30/07/2013 11:26, Sjoerd Huisman | Congressus wrote:
> I have implemented a ChoiceLoader, that detects the current website and
> calls the FileSystemLoader for that site first. Unfortunately, this
> ChoiceLoader is initiated only on application startup. Is there a way to
> redirect between multiple template folders per request?
You can make a custom loader that looks at your `g` object to decide on 
further things if you want.  Should be fairly straightforward to write.


Regards,
Armin

Re: [flask] Per request, change template folders

From:
Bruno Rocha
Date:
2013-07-30 @ 20:26
I am also interested on that, I would like to have multi-theme support so I
can set current theme in settings or db and check it per request. In this
case theme would be a folder under templates. i.e:  templates/bootstrap or
templates/foundation etc..

I am in doubt on what to do with Blueprints themes, maybe I can iterate
over app.blueprints and change its template_folder?

Tks.

---
Bruno Rocha
http://github.com/rochacbruno



On Tue, Jul 30, 2013 at 5:10 PM, Armin Ronacher <armin.ronacher@active-4.com
> wrote:

> Hi,
>
> On 30/07/2013 11:26, Sjoerd Huisman | Congressus wrote:
> > I have implemented a ChoiceLoader, that detects the current website and
> > calls the FileSystemLoader for that site first. Unfortunately, this
> > ChoiceLoader is initiated only on application startup. Is there a way to
> > redirect between multiple template folders per request?
> You can make a custom loader that looks at your `g` object to decide on
> further things if you want.  Should be fairly straightforward to write.
>
>
> Regards,
> Armin
>
>

Re: [flask] Per request, change template folders

From:
Philippe Ndiaye
Date:
2013-07-30 @ 20:35
Hi, Bruno

Concerning the theme system, another method is to set a field theme_path in
your User (or another entity) table and then store the themes somewhere in
your filesystem, say template_folder. Once this will be done, you can build
a Jinja function get_layout() which will return the layout corresponding to
the current user or site.


On Tue, Jul 30, 2013 at 10:26 PM, Bruno Rocha <rochacbruno@gmail.com> wrote:

> I am also interested on that, I would like to have multi-theme support so
> I can set current theme in settings or db and check it per request. In this
> case theme would be a folder under templates. i.e:  templates/bootstrap or
> templates/foundation etc..
>
> I am in doubt on what to do with Blueprints themes, maybe I can iterate
> over app.blueprints and change its template_folder?
>
> Tks.
>
> ---
> Bruno Rocha
> http://github.com/rochacbruno
>
>
>
> On Tue, Jul 30, 2013 at 5:10 PM, Armin Ronacher <
> armin.ronacher@active-4.com> wrote:
>
>> Hi,
>>
>> On 30/07/2013 11:26, Sjoerd Huisman | Congressus wrote:
>> > I have implemented a ChoiceLoader, that detects the current website and
>> > calls the FileSystemLoader for that site first. Unfortunately, this
>> > ChoiceLoader is initiated only on application startup. Is there a way to
>> > redirect between multiple template folders per request?
>> You can make a custom loader that looks at your `g` object to decide on
>> further things if you want.  Should be fairly straightforward to write.
>>
>>
>> Regards,
>> Armin
>>
>>
>


-- 
Regards,
*
*
*Philippe Thécou NDIAYE*
IT Student @ SUPINFO Lyon
Linux Sysadmin  & Python programmer

Re: [flask] Per request, change template folders

From:
Owein Reese
Date:
2013-07-31 @ 03:21
Have you looked at flask-split? It is supposedly for A/B testing but could
give you starting ideas. There's also flask-theme but I haven't looked into
it very deeply so can not recommend it.
On Jul 30, 2013 4:28 PM, "Bruno Rocha" <rochacbruno@gmail.com> wrote:

> I am also interested on that, I would like to have multi-theme support so
> I can set current theme in settings or db and check it per request. In this
> case theme would be a folder under templates. i.e:  templates/bootstrap or
> templates/foundation etc..
>
> I am in doubt on what to do with Blueprints themes, maybe I can iterate
> over app.blueprints and change its template_folder?
>
> Tks.
>
> ---
> Bruno Rocha
> http://github.com/rochacbruno
>
>
>
> On Tue, Jul 30, 2013 at 5:10 PM, Armin Ronacher <
> armin.ronacher@active-4.com> wrote:
>
>> Hi,
>>
>> On 30/07/2013 11:26, Sjoerd Huisman | Congressus wrote:
>> > I have implemented a ChoiceLoader, that detects the current website and
>> > calls the FileSystemLoader for that site first. Unfortunately, this
>> > ChoiceLoader is initiated only on application startup. Is there a way to
>> > redirect between multiple template folders per request?
>> You can make a custom loader that looks at your `g` object to decide on
>> further things if you want.  Should be fairly straightforward to write.
>>
>>
>> Regards,
>> Armin
>>
>>
>

Re: [flask] Per request, change template folders

From:
Sjoerd Huisman | Congressus
Date:
2013-08-01 @ 15:13
Thanks for all your replies;

ð  The before_request method does not work, as the template folders are 
loaded upon application start-up, not request start-up.

 

ð  A custom loader that looks at the g object will therefore also not 
work, once the application is loaded, you cannot change the order anymore

 

ð  It could be an interesting idea to determine the template based on a 
function, but then you have to implement this function in every end-point.
I’m hoping it can be done centralised..

 

ð  Flask-theming looks interesting, but the endpoint templates remain the 
same. It is only the extended templates or other imports you can theme. 
Not the endpoint template itself (where I’m looking for).. 

 

Are there some other idea’s?

 

Best,

Sjoerd Huisman

 

From: flask@librelist.com [mailto:flask@librelist.com] On Behalf Of Owein Reese
Sent: woensdag 31 juli 2013 05:21
To: flask@librelist.com
Subject: Re: [flask] Per request, change template folders

 

Have you looked at flask-split? It is supposedly for A/B testing but could
give you starting ideas. There's also flask-theme but I haven't looked 
into it very deeply so can not recommend it.

On Jul 30, 2013 4:28 PM, "Bruno Rocha" <rochacbruno@gmail.com> wrote:

I am also interested on that, I would like to have multi-theme support so 
I can set current theme in settings or db and check it per request. In 
this case theme would be a folder under templates. i.e:  
templates/bootstrap or templates/foundation etc..

 

I am in doubt on what to do with Blueprints themes, maybe I can iterate 
over app.blueprints and change its template_folder?

 

Tks.

 

---

Bruno Rocha

http://github.com/rochacbruno

 

 

On Tue, Jul 30, 2013 at 5:10 PM, Armin Ronacher 
<armin.ronacher@active-4.com> wrote:

Hi,


On 30/07/2013 11:26, Sjoerd Huisman | Congressus wrote:
> I have implemented a ChoiceLoader, that detects the current website and
> calls the FileSystemLoader for that site first. Unfortunately, this
> ChoiceLoader is initiated only on application startup. Is there a way to
> redirect between multiple template folders per request?

You can make a custom loader that looks at your `g` object to decide on
further things if you want.  Should be fairly straightforward to write.


Regards,
Armin

 

Re: [flask] Per request, change template folders

From:
Ron DuPlain
Date:
2013-08-01 @ 23:26
This gist shows how to select a template based on some criteria, with a default.

https://gist.github.com/rduplain/1355170

-Ron


On Thu, Aug 1, 2013 at 11:13 AM, Sjoerd Huisman | Congressus
<sjoerd@congressus.nl> wrote:
> Thanks for all your replies;
>
> ð  The before_request method does not work, as the template folders are
> loaded upon application start-up, not request start-up.
>
>
>
> ð  A custom loader that looks at the g object will therefore also not work,
> once the application is loaded, you cannot change the order anymore
>
>
>
> ð  It could be an interesting idea to determine the template based on a
> function, but then you have to implement this function in every end-point.
> I’m hoping it can be done centralised..
>
>
>
> ð  Flask-theming looks interesting, but the endpoint templates remain the
> same. It is only the extended templates or other imports you can theme. Not
> the endpoint template itself (where I’m looking for)..
>
>
>
> Are there some other idea’s?
>
>
>
> Best,
>
> Sjoerd Huisman
>
>
>
> From: flask@librelist.com [mailto:flask@librelist.com] On Behalf Of Owein
> Reese
> Sent: woensdag 31 juli 2013 05:21
> To: flask@librelist.com
> Subject: Re: [flask] Per request, change template folders
>
>
>
> Have you looked at flask-split? It is supposedly for A/B testing but could
> give you starting ideas. There's also flask-theme but I haven't looked into
> it very deeply so can not recommend it.
>
> On Jul 30, 2013 4:28 PM, "Bruno Rocha" <rochacbruno@gmail.com> wrote:
>
> I am also interested on that, I would like to have multi-theme support so I
> can set current theme in settings or db and check it per request. In this
> case theme would be a folder under templates. i.e:  temp lates/bootstrap or
> templates/foundation etc..
>
>
>
> I am in doubt on what to do with Blueprints themes, maybe I can iterate over
> app.blueprints and change its template_folder?
>
>
>
> Tks.
>
>
>
> ---
>
> Bruno Rocha
>
> http://github.com/rochacbruno
>
>
>
>
>
> On Tue, Jul 30, 2013 at 5:10 PM, Armin Ronacher
> <armin.ronacher@active-4.com> wrote:
>
> Hi,
>
>
> On 30/07/2013 11:26, Sjoerd Huisman | Congressus wrote:
>> I have implemented a ChoiceLoader, that detects the current website and
>> calls the FileSystemLoader for that site first. Unfortunately, this
>> ChoiceLoader is initiated only on application startup. Is there a way to
>> redirect between multiple template folders per request?
>
> You can make a custom loader that looks at your `g` object to decide on
> further things if you want.  Should be fairly straightforward to write.
>
>
> Regards,
> Armin
>
>

Re: [flask] Per request, change template folders

From:
Sjoerd Huisman | Congressus
Date:
2013-08-05 @ 12:13
Thanks Ron - your suggestion worked! 

Met vriendelijke groet,

Sjoerd Huisman



Rode Weeshuisstraat 25
9712 ET Groningen

050 - 211 00 16
www.congressus.nl

-----Original Message-----
From: flask@librelist.com [mailto:flask@librelist.com] On Behalf Of Ron DuPlain
Sent: vrijdag 2 augustus 2013 01:27
To: flask@librelist.com
Subject: Re: [flask] Per request, change template folders

This gist shows how to select a template based on some criteria, with a default.

https://gist.github.com/rduplain/1355170

-Ron


On Thu, Aug 1, 2013 at 11:13 AM, Sjoerd Huisman | Congressus 
<sjoerd@congressus.nl> wrote:
> Thanks for all your replies;
>
> ð  The before_request method does not work, as the template folders 
> are loaded upon application start-up, not request start-up.
>
>
>
> ð  A custom loader that looks at the g object will therefore also not 
> work, once the application is loaded, you cannot change the order 
> anymore
>
>
>
> ð  It could be an interesting idea to determine the template based on 
> a function, but then you have to implement this function in every end-point.
> I’m hoping it can be done centralised..
>
>
>
> ð  Flask-theming looks interesting, but the endpoint templates remain 
> the same. It is only the extended templates or other imports you can 
> theme. Not the endpoint template itself (where I’m looking for)..
>
>
>
> Are there some other idea’s?
>
>
>
> Best,
>
> Sjoerd Huisman
>
>
>
> From: flask@librelist.com [mailto:flask@librelist.com] On Behalf Of 
> Owein Reese
> Sent: woensdag 31 juli 2013 05:21
> To: flask@librelist.com
> Subject: Re: [flask] Per request, change template folders
>
>
>
> Have you looked at flask-split? It is supposedly for A/B testing but 
> could give you starting ideas. There's also flask-theme but I haven't 
> looked into it very deeply so can not recommend it.
>
> On Jul 30, 2013 4:28 PM, "Bruno Rocha" <rochacbruno@gmail.com> wrote:
>
> I am also interested on that, I would like to have multi-theme support 
> so I can set current theme in settings or db and check it per request. 
> In this case theme would be a folder under templates. i.e:  temp 
> lates/bootstrap or templates/foundation etc..
>
>
>
> I am in doubt on what to do with Blueprints themes, maybe I can 
> iterate over app.blueprints and change its template_folder?
>
>
>
> Tks.
>
>
>
> ---
>
> Bruno Rocha
>
> http://github.com/rochacbruno
>
>
>
>
>
> On Tue, Jul 30, 2013 at 5:10 PM, Armin Ronacher 
> <armin.ronacher@active-4.com> wrote:
>
> Hi,
>
>
> On 30/07/2013 11:26, Sjoerd Huisman | Congressus wrote:
>> I have implemented a ChoiceLoader, that detects the current website 
>> and calls the FileSystemLoader for that site first. Unfortunately, 
>> this ChoiceLoader is initiated only on application startup. Is there 
>> a way to redirect between multiple template folders per request?
>
> You can make a custom loader that looks at your `g` object to decide 
> on further things if you want.  Should be fairly straightforward to write.
>
>
> Regards,
> Armin
>
>

Re: [flask] Per request, change template folders

From:
Philippe Ndiaye
Date:
2013-07-30 @ 11:43
Hi,

I think the before_request decorator can be used for this.

@yourapplication.before_request
def before_request():
    # what you want to do

This will run the #what you want to do part before each request. Hope it
will help :)


On Tue, Jul 30, 2013 at 11:26 AM, Sjoerd Huisman | Congressus <
sjoerd@congressus.nl> wrote:

> Hi,
>
> I'm running Flask to host multiple websites. We have a common template
> folder as a fallback, and if specific websites have specific requirements,
> we want to overwrite these with specific templates.
> Our folder structures looks like;
> - templates
> - - common
> - - - a.html
> - - - b.html
> - - website1
> - - - a.html
>
> I have implemented a ChoiceLoader, that detects the current website and
> calls the FileSystemLoader for that site first. Unfortunately, this
> ChoiceLoader is initiated only on application startup. Is there a way to
> redirect between multiple template folders per request?
>
> Thanks****
>



-- 
Regards,
*
*
*Philippe Thécou NDIAYE*
IT Student @ SUPINFO Lyon
Linux Sysadmin  & Python programmer