Updated blueprint documentation
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Armin Ronacher · committed by Armin Ronacher · parent 2647bec · Browse files · 1 file·+89 added−1 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+89 added−1 removed
- other:+89 added−1 removed
Provenance
- Commit
59a92eb- Tree
12dd1ac- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
2647bec
1 file changed+89 added−1 removed
modified:docs/blueprints.rst+89 added-1 removed
55 unmodified lines56 @simple_page.route('/', defaults={'page': 'index'})57 @simple_page.route('/<page>')58 def show(page):59 try:60- return render_template('simple_pages/%s.html' % page)60+ return render_template('pages/%s.html' % page)61 except TemplateNotFound:62 abort(404)63+64+When you bind a function with the help of the ``@simple_page.route``65+decorator the blueprint will record the intention of registering the66+function `show` on the application when it's later registered.67+Additionally it will prefix the endpoint of the function with the68+name of the blueprint which was given to the :class:`Blueprint`69+constructor (in this case also ``simple_page``).70+71+So how do you register that blueprint? Like this::72+73+ from flask import Flask74+ from yourapplication.simple_page import simple_page75+76+ app = Flask(__name__)77+ app.register_blueprint(simple_page)78+79+If you check the rules registered on the application, you will find80+these::81+82+ [<Rule '/static/<filename>' (HEAD, OPTIONS, GET) -> static>,83+ <Rule '/<page>' (HEAD, OPTIONS, GET) -> simple_page.show>,84+ <Rule '/' (HEAD, OPTIONS, GET) -> simple_page.show>]85+86+The first one is obviously from the application ifself for the static87+files. The other two are for the `show` function of the ``simple_page``88+blueprint. As you can see, they are also prefixed with the name of the89+blueprint and separated by a dot (``.``).90+91+Blueprint Resources92+-------------------93+94+Blueprints can provide resources as well. Sometimes you might want to95+introduce a blueprint only for the resources it provides.96+97+Blueprint Resource Folder98+`````````````````````````99+100+Like for regular applications, blueprints are considered to be contained101+in a folder. While multiple blueprints can origin from the same folder,102+it does not have to be the case and it's usually not recommended.103+104+The folder is infered from the second argument to :class:`Blueprint` which105+is ususally `__name__`. This argument specifies what logical Python106+module or package corresponds to the blueprint. If it points to an actual107+Python package that package (which is a folder on the filesystem) is the108+resource folder. If it's a module, the package the module is contained in109+will be the resource folder. You can access the110+:attr:`Blueprint.root_path` property to see what's the resource folder::111+112+ >>> simple_page.root_path113+ '/Users/username/TestProject/yourapplication'114+115+To quickly open sources from this folder you can use the116+:meth:`~Blueprint.open_resource` function::117+118+ with simple_page.open_resource('static/style.css') as f:119+ code = f.read()120+121+Static Files122+````````````123+124+A blueprint can expose a folder with static files by providing a path to a125+folder on the filesystem via the `static_folder` keyword argument. It can126+either be an absolute path or one relative to the folder of the127+blueprint::128+129+ admin = Blueprint('admin', __name__, static_folder='static')130+131+By default the rightmost part of the path is where it is exposed on the132+web. Because the folder is called ``static`` here it will be available at133+the location of the blueprint + ``/static``. Say the blueprint is134+registered for ``/admin`` the static folder will be at ``/admin/static``.135+136+The endpoint is named `blueprint_name.static` so you can generate URLs to137+it like you would do to the static folder of the application::138+139+ url_for('admin.static', filename='style.css')140+141+Templates142+`````````143+144+If you want the blueprint to expose templates you can do that by providing145+the `template_folder` parameter to the :class:`Blueprint` constructor::146+147+ admin = Blueprint('admin', __name__, template_folder='templates')148+149+As for static files, the path can be absolute or relative to the blueprint150+resource folder.