Created
June 2, 2023 13:03
-
-
Save marknotton/ed926e9a985ce26ed852ab3d516e547a to your computer and use it in GitHub Desktop.
Add a template path to any location, includes plugins/modules as well as the native templates directory
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace yellostudio\helpers; | |
use Craft; | |
use craft\base\Plugin; | |
use yii\base\Event; | |
use craft\web\View; | |
use craft\events\RegisterTemplateRootsEvent; | |
class Helpers extends Plugin { | |
public static Helpers $plugin; | |
public function init(): void | |
{ | |
parent::init(); | |
self::$plugin = $this; | |
Craft::setAlias('@helpers', $this->getBasePath()); | |
if ( getenv('CRAFT_ENVIRONMENT') == 'dev' ) { | |
Event::on(View::class, View::EVENT_REGISTER_SITE_TEMPLATE_ROOTS, function (RegisterTemplateRootsEvent $event) { | |
$event->roots['helpers'] = Craft::getAlias('@helpers/templates'); | |
}); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
{{ include('helpers/foo') }}
Will now look for a foo.twig file in
/vendor/yellostudio/helpers/src/templates/foo.twig
. If the file doesn't exist, it will then fallback to the standard/templates/helpers/foo.twig
file.This prevents the need to do something like this: