PHPackages                             jmasci/component-template - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Templating &amp; Views](/categories/templating)
4. /
5. jmasci/component-template

ActiveLibrary[Templating &amp; Views](/categories/templating)

jmasci/component-template
=========================

Base classes for creating flexible HTML rendering templates consisting of overridable callable components.

014PHP

Since Jan 27Pushed 6y ago1 watchersCompare

[ Source](https://github.com/j-masci/component-template)[ Packagist](https://packagist.org/packages/jmasci/component-template)[ RSS](/packages/jmasci-component-template/feed)WikiDiscussions master Synced 1w ago

READMEChangelogDependenciesVersions (1)Used By (0)

### Components

[](#components)

A Component is an object which wraps a callable and an array of argument filters which are also callables. It has an invoke method will which filter the arguments and then invoke the callable.

The purpose of components becomes more clear in the context of a template, where they serve a similar role to class methods except that we can override them after template instantiation.

Note that the behaviour gained from adding argument filters can sometimes be gained by overriding the callable or by invoking the callable inside of another callable.

```
use JMasci\ComponentTemplate\Component;

$comp = new Component( function( $a_number ){
    echo "You passed in: " . (int) $a_number;
});

// filters are callbacks that accept the same arguments as the callable and returns
// the arguments in a list.
// this is an example of a completely redundant filter.
$comp->set_filter( function( $a_number ){
    return [ $a_number ];
});

// a filter which does not like the number 42
$comp->set_filter( function( $a_number ){
    return [ $a_number === 42 ? 11 : $a_number ];
});

// a filter which doubles
$comp->set_filter( function( $a_number ){
    return[ $a_number * 2 ];
});

// prints 22
// the first parameter of invoke is called $new_this. You can use it to bind an object
// to $this inside of the callable.
echo $comp->invoke( null, 42 );
```

### Template

[](#template)

A template is an object that simply wraps a collection of Components. When you invoke a component it passes itself as $new\_this by default. This makes it easiest for components to invoke other components within the same template. And makes components more like dynamic (overridable) methods of a template.

```
use JMasci\ComponentTemplate\Template;

// specify the name of the top level component to invoke upon $template->render().
// we can also specify a callback here if needed (as 2nd parameter).
$template = new Template( 'main', $render_callback = null );

// set the main component. Pass in a function but it will store a Component object.
// once again, $this inside the callback will refer to the template by default, but you
// could change this behaviour by extending Template and re defining the invoke method.
$template->set( 'main', function( $p1, $p2 ){
    ?>
