PHPackages                             guimauve/craft-twig-view-composers - 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. guimauve/craft-twig-view-composers

ActiveCraft-plugin[Templating &amp; Views](/categories/templating)

guimauve/craft-twig-view-composers
==================================

Allow composable PHP classes to be called before page view rendering, hence allowing organizing code à-la Laravel View Composers.

0.0.1(2y ago)01PHPPHP &gt;=8.0.2

Since Jun 1Pushed 2y ago1 watchersCompare

[ Source](https://github.com/guimauve-creative/craft-twig-view-composers)[ Packagist](https://packagist.org/packages/guimauve/craft-twig-view-composers)[ RSS](/packages/guimauve-craft-twig-view-composers/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (1)Versions (2)Used By (0)

Craft Twig View Composers
=========================

[](#craft-twig-view-composers)

Allow composable PHP classes to be called before page view rendering, hence allowing organizing code à-la Laravel View Composers.

Configuration
-------------

[](#configuration)

Install the plugin

```
composer require guimauve/craft-twig-view-composer
php craft plugin/install _twig-view-composers

```

Then, add the following path to your composer autoloader in composer.json. You can name the second part, which is the directory that psr-4 is going to look into, as you wish.

```
"autoload": {
"psr-4": {
  "guimauve\\composers\\": "composers/"
}
},

```

In that folder, you can now create a simple PHP class, named according to your twig template, with a "compose" method and you will receive the TemplateEvent as single argument. You can now add anything you want to the template data and thus isolate functionality, such as database calls, outside of the templates.

```
