It’s simple tool to build a Single Page Application based on PHP with minimal JS require
Yes, sure. There are others similar frameworks/tools on the market. But the most good analogs have a specific.
Part of analogs were developed for single framework, like Livewire for Laravel, Sprig for Craft CMS or Turbolinks for Ruby on Rails. The another ones require a lot skills and time for implementation, like Inertiajs.
It's unnecessary to spend much time to adapt tool or to JS development for a project
The simple installation and quick start for any PHP project, that’s the main reason, why the Sparky was created.
The syntax is very similar to Livewire. Please check the next screenshots
The next code will display current time and a button to refresh the data.
Also it will refresh by itself each minute
// Clock Component namespace App\SparkyComponent; use SparkySpa\Component; class ClockComponent extends Component { public $time_now; #region Actions /** * @inheritDoc */ protected function render() { $this->time_now = date('H:i:s'); return $this->view('spa/clock'); } #endregion }
// Input the Component to a page template echo sparky(ClockComponent::getName());
// Layout `spa/clock` implementation for the Component <p spa:emit.1m="refresh()"> It is <?php echo $time_now ?> now </p> <button spa:click="refresh()" type="button">Refresh</button>
// Layout `spa/chat` implementation for the Component @if ($history) <ul> @foreach ($history as $msg) <li> {{ $msg }} </li> @endforeach </ul> @endif <input spa:bind name="message" placeholder="Put your message here" type="text"> <button spa:click="sendMessage( {{ $flag }} )" type="button">Send</button>
// Chat Component namespace App\SparkyComponent; use SparkySpa\Component; class Chat extends Component { public int $chat_id; public ?string $message = null private bool $flag = true; #region Actions /** * */ protected function mount() { // A code to define $this->chat_id } /** * @inheritDoc */ protected function render() { return $this->view('spa/chat', [ 'history' => $this->getHistory(), 'flag' => $this->flag, ]); } /** * */ public function sendMessage($flag) { // A code to save property $this->message into a DB $this->flag = !filter_var($flag, FILTER_VALIDATE_BOOLEAN); $this->message = null; } #endregion #region Getters /** * @return string[]; */ private function getHistory() { $history = []; // A code to set a history to the var $history return $history; } #endregion }