Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/clickfwd/yoyo

Yoyo is a full-stack PHP framework to create rich, dynamic interfaces using server-rendered HTML. You keep on writing PHP and let Yoyo make your creations come alive.
https://github.com/clickfwd/yoyo

blade livewire reactive reactive-components twig

Last synced: 1 day ago
JSON representation

Yoyo is a full-stack PHP framework to create rich, dynamic interfaces using server-rendered HTML. You keep on writing PHP and let Yoyo make your creations come alive.

Awesome Lists containing this project

README

        

# Yoyo

Yoyo is a full-stack PHP framework that you can use on any project to create rich dynamic interfaces using server-rendered HTML.

With Yoyo, you create reactive components that are seamlessly updated without the need to write any Javascript code.

Yoyo ships with a simple templating system, and offers out-of-the-box support for [Blade](https://laravel.com/docs/8.x/blade), without having to use Laravel, and [Twig](https://twig.symfony.com/).

Inspired by [Laravel Livewire](https://laravel-livewire.com/) and [Sprig](https://putyourlightson.com/plugins/sprig), and using [htmx](https://htmx.org/).

## 🚀 Yoyo Demo Apps

Check out the [Yoyo Demo App](https://app.getyoyo.dev) to get a better idea of what you can build with Yoyo. It showcases many different types of Yoyo components. You can also clone and install the demo apps:

- [Yoyo Blade App](https://github.com/clickfwd/yoyo-blade-app)
- [Yoyo Laravel App](https://github.com/clickfwd/yoyo-laravel-app)
- [Yoyo PHP template App](https://github.com/clickfwd/yoyo-app)
- [Yoyo Twig App](https://github.com/clickfwd/yoyo-twig-app)

## Documentation

- [How it Works](#how-it-works)
- [Installation](#installation)
- [Updating](#updating)
- [Configuring Yoyo](#configuring-yoyo)
- [Creating Components](#creating-components)
- [Rendering Components](#rendering-components)
- [Properties](#properties)
- [Actions](#actions)
- [View Data](#view-data)
- [Computed Properties](#computed-properties)
- [Events](#events)
- [Redirecting](#redirecting)
- [Component Props](#component-props)
- [Query String](#query-string)
- [Loading States](#loading-states)
- [Using Blade](#using-blade)
- [Using Twig](#using-twig)
- [License](#license)

## How it Works

Yoyo components are rendered on page load and can be individually updated, without the need for page-reloads, based on user interaction and specific events.

Component update requests are sent directly to a Yoyo-designated route, where it processes the request and then sends the updated component HTML partial back to the browser.

Yoyo can update the browser URL state and trigger browser events straight from the server.

Below you can see what a Counter component looks like:

**Component class**

```php
# /app/Yoyo/Counter.php

count++;
}
}
```

**Component template**

```html

+


```

Yes, it's that simple! One thing to note above is the use of the protected property `$props`. This indicates to Yoyo that the `count` variable, which is not explicitly available within the template, should be persisted and updated in every request.

## Installation

### Install the Package

```bash
composer require clickfwd/yoyo
```

#### Phalcon Framework Installation

For phalcon, you need to add di

```php
$di->register(new \Clickfwd\Yoyo\YoyoPhalconServiceProvider());
```

and you need to add router:

```php
$router->add('/yoyo', [
'controller' => 'yoyo',
'action' => 'handle',
]);
```

and you should create a controller and inherit from `Clickfwd\Yoyo\PhalconController` class.

## Updating

After performing the usual `composer update`, remember to also update the `yoyo.js` script per the [Load Assets](#load-assets) instructions.

## Configuring Yoyo

It's necessary to bootstrap Yoyo with a few configuration settings. This code should run when rendering and updating components.

```php
use Clickfwd\Yoyo\View;
use Clickfwd\Yoyo\ViewProviders\YoyoViewProvider;
use Clickfwd\Yoyo\Yoyo;

$yoyo = new Yoyo();

$yoyo->configure([
'url' => '/yoyo',
'scriptsPath' => 'app/resources/assets/js/',
'namespace' => 'App\\Yoyo\\'
]);

// Register the native Yoyo view provider
// Pass the Yoyo components' template directory path in the constructor

$yoyo->registerViewProvider(function() {
return new YoyoViewProvider(new View(__DIR__.'/resources/views/yoyo'));
});
```

**'url'**

Absolute or relative URL that will be used to request component updates.

**'scriptsPath'**

The location where you copied the `yoyo.js` script.

**'namespace'**

This is the PHP class namespace that will be used to discover auto-loaded dynamic components (components that use a PHP class).

If the namespace is not provided or components are in different namespaces, you need to register them manually:

```php
$yoyo->registerComponents([
'counter' => App\Yoyo\Counter::class,
];
```

You are required to load the component classes at run time, either using a `require` statement to load the component's PHP class file, or by including your component namespaces in you project's `composer.json`.

Anonymous components don't need to be registered, but the template name needs to match the component name.

### Load Assets

Find `yoyo.js` in the following vendor path and copy it to your project's public assets directory.

```file
/vendor/clickfwd/yoyo/src/assets/js/yoyo.js
```

To load the necessary scripts in your template add the following code inside the `` tag:

```php

```

## Creating Components

Dynamic components require a class and a template. When using the Blade and Twig view providers, you can also use inline views, where the component markup is returned directly in the component's `render` method.

Anonymous components allow creating components with just a template file.

To create a simple search component that retrieves results from the server and updates itself, create the component template:

```html
// resources/views/yoyo/search.php


Submit

```

Yoyo will render the component output and compile it to add the necessary attributes that makes it dynamic and reactive.

When you submit the form, posted data is automatically made available within the component template. The template code can be expanded to show a list of results, or an empty state:

```php

```

```html


Submit




  • No results found







```

The `$results` array can be populated from any source (i.e. database, API, etc.)

The example can be converted into a live search input, with a 300ms debounce to minimize the number of requests. Replace the `form` tag with:

```html

```

The `yoyo:on="keyup delay:300ms change"` directive tells Yoyo to make a request on the keyup event, with a 300ms debounce, and only if the input text changed.

Now let's turn this into a dynamic component using a class.

```php
# /app/Yoyo/Search

query;

// Perform your database query
$entries = ['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday'];

$results = array_filter($entries, function($entry) use ($query) {
return $query && stripos($entry, $query) !== false;
});

// Render the component view
return $this->view('search',['results' => $results]);
}
}
```

And the template:

```html



  • No results found







```

A couple of things to note here that are covered in more detail in other sections.

1. The component class includes a `queryString` property that tells Yoyo to automatically include the queryString values in the browser URL after a component update. If you re-load the page with the `query` value in the URL, you'll automatically see the search results on the page.
2. Yoyo will automatically make available component class public properties as template variables. This allows using `$this->query` to access the search keyword in the component and `$query` in the template.

When you compare this search example to the counter example at the beginning, you can see that there are no action methods (i.e. increment, decrement). A component update will always default to the `render` method, unless an action is specified via one of the method attributes (i.e. yoyo:get, yoyo:post, etc.). In that case, the action method always runs before the render method.

## Rendering Components

There are two instances when components are rendered. On page load, and on component updates.

### Rendering on Page Load

To render any component on page load within your templates, use the `yoyo_render` function and pass the component name as the first parameter.

```php

```

For dynamic components, the component name is a hyphenated version of the class name (i.e. LiveSearch → live-search). If you register components while bootstrapping Yoyo using the `registerComponents` method, then you can use the registered alias as the component name.

```php
$yoyo->registerComponent('search', App\Yoyo\LiveSearch::class);
```

For anonymous components, the component name should match the template name without the file extension. So if the template name is `form.php`, the component can be rendered with:

```php

```

### Rendering on updates

Use the `yoyo_update` function to automatically process the component request and output the updated component.

```php

```

You need to add this function call for requests routed to the Yoyo `url` used in the initial configuration.

## Properties

In dynamic components, all public properties in the component class are automatically made available to the view and tracked in component updates.

```php
class HelloWorld extends Component
{
public $message = 'Hello World!';
}
```

```html





```

Public properties should only be of type: `string`, `int`, `array`, `boolean`, and should not contain any sensitive information because they can be used in component requests to keep the data in sync.

### Initializing Properties

You can initialize properties using the `mount` method of your component which runs right after the component is instantiated, and before the `render` method.

```php
class HelloWorld extends Component
{
public $message;

public function mount()
{
$this->message = 'Hello World!';
}
}
```

### Data Binding

You can automatically bind, or synchronize, the value of an HTML element with a component public property.

```php
class HelloWorld extends Component
{
public $message = 'Hello World!';
}
```

```html





```

Adding the `yoyo` attribute to any input will instantly make it reactive. Any changes to the input will be updated in the component.

By the default, the natural event of an element will be used as the event trigger.

- input, textarea and select elements are triggered on the change event.
- form elements are triggered on the submit event.
- All other elements are triggered on the click event.

You can modify this behavior using the `yoyo:on` directive which accepts multiple events separated by comma:

```html

```

### Debouncing and Throttling Requests

The are several ways to limit the requests to update components.

**`delay`** - debounces the request so it's made only after the specified period passes after the last trigger.

```html

```

**`throttle`** limits request to one dwithin the specified interval.

```html

```

**`changed`** - only makes the request when the input value has changed.

```html

```

## Actions

An action is a request made to a Yoyo component method to update (re-render) it as a result of a user interaction or page event (click, mouseover, scroll, load, etc.).

The `render` method is the default action when one is not provided explicitly. You can also override it in the component class to change the template name or when you need to send additional variables to the template in addition to the public properties.

```php
public function render()
{
return $this->view($this->componentName, ['foo' => 'bar']);
}
```

To specify an action you use one of the available action directives with the name of the action as the value.

- `yoyo:get`
- `yoyo:post`
- `yoyo:put`
- `yoyo:patch`
- `yoyo:delete`

For example:

```php
class Review extends Component
{
public Review $review;

public function helpful()
{
$this->review->userFoundHelpful($userId);
}
}
```

```html


Found Helpful

```

All components automatically listen for the `refresh` event and trigger the `render` action to refresh the component state.

### Passing Data to Actions

You can include additional data to send to the server on component update requests using the `yoyo:vals` directive which accepts a JSON encoded list of name-value pairs.

```html
Found Helpful

Found Helpful
```

You can also use `yoyo:val.name` for individual values. kebab-case variable names are automatically converted to camel-case.

```html
Found Helpful
```

Yoyo will automatically track and send component public properties and input values with every request.

```php
class Review extends Component {

public $reviewId;

public function helpful()
{
// access reviewId via $this->reviewId
}
}
```

You can also pass extra parameters to an action as arguments using an expression, without having to define them as public properties in the component:

```html

Add Todo

```

Extra parameters passed to an action are made available to the component method as regular arguments:

```php
public function addToCart($productId, $style)
{
// ...
}
```

### Actions Without a Response

Sometimes you may want to use a component action only to make changes to a database and trigger events, without rendering a response. You can use the component `skipRender` method for this:

```php
public function savePost()
{
// Store the post to the database

// Send event to the browser to close modal, or trigger a notification
$this->emitSelf('PostSaved');

// Skip template rendering
$this->skipRender();
}
```

## View Data

Sometimes you want to send data to a view without declaring the variable as a public property. You can do this by defining a render method in your component and passing a data array as the second argument:

```php
public function render()
{
return $this->view($this->componentName, ['foo' => 'bar']);
}
```

Then access the $foo variable in your template.

You can also send data to the component view using the `set` method in any component action. For example:

```php
public function increment()
{
$this->set('foo', 'bar');
// or
$this->set(['foo' => 'bar']);
}
```

## Computed Properties

```php
class HelloWorld extends Component
{
public $message = 'Hello World!';

// Computed Property
public function getHelloWorldProperty()
{
return $message;
}

// Computed Property with argument
public function getErrorsProperty($name)
{
return [
'title' => 'Please enter a title',
'description' => 'Please enter a description',
][$name] ?? null;
}
}
```

Now, you can access `$this->hello_world` from either the component's class or template:

```php


hello_world ;?>




```

Computed properties with arguments behave like normal class methods that you can call in your templates:

```php


errors('title') ;?>




```

The output of computed properties is cached within the same component request, allowing you to perform complex tasks like querying the database and not duplicating the tasks if the property is accessed multiple times. If you need to clear the cache for a computed property:

```php
// Clear all computed properties, including those with arguments
$this->forgetComputed();

// Clear a single property
$this->forgetComputed($property);

// Clear multiple properties
$this->forgetComputed([$property1, $property2]);

// Clear a single computed property with arguments
$this->forgetComputedWithArgs($property, $arg1, $arg2);
```

## Component Props

Yoyo can persist and update variables in requests without the need to explicitly include an input element.

For an anonymous component, it's possible to specify the props directly in the component root node using a comma-separated list of variable names and this allows implementing a counter without the need for a component class:

```php


+



```

By adding the `yoyo:props="count"`, Yoyo knows to automatically include the value of `count` in every request.

For dynamic components, there's no need to use the `yoyo:props` attribute because we use the protected method $props in the component class with an array of variable names.

```php
class Counter extends Component
{
public $count = 0;

protected $props = ['count'];

public function increment()
{
$this->count++;
}
}
```

Since the `$count` variable is also defined as a public property, it's already available in the template and the value is incremented throgh the `increment` method in the component class without having to use `yoyo:val.count`.

```html


+


```

## Query String

Components have the ability to automatically update the browser's query string on state changes.

```php
class Search extends Component
{
public $query;

protected $queryString = ['query'];
}
```

Yoyo is smart enough to automatically remove the query string when the current state value matches the property's default value.

For example, in a pagination component, you don't need the `?page=1` query string to appear in the URL.

```php
class Posts extends Component
{
public $page = 1;

protected $queryString = ['page'];
}
```

## Loading States

Updating Yoyo components requires an Ajax request to the server and depending on what the component does, the response time will vary. The `yoyo:spinning` directive allows you to do all sorts of cool things when a component is updating to provide a visual indicator to end-users.

### Toggling Elements During Loading States

To show an element at the start of a Yoyo update request and hide it again when the update is complete:

```html


Submit


Processing your submission...


```

Yoyo adds some CSS to the page to automatically hide the element with the `yoyo:spinning` directive.

To hide a visible element while the component is updating you can add the `remove` modifier:

```html


Submit


Text hidden while updating ...


```

## Delaying Loading States

Some actions may update quickly and showing a loading state in these cases may be more of a distraction. The `delay` modifier ensures that the loading state changes are applied only after 200ms if the component hasn't finished updating.

```html


Submit


Processing your submission...


```

### Targeting Specific Actions

If you need to toggle different indicators for different component actions, you can add the `yoyo:spin-on` directive and pass a comma separated list of action names. For example:

```html


Edit

Like


Show for edit action


Show for like action


Show for edit and like actions


```

## Toggling Element CSS Classes

Instead of toggling the visibility of an element you can also add specific CSS classes while the component updates. Use the `class` modifier and include the space-separated class names as the attribute value:

```html



Submit


```

You can also remove specific class names by adding the `remove` modifier:

```html



Submit


```

## Toggling Element Attributes

Similar to CSS class toggling, you can also add or remove attributes while the component is updating.

```html



Submit


```

## Events

Events are a great way to establish communication between Yoyo components on the same page, where one or more components can listen to events fired by another component.

Events can be fired from component methods and templates using a variety of emit methods.

All emit methods accept any number of arguments that allow sending data (string, number, array) to listeners.

### Emitting an Event to All Yoyo Components

From a component method.

```php
public function increment()
{
$this->count++;

$this->emit('counter-updated', $count);
}
```

From a template

```php
emit('counter-updated', $count) ; ?>
```

### Emitting an Event to Parent Components

When dealing with nested components you can emit events to parents and not children or sibling components.

```php
$this->emitUp('postWascreated', $arg1, $arg2);
```

### Emitting an Event to a Specific Component

When you need to emit an event to a specific component using the component name (e.g. `cart`).

```php
$this->emitTo('cart', 'productAddedToCart', $arg1, $arg2);
```

### Emitting an Event to an Element Using a Selector

The `emitTo` method also works with selectors. When a component is not found, the selector is used instead. Emitting events using selectors doesn't support passing arguments.

```php
$this->emitTo('.cart', 'productAddedToCart');
$this->emitTo('#cart', 'productAddedToCart');
$this->emitTo('.post-100', 'saved');
```

#### Emitting an Event to Itself

When you need to emit an event on the same component.

```php
$this->emitSelf('productAddedToCart', $arg1, $arg2);
```

### Listening for Events

To register listeners in Yoyo, use the `$listeners` protected property of the component.

Listeners are a key->value pair where the key is the event to listen for, and the value is the method to call on the component. If the event and method are the same, you can leave out the key.

```php
class Counter extends Component {

public $message;

protected $listeners = ['counter-updated' => 'showNewCount'];

protected function showNewCount($count)
{
$this->message = "The new count is: $count";
}
}
```

### Listening For Events In JavaScript

Yoyo allows registering event listeners for component emitted events:

```js

Yoyo.on('productAddedToCart', id => {
alert('A product was added to the cart with ID:' + id
});

```

With this feature you can control toasters, alerts, modals, etc. directly from a component action on the server by emitting the event and listening for it on the browser.

### Dispatching Browser Events

In addition to allowing components to communicate with each other, you can also send browser window events directly from a component method or template:

```php
// passing single value
$this->dispatchBrowserEvent('counter-updated', $count);

// Passing an array
$this->dispatchBrowserEvent('counter-updated', ['count' => $count]);
```

And listen for the event anywhere on the page:

```js

window.addEventListener('counter-updated', event => {
// Reading a single value
alert('Counter is now: ' + event.detail);

// Reading from an array
alert('Counter is now: ' + event.detail.count);
})

```
## Redirecting

Sometimes you may want to redirect the user to a different page after performing an action within a Yoyo component.

```php
class Registration extends Component
{
public function register()
{
// Create the user

$this->redirect('/welcome');
}
}
```

## Using Blade

You can use Yoyo with Laravel's [Blade](https://laravel.com/docs/8.x/blade) templating engine, without having to use Laravel.

### Installation

To get started install the following packages in your project:

```bash
composer require clickfwd/yoyo
composer require jenssegers/blade
```

### Configuration

Create a Blade instance and set it as the view provider for Yoyo. We also add the `YoyoServiceProvider` for Blade.

This code should run when rendering and updating components.

```php
configure([
'url' => 'yoyo',
'scriptsPath' => APP_PATH.'/app/resources/assets/js/',
'namespace' => 'App\\Yoyo\\',
]);

// Create a Blade instance

$app = Application::getInstance();

$app->bind(ApplicationContract::class, Application::class);

// Needed for Blade anonymous components

$app->alias('view', ViewFactory::class);

$app->extend('config', function (array $config) {
return new Fluent($config);
});

$blade = new Blade(
[
APP_PATH.'/resources/views',
APP_PATH.'/resources/views/yoyo',
APP_PATH.'/resources/views/components',
],
APP_PATH.'/../cache',
$app
);

$app->bind('view', function () use ($blade) {
return $blade;
});

(new YoyoServiceProvider($app))->boot();

// Optionally register Blade components

$blade->compiler()->components([
// 'button' => 'button',
]);

// Register Blade view provider for Yoyo

$yoyo->registerViewProvider(function() use ($blade) {
return new BladeViewProvider($blade);
});
```

### Load Assets

Find `yoyo.js` in the following vendor path and copy it to your project's public assets directory.

```file
/vendor/clickfwd/yoyo/src/assets/js/yoyo.js
```

To load the necessary scripts in your Blade template you can use the `yoyo_scripts` directive in the `` tag:

```blade
@yoyo_scripts
```

### Rendering a Blade View

You can use the Blade instance to render any Blade view.

```
$blade = \Clickfwd\Yoyo\Yoyo::getViewProvider()->getProviderInstance();

echo $blade->render('home');
```

### Rendering Yoyo Blade Components

To render Yoyo components inside Blade views, use the `@yoyo` directive.

```blade
@yoyo('search')
```

### Updating Yoyo Blade Components

To update Yoyo components in the Yoyo-designated route.

```php
echo (new \Clickfwd\Yoyo\Blade\Yoyo())->update();
```

### Inline Views

When dealing with simple templates, you can create components without a template file and instead return an inline view in the component's `render` method.

```php
class HelloWorld extends Component
{
public $message = 'Hello World!';
}

public function render()
{
return <<<'yoyo'



{{ $message }}



yoyo;
}
```

### Other Blade Features

Yoyo implements several Blade directives that can be used within Yoyo component templates.

- `@spinning` and `@endspinning` - Check if a component is being re-rendered.

```blade
@spinnning
Component updated
@endspinning

@spinning($liked == 1)
Component updated and liked == 1
@endspinning
```

- All event methods are available as directives within blade components

```blade
@emit('eventName', ['foo' => 'bar']);
@emitUp('eventName', ['foo' => 'bar']);
@emitSelf('eventName', ['foo' => 'bar']);
@emitTo('component-name', 'eventName', ['foo' => 'bar']);
```

- Computed properties

```php
class HelloWorld extends Component
{
public $message = 'Hello World!';

public function getHelloWorldProperty()
{
return $message;
}
}
```

```blade


{{ $this->hello_world }}




```

## Using Twig

You can use Yoyo with Symfony's [Twig](https://twig.symfony.com/) templating engine.

### Installation

To get started install the following packages in your project:

```bash
composer require clickfwd/yoyo
composer require twig/twig
```

### Configuration

Create a Twig instance and set it as the view provider for Yoyo. We also add the `YoyoTwigExtension` to Twig.

This code should run when rendering and updating components.

```php
use Clickfwd\Yoyo\Twig\YoyoTwigExtension;
use Clickfwd\Yoyo\ViewProviders\TwigViewProvider;
use Clickfwd\Yoyo\Yoyo;
use Twig\Extension\DebugExtension;

define('APP_PATH', __DIR__);

$yoyo = new Yoyo();

$yoyo->configure([
'url' => 'yoyo',
'scriptsPath' => APP_PATH.'/app/resources/assets/js/',
'namespace' => 'App\\Yoyo\\',
]);

$loader = new \Twig\Loader\FilesystemLoader([
APP_PATH.'/resources/views',
APP_PATH.'/resources/views/yoyo',
]);

$twig = new \Twig\Environment($loader, [
'cache' => APP_PATH.'/../cache',
'auto_reload' => true,
// 'debug' => true
]);

// Add Yoyo's Twig Extension

$twig->addExtension(new YoyoTwigExtension());

// Register Twig view provider for Yoyo

$yoyo->registerViewProvider(function() use ($twig) {
return new TwigViewProvider($twig);
});
```

### Load Assets

Find `yoyo.js` in the following vendor path and copy it to your project's public assets directory.

```file
/vendor/clickfwd/yoyo/src/assets/js/yoyo.js
```

To load the necessary scripts in your Twig template you can use the `yoyo_scripts` function in the `` tag:

```twig
{{ yoyo_scripts() }}
```

### Rendering a Twig View

You can use the Twig instance to render any Twig view.

```
$twig = \Clickfwd\Yoyo\Yoyo::getViewProvider()->getProviderInstance();

echo $twig->render('home');
```

### Rendering Yoyo Twig Components

To render Yoyo components inside Twig views, use the `yoyo` function.

```twig
yoyo('search')
```

### Updating Yoyo Twig Components

To update Yoyo components in the Yoyo-designated route.

```php
echo (new \Clickfwd\Yoyo\Yoyo())->update();
```

### Inline Views

When dealing with simple templates, you can create components without a template file and instead return an inline view in the component's `render` method.

```php
class HelloWorld extends Component
{
public $message = 'Hello World!';
}

public function render()
{
return <<<'twig'



{{ message }}



twig;
}
```

### Other Twig Features

Yoyo adds a few functions and variables that can be used within Yoyo component templates.

- The `spinning` variable can be used to check if a component is being re-rendered.

```twig
{% if spinning %}
Component updated
{% endif %}
```

- All event methods are available as functions within blade components

```twig
{{ emit('eventName', {'foo':'bar'}) }}
{{ emitUp('eventName', {'foo':'bar'}) }}
{{ emitSelf('eventName', {'foo':'bar'}) }}
{{ emitTo('component-name', 'eventName', {'foo':'bar'}) }}
```

- Computed properties

```php
class HelloWorld extends Component
{
public $message = 'Hello World!';

public function getHelloWorldProperty()
{
return $this->message;
}
}
```

```twig


{{ this.hello_world }}




```

## License

Copyright © ClickFWD

Yoyo is open-sourced software licensed under the [MIT license](LICENSE.md).