Classe Hello World
Cet exemple montre l'export d'une classe avec Jaxon.
The global function rq() takes a class name as parameter, and returns a call factory for the methods in that class.
The calls are made using a fluent syntax, for example rq('HelloWorld')->sayHello(1).
The HTML code.
<div class="row">
<div class="col-md-12" id="hello-text">
</div>
<div class="col-md-4 select">
<select class="form-select form-control" id="colorselect" name="colorselect" <?= attr()
->on('change', rq('HelloWorld')->setColor(Jaxon\select('colorselect'))) ?>>
<option value="black" selected="selected">Black</option>
<option value="red">Red</option>
<option value="green">Green</option>
<option value="blue">Blue</option>
</select>
</div>
<div class="col-md-8 buttons">
<button type="button" class="btn btn-primary" <?= attr()
->click(rq('HelloWorld')->sayHello(1)) ?> >CLICK ME</button>
<button type="button" class="btn btn-primary" <?= attr()
->click(rq('HelloWorld')->sayHello(0)) ?> >Click Me</button>
</div>
</div>
The Jaxon library config
The Jaxon setup code.
<?php
class HelloWorld
{
public function sayHello(bool $isCaps)
{
$text = $isCaps ? 'HELLO WORLD!' : 'Hello World!';
$xResponse = jaxon()->newResponse();
$xResponse->assign('hello-text', 'innerHTML', $text);
}
public function setColor(string $sColor)
{
$xResponse = jaxon()->newResponse();
$xResponse->assign('hello-text', 'style.color', $sColor);
}
public function showError($sMessage)
{
$xResponse = jaxon()->newResponse();
$xResponse->assign('hello-text', 'innerHTML', $sMessage);
}
}
// Register object
$jaxon = jaxon();
$jaxon->app()->setup(configFile('class.php'));
// Js options
$jaxon->setOptions(['lib' => ['uri' => '/js'], 'app' => ['minify' => false]], 'js');
The Javascript code is generated from this PHP template.
jaxon.dom.ready(() => {
// Call the HelloWorld class to populate the 2nd div
<?= rq('HelloWorld')->sayHello(0) ?>;
// call the HelloWorld->setColor() method on load
<?= rq('HelloWorld')->setColor(Jaxon\select('colorselect')) ?>;
});