Skip to content
Ross Morsali edited this page May 22, 2022 · 6 revisions

The transform support direct compilation to the PHP language.

Once you have generated your pre-render build, it should be saved as a PHP file and included in your application.

Including the template

Once you have generated a PHP file from your pre-render output, it is recommended to include this in your application using include:

<?php
	include dirname(__FILE__) . '/path/to/template.php';
?>

You could use output buffering to capture the output to a variable to use elsewhere:

<?php
	ob_start();
	include dirname(__FILE__) . '/path/to/template.php';
	$output = ob_get_clean();
?>

Supplying data

The PHP code will use $data as the main object for all variables referenced throughout the template, and exposed template vars should be accessible via array keys with the same name.

const HelloWorld = ({ name }) => (
  <div>
    <h1>Hello {name}</h1>
  </div>
);

This will generate the output:

<div>
    <h1>Hello <?php echo $data['name'] ?></h1>
</div>

Therefor an object must be created in the same scope such as:

<?php
	$data = array(
		'name' => 'Mary',
	);
	include dirname(__FILE__) . '/path/to/template.php';
?>

Escaping output and customising variable names

If you want to supply a custom escaping function (you probably should), then its best to duplicate the PHP language config, and supply this as a custom language, with your escaping functions included in the output.

An example of this can be found on the custom language page.

The same with defining the variable names.

By default, we use the parent variable name of $data, and for loops and arrays we use $item.

These can be changed when using a custom language.