URL Router
Automatic Page URLs
The easiest way to add a web page is to create a .tht
file in the pages
directory.
The file name is always lowerCamelCase.
The URL for each page is automatically located at the dash-case version of the file name.
File Path URL pages/hello.tht /hello pages/helloWorld.tht /hello-world pages/misc/myOtherPage.tht /misc/my-other-page
Custom Routes
You can create dynamic URLs in your settings/app.jcon
file.
Just add an entry to routes
, mapping the URL to a file in the pages
directory.
Optional parameters can be added in curly braces, but must be an entire sub-path. (e.g. /page/user{id}
is not allowed, but /page/{userId}
is okay.)
routes: { /my-url: thisPage.tht /another/url: thatOtherPage.tht /user/{userId}: user.tht }
a-z_-./
are allowed in a route. THT will automatically respond with a 404 error page if any other character is present in the URL. Trailing slashes (directory paths) are not allowed.Route Params
You can access the value of a route param with Input.route(paramName)
.
// config.jcon routes: { /blog/{articleId}/{articleSlug}: blog.tht } // blog.tht // URL: /blog/1234/my-favorite-things Input.route('articleId'); //= '1234' $slug = Web.routeParam('articleSlug'); $slug.split('-').toTitleCase(); //= 'My Favorite Things'
Route Methods
A route can specify a method appended with an @
.
This method will be automatically called instead of main
.
// e.g. Call editProfile() in `user.tht` routes: { /user/edit: user.tht@editProfile // ... }
Default Route
If a route does not exist, and you have a default.tht
file in your pages
directory, THT will call that file instead of returning a “404 Not Found” error.
You can use this to implement your own fallback routing logic.
// file: default.tht F main() { // URL: /blog/post-123 $parts = Web.request().url.pathParts; //= ['blog', 'post-123'] if $parts[0] == 'blog' { //... } }