librelist archives

« back to archive

render_template with path

render_template with path

From:
Tom Whitcomb
Date:
2013-04-19 @ 03:24
Hi All,

I am a long time developer but just recently started using Flask.  I love 
it for its smallness and it always seems to have what I need.  I feel like
I get this great,simple framework with the really important parts yet I am
free to do what I need.

Long live Flask.

Sorry about the love fest.  Now for my question.

I am using a 'path/file.html'  in the render_template() and it works 
nicely.  Lets me subdivide my template directory into functional areas.  
My questions is, I read about the render_template API and I could never 
find a reference to it taking a path.  The documentation seems to only 
show file names.

Is render_template supposed to take a path or filename or did I find a 
side affect?

Tom

Re: [flask] render_template with path

From:
伍五
Date:
2013-04-19 @ 03:46
Hi tom, when you init with the Flask, there is a parameter called
template_folder,
you can choose your path where is your html file.

def __init__(self, import_name, static_path=None, static_url_path=None,
                 static_folder='static', template_folder='templates',
                 instance_path=None, instance_relative_config=False):

app = Flask(__name__, template_folder='path/templates')

Im not sure if there is what you need ~


2013/4/19 Tom Whitcomb <thomaswhitcomb@yahoo.com>

> Hi All,
>
> I am a long time developer but just recently started using Flask.  I love
> it for its smallness and it always seems to have what I need.  I feel like
> I get this great,simple framework with the really important parts yet I am
> free to do what I need.
>
> Long live Flask.
>
> Sorry about the love fest.  Now for my question.
>
> I am using a 'path/file.html'  in the render_template() and it works
> nicely.  Lets me subdivide my template directory into functional areas.  My
> questions is, I read about the render_template API and I could never find a
> reference to it taking a path.  The documentation seems to only show file
> names.
>
> Is render_template supposed to take a path or filename or did I find a
> side affect?
>
> Tom
>
>

Re: [flask] render_template with path

From:
Tom Whitcomb
Date:
2013-04-19 @ 04:49
Almost but not quite.  For example

a/templates/b/login.html
a/templates/c/login.html


So I create my app like this:

app = Flask(......,template_folder='a/templates')

Then in my controllers I do a render_template like

render_template('b/login.hml',........)

It's OK to use a path/file in the render_template?

Tom


>________________________________
> From: 伍五 <wukehong86@gmail.com>
>To: flask@librelist.com 
>Sent: Thursday, April 18, 2013 8:46 PM
>Subject: Re: [flask] render_template with path
> 
>
>
>Hi tom, when you init with the Flask, there is a parameter 
called template_folder, you can choose your path where is your html file. 
>
>
>def __init__(self, import_name, static_path=None, static_url_path=None,
>                 static_folder='static', template_folder='templates',
>                 instance_path=None, instance_relative_config=False):
>
>
>app = Flask(__name__, template_folder='path/templates')
>
>
>Im not sure if there is what you need ~
>
>
>
>2013/4/19 Tom Whitcomb <thomaswhitcomb@yahoo.com>
>
>Hi All,
>>
>>I am a long time developer but just recently started using Flask.  I 
love it for its smallness and it always seems to have what I need.  I feel
like I get this great,simple framework with the really important parts yet
I am free to do what I need.
>>
>>Long live Flask.
>>
>>Sorry about the love fest.  Now for my question.
>>
>>I am using a 'path/file.html'  in the render_template() and it works 
nicely.  Lets me subdivide my template directory into functional areas.  
My questions is, I read about the render_template API and I could never 
find a reference to it taking a path.  The documentation seems to only 
show file names.
>>
>>Is render_template supposed to take a path or filename or did I find a 
side affect?
>>
>>Tom
>>
>>
>>
>
>
>

Re: [flask] render_template with path

From:
René Neumann
Date:
2013-04-19 @ 06:37
To ease managing the pathes to the templates you could take a look here
[1], so you can write

(in Blueprint "a"):

@templated()
def login (...):
 ... # renders a/login.html

(in Blueprint "b"):

@templated()
def login (...):
 ... # renders b/login.html

And each time it will pick up the correct template file by using
template_folder/blueprint_name/endpoint.

An enhanced version is here [2] -- it now allows names in the style of
url_for (restricted to current blueprint):

(in Blueprint "a")

@templated()
def login (...):
    ... # renders a/login.html

@templated(".login")
def login_with_id (id, ...):
    ... # renders a/login.html

@templated("index.html")
def show(...):
    ... # renders index.html

Regards,
René

[1]
http://flask.pocoo.org/docs/patterns/viewdecorators/#templating-decorator
[2] http://git.necoro.eu/web/kosten.git/tree/app/utils.py

Re: [flask] render_template with path

From:
伍五
Date:
2013-04-19 @ 05:02
Yep , your example is current.
path/file in the render_template is for sure, but this path(folder) must
inside the template_folder.

With blueprint, you can change the blueprint template_folder also .


2013/4/19 Tom Whitcomb <thomaswhitcomb@yahoo.com>

> Almost but not quite.  For example
>
> a/templates/b/login.html
> a/templates/c/login.html
>
> So I create my app like this:
>
> app = Flask(......,template_folder='a/templates')
>
> Then in my controllers I do a render_template like
>
> render_template('b/login.hml',........)
>
> It's OK to use a path/file in the render_template?
>
> Tom
>
>   ------------------------------
>  *From:* 伍五 <wukehong86@gmail.com>
> *To:* flask@librelist.com
> *Sent:* Thursday, April 18, 2013 8:46 PM
> *Subject:* Re: [flask] render_template with path
>
> Hi tom, when you init with the Flask, there is a parameter called 
template_folder,
> you can choose your path where is your html file.
>
> def __init__(self, import_name, static_path=None, static_url_path=None,
>                  static_folder='static', template_folder='templates',
>                  instance_path=None, instance_relative_config=False):
>
> app = Flask(__name__, template_folder='path/templates')
>
> Im not sure if there is what you need ~
>
>
> 2013/4/19 Tom Whitcomb <thomaswhitcomb@yahoo.com>
>
> Hi All,
>
> I am a long time developer but just recently started using Flask.  I love
> it for its smallness and it always seems to have what I need.  I feel like
> I get this great,simple framework with the really important parts yet I am
> free to do what I need.
>
> Long live Flask.
>
> Sorry about the love fest.  Now for my question.
>
> I am using a 'path/file.html'  in the render_template() and it works
> nicely.  Lets me subdivide my template directory into functional areas.  My
> questions is, I read about the render_template API and I could never find a
> reference to it taking a path.  The documentation seems to only show file
> names.
>
> Is render_template supposed to take a path or filename or did I find a
> side affect?
>
> Tom
>
>
>
>
>