Fork me on GitHub

Alias Hello World


Cet exemple montre l'export d'une méthode d'une classe comme une fonction, à l'aide d'alias.

 

The HelloWorld::sayHello(bool $isCaps) method is exported as a function with the helloWorld alias. The corresponding Ajax call is then made with rq()->helloWorld(1).

The HTML code.

<div class="row">
    <div class="col-md-12" id="hello-text">
        &nbsp;
    </div>
    <div class="col-md-4 select">
        <select class="form-select form-control" id="colorselect" name="colorselect"
                <?= attr()->on('change', rq()->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(1)) ?> >CLICK ME</button>
        <button type="button" class="btn btn-primary" <?= attr()->click(rq()->helloWorld(0)) ?> >Click Me</button>
    </div>
</div>
The Jaxon setup code.

<?php

use Jaxon\Jaxon;

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);
    }
}

$jaxon = jaxon();

$jaxon->setOption('js.lib.uri', '/js');
$jaxon->setOption('core.language', 'fr');
$jaxon->setOption('core.debug.on', false);
$jaxon->setOption('core.prefix.function', 'jaxon_');

// Register functions
$jaxon->register(Jaxon::CALLABLE_FUNCTION, 'sayHello', ['class' => 'HelloWorld', 'alias' => 'helloWorld']);
$jaxon->register(Jaxon::CALLABLE_FUNCTION, 'setColor', ['class' => 'HelloWorld']);
The Javascript code is generated from this PHP template.

jaxon.dom.ready(() => {
    // Call the HelloWorld class to populate the 2nd div
    <?= rq()->helloWorld(0) ?>;
    // call the HelloWorld->setColor() method on load
    <?= rq()->setColor(Jaxon\select('colorselect')) ?>;
});