Routing
For single page apps (SPAs) or even complex pages with internal navigation, PuePy's client-side routing feature renders different pages based on the URL and provides a way of linking between various routes. Use of the router is optional and if no router is installed, the application will always render the default page.
URL Changes
In the embedded example above, the "URL" does not change because the embedded example is not a full web page. In a full web page, the URL would change to reflect the current page. Try opening the example in a new window to see the URL change.
Inspired by Flask's simple and elegant routing system, PuePy uses decorators on page classes to define routes and parameters. The router can be configured to use either hash-based or history-based routing. Consider this example's source code:
- The router is installed with the
link_mode
set toRouter.LINK_MODE_HASH
. This sets the router to use hash-based routing. - The
Link
component is a custom component that creates links to other pages. It uses the router to navigate to the specified page. - The
PetPage
class is decorated with a route. Thepet_id
parameter is parsed from the URL. - The
Link
component is used to create a link back to the homepage, as passed by thehref
parameter. - The
Link
component is used to create links to each pet's page, passing thepet_id
as a parameter.
Installing the router
The router is installed with the install_router
method on the application instance:
If you wanted to use html5 history mode (see the Router developer guide), you would set link_mode=Router.LINK_MODE_HISTORY
.
The default page
The default page is rendered for the "root" URL or when no URL is specified. The default page is defined with no path:
More information on the router
For more information on the router, see the Router Developer Guide.