Skip to content

Latest commit

 

History

History
92 lines (66 loc) · 3.79 KB

README.adoc

File metadata and controls

92 lines (66 loc) · 3.79 KB

Inertia.js for ProcessWire

Inertia allows you to create fully client-side rendered, single-page apps, without much of the complexity that comes with modern SPAs. It does this by leveraging existing server-side frameworks.

Inertia isn’t a framework, nor is it a replacement to your existing server-side or client-side frameworks. Rather, it’s designed to work with them. Think of Inertia as glue that connects the two. Inertia comes with three official client-side adapters (React, Vue, and Svelte).

This is an adapter for ProcessWire. Inertia replaces PHP views altogether by returning JavaScript components from controller actions. Those components can be built with your frontend framework of choice.

Features

Installation

You only need to create a file named templates/app.view.php. This file will be rendered when calling $inertia→render(). This view will receive a param named $inertia that you can use to render the app div.

  • $inertia→page: The properties that will render.

  • $inertia→json: The inertia page as a json string.

  • $inertia→tag: An html div tag with the json string.

Use $inertia→tag to render Inertia’s root element. If you want more custom element you can use the $inertia→json variable.

<div id='app' data-page='{$inertia->json}'></div>

Example templates/app.view.php

<!DOCTYPE html>
<html lang='en'>
<head>
  <meta charset='utf-8' />
  <meta name='viewport' content='width=device-width, initial-scale=1.0, maximum-scale=1.0'>
  <title>Hello World</title>
  <!-- Your css and js files -->
  <link href='/css/app.css' rel='stylesheet'>
  <script src='/js/app.js' defer></script>
</head>
<body>
  <?php echo $inertia->tag ?>
</body>
</html>

Rendering

This adapter follows the Inertia Protocol.

The very first request to an Inertia app is just a regular full-page browser request, with no special Inertia headers or data. For these requests, the server returns a full HTML document.

Once an Inertia app has been booted, all subsequent requests to the site are made via XHR with a special X-Inertia header set to true. This header indicates that the request is being made by Inertia, and isn’t a standard full-page visit.

When the server detects the X-Inertia header, instead of responding with a full HTML document, it returns a JSON response with an encoded page object.

Using Inertia with ProcessWire is easy. Just call the render function passing the name of your Component and your Properties.

$inertia = $modules->get('Inertia');
echo $inertia->render('MyComponent', ["my" => "props"]);

Example Project

A sample project is provided at https://github.com/joyofpw/inertia-svelte-mix-pw

Credits

Made with by Ninjas.