Skip to main content

Masonite Package GitHub Workflow Status (branch) Coveralls github branch PyPI Python Version GitHub release (latest by date) License Code style: black

Introduction

Use your Masonite named routes in Javascript.

This package provides a helper that inject your Masonite application routes definition into your views. It will export a JavaScript object of your application's named routes, keyed by their names (aliases).

You can combine it with ziggy-js library as to get a global route() helper function which you can use to access your routes in your JavaScript.

Features

  • Avoid hard-coding urls client-side
  • Generate once your routes file
  • Compatible with client-side route helper ziggy-js
  • Possibility to filter routes to include client-side

Official Masonite Documentation

New to Masonite ? Please first read the Official Documentation. Masonite strives to have extremely comprehensive documentation 😃. It would be wise to go through the tutorials there. If you find any discrepencies or anything that doesn't make sense, be sure to comment directly on the documentation to start a discussion!

Hop on Masonite Discord Community to ask any questions you need!

Installation

pip install masonite-js-routes

Configuration

Add JSRoutesProvider to your project in config/providers.py:

# config/providers.py
# ...
from masonite.js_routes import JSRoutesProvider

# ...
PROVIDERS = [
    # ...

    # Third Party Providers
    JSRoutesProvider,

    # ...
]

Then you can publish the configuration file in your project if you need to change some parameters:

python craft package:publish js_routes

Usage

⚠️ The routes must have a name, else the package won't pick up your routes ! Ensure you're using Route.get()...name("some_name").

  1. Using routes view helper: routes will be generated at each request and included client-side in the page.
  2. Generating routes as Javascript file: routes are generated once in a file and you load this file client-side. When you update your routes, you must regenerate the file.

1. Using routes view helper

  1. In your views, just add this helper where you want to get Ziggy routes as a Javascript object (e.g. in <head></head> or at the end of body).
{{ routes() }}

Basic filtering

Only the routes matching a list of name patterns will be included:

FILTERS = {
    "only": ["welcome.*"],
}

Only the routes which does not match a list of name patterns will be included:

FILTERS = {
    "except": ["admin.*"],
}

Note: You have to choose one or the other. Setting both only and except will disable filtering altogether and simply return the default list of routes.

Filtering using Groups

Only the routes matching a group or a list of groups will be included if groups are passed to the helpers

FILTERS = {
    "groups": {
        "app": ["app.*"],
        "api": ["api.*"],
    }
}

and in your view :

{{ routes('app') }}

or a list of group names

{{ routes(['app', 'api']) }}

Note: Passing group names to the routes helper will always take precedence over your other only or except settings.

2. Generating routes as Javascript file

You can also generate once the routes as a Javascript file. For now it generates a file exporting Ziggy object routes as it is made to use it with ziggy-js.

To generate the routes, run the craft command (it takes an optional --path argument to change the output path):

python craft js_routes:generate

(You could add this into a pipeline, to regenerate it whenever needed).

You will get a file like this:

var Ziggy = {
  routes: {
    home: { uri: "", methods: ["GET", "HEAD"], domain: null },
    login: { uri: "login", methods: ["GET", "HEAD"], domain: null },
  },
  url: "http://ziggy.test",
  port: null,
  defaults: {},
};

export { Ziggy };

Client-side usage

Then to be able to use it client-side you can refer to ziggy-js documentation. (Note that you don't have to use this library you can use the routes objects as you like.)

Quick explanation with Vue.js

Install the library:

npm install ziggy-js

Then in your Vue.js entrypoint you could for example define a global route() mixin (using the route() method from ziggy-js).

  • With the option 1, Ziggy will be available in the global javascript window scope
// app.js
import { createApp, h } from "vue";
import route from "ziggy-js";

import App from "./App.vue";

const app = createApp(App).mixin({
  methods: {
    route(name, params = {}, absolute = true) {
      return route(name, params, absolute, window.Ziggy);
    },
  },
});

// App.vue
// ...
this.route("users", 2); // == http://ziggy.test/users/2
  • With the option 2, the routes config must be imported from the generated file (instead of the window object)
// app.js
import { createApp, h } from "vue";
import route from "ziggy-js";
import { Ziggy } from "./routes";

import App from "./App.vue";

const app = createApp(App).mixin({
  methods: {
    route(name, params = {}, absolute = true) {
      return route(name, params, absolute, Ziggy);
    },
  },
});

// App.vue
// ...
this.route("users", 2); // == http://ziggy.test/users/2

Content Security Policy

A Content Security Policy may block unsafe inline scripts which Ziggy uses to pass the routes to JavaScript. By adding a nonce to your CSP you can allow certain inlined scripts. To add this nonce to Ziggy you can pass it as the second argument to the helper:

{{ routes(false, '[YOUR_NONCE]') }}

Contributing

Please read the Contributing Documentation here.

Maintainers

License

Masonite JS Routes is open-sourced software licensed under the MIT license.

Metadata

Release files for masonite-js-routes 2.0.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for masonite-js-routes 2.0.5
File Size Uploaded
masonite-js-routes-2.0.5.tar.gz 204.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for masonite-js-routes 2.0.5
File Interpreter ABI Platform
masonite_js_routes-2.0.5-py3-none-any.whl Python 3 none any Details

Total release size: 216.1 kB

Release files / masonite-js-routes-2.0.5.tar.gz

Download URL masonite-js-routes-2.0.5.tar.gz
Size 204.4 kB
Tags Source
SHA-256 checksum
How to use checksums
0076474dc40612f83a68feec56c7284b9c3211994878870b8dd0d96d9c3ca6d0
BLAKE2b-256 checksum
How to use checksums
d8f88866cd2487356b3ad0640053cd4f46b94ccabc74409ffe58a2e18abd64d4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.4

Release files / masonite_js_routes-2.0.5-py3-none-any.whl

Download URL masonite_js_routes-2.0.5-py3-none-any.whl
Size 11.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8575b5645483d51d6fec4b081799feb997bf82706c51fae8c37644472643eba1
BLAKE2b-256 checksum
How to use checksums
41b74fb8998a08a365e203e248ce9b254e859b7e09af82532276a004c72d5a37
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.4

Release history Release notifications | RSS feed

This release

2.0.5 This release

2 release files

2.0.4

2 release files

2.0.3

2 release files

2.0.2

2 release files

2.0.1

2 release files

2.0.0

2 release files

1.0.0

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page