Skip to content

Instantly share code, notes, and snippets.

@carlcs
Last active February 18, 2019 13:23
Show Gist options
  • Save carlcs/499e698e97d72b31cbee93ae64c23b95 to your computer and use it in GitHub Desktop.
Save carlcs/499e698e97d72b31cbee93ae64c23b95 to your computer and use it in GitHub Desktop.
<?php
namespace modules;
use Craft;
class Module extends \yii\base\Module
{
/**
* Initializes the module.
*/
public function init()
{
// Set a @modules alias pointed to the modules/ directory
Craft::setAlias('@modules', __DIR__);
// Set the controllerNamespace based on whether this is a console or web request
if (Craft::$app->getRequest()->getIsConsoleRequest()) {
$this->controllerNamespace = 'modules\\console\\controllers';
} else {
$this->controllerNamespace = 'modules\\controllers';
}
parent::init();
// Custom initialization code goes here...
Craft::$app->getView()->registerTwigExtension(new TwigExtension());
}
}
<?php
namespace modules;
use craft\helpers\StringHelper;
use yii\helpers\Inflector;
class TwigExtension extends \Twig_Extension implements \Twig_Extension_GlobalsInterface
{
public function getFunctions()
{
return [
new \Twig_Function('foo', [$this, 'fooFunction']),
];
}
public function fooFunction($value)
{
return 'FOO';
}
public function getFilters()
{
return [
new \Twig_Filter('safeTruncate', [StringHelper::class, 'safeTruncate']),
new \Twig_Filter('camel2words', [Inflector::class, 'camel2words']),
new \Twig_Filter('json_decode', 'json_decode'),
new \Twig_Filter('bar', function() { return 'BAR'; }),
];
}
public function getGlobals()
{
return [
'foo' => 'bar',
];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment