{ajax}

Writes an empty div and a script that has the browser fetch another page into it after the page has loaded.

Pages and execution Single tags

Syntax

{ajax 'name'}
{ajax 'name', fragment='fragmentName'}
{ajax 'name', app='other'}
{ajax 'name', $colour = 'red'}

Parameters

page
The page to fetch, named as in a URL - 'orders/list'.

Options

fragment
Ask the page for one of its response fragments alone - &padFragment=name - instead of the whole page.
app
A page of another application; under the strict check it must exist there.

Description

{ajax 'name'} does not render the page itself. It writes an empty <div> and a small script; once the page is in the browser, the script requests the named page with &padInclude - bare, without its wrappers - and puts the answer in the div. The page around it is sent at once, and a slow part arrives when it is ready. When the request fails, the div shows the status text.

The div's id is padAjax followed by a hash of the page and its query, so the markup is the same on every request. Variables assigned on the tag - {ajax 'orders', $status = 'open'} - go on the query string, and fragment='name' asks for one {fragment} of the page alone: the page renders whole on the server, but only that part is sent back.

With app='other' the page belongs to another application. Under the strict check the page must exist, in this application or the one named - a typo is reported when the template renders, not later as a 404 in the visitor's browser.

Compare {page}, which renders the page on the server in the same request, and {get}, which fetches it on the server as a second request. For a part that re-renders on clicks and forms, see {live}.

Examples

A part the browser fetches

The tag writes an empty div and a script that loads the page into it after the page arrived.

Templateexamples/ajax/1-load.pad
{ajax 'examples/ajax/parts/note'}
Resultopen alone

One fragment of a page

fragment= asks the page for that response fragment alone.

Templateexamples/ajax/2-fragment.pad
{ajax 'examples/ajax/parts/list', fragment='ajaxItems'}
Resultopen alone

See also

  • {page} Renders another page of the application - its PHP and its template - in place of the tag.
  • {get} Fetches another page of the application over HTTP, as a second request, and inserts its output.
  • {live} A region of the page that re-renders on the server when something in it is clicked, submitted or changed, and swaps itself in.
  • {fragment} A named part of the page that a request can ask for alone - for htmx and ajax swaps.