Lenra Docs
Register on Lenra
  • Home
  • Getting started
    Open/Close
  • Guides
    Open/Close
  • Features
    Open/Close
  • References
    Open/Close
  • Contribute

    Routes

    Routes are defined in the app manifest under the lenraRoutes property. The lenraRoutes property is an array of objects, where each object represents a route. Each route object has two properties:

    Manifest example

    {
      "lenraRoutes": [
        {
          "path": "/",
          "view": {
            "type": "view",
            "name": "main"
          }
        },
        {
          "path": "/newPage",
          "view": {
            "type": "view",
            "name": "newPage"
          }
        }
      ]
    }
    

    You can utilize path parameters for your routes. For instance, you can use these routes:

    [
      {
        "path": "/",
        "view": {
          "type": "view",
          "name": "main"
        }
      },
      {
        "path": "/books/:id",
        "view": {
          "type": "view",
          "name": "bookPage",
          "find": {
            "type": "book",
            "query": {
              "id": "@route.id"
            }
          }
        }
      }
    ]
    

    By using the :id parameter, the route /books/:id will match any route. The value for :id can be used in the view query as you can see in the example above by prefixing the variable name by @route.. The :id parameter can also be fetched from inside of the view by doing as follows.

    {
        "type": "view",
        "name": "myView",
        "context": {
            "me": true,
            "pathParams": true,
        }
    }
    
    export default function (data, props, context) {
      return {
          type: "text",
          value: JSON.stringify(context), // This will be equal to {"me":"8bf756dd-0028-4bbd-b439-083add59ba54","pathParams":{"id":1}}
      }
    

    You can navigate to any route that is defined in the lenraRoutes property in the app manifest. To do that, you have to call the specific listener action @lenra:navTo. Here is an example of a button using this listener.

    {
        "type": "button",
        "text": "@lenra",
        "onPressed": {
            "action": "@lenra:navTo",
            "props": { "path": "/newPage" }
        }
    }