-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from imarc/refactor-and-add-variables
Refactor and add variables
- Loading branch information
Showing
9 changed files
with
108 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,34 @@ | ||
<?php | ||
namespace Imarc\Craft\Kindling; | ||
|
||
use Imarc\Craft\Kindling\variables\KindlingVariable; | ||
|
||
use Craft; | ||
use craft\web\twig\variables\CraftVariable; | ||
use yii\base\Event; | ||
|
||
class Plugin extends \craft\base\Plugin | ||
{ | ||
public function init() | ||
{ | ||
parent::init(); | ||
|
||
Craft::$app->view->registerTwigExtension(new PathingVariablesExtension()); | ||
Craft::$app->view->registerTwigExtension(new CookieExtension()); | ||
Craft::$app->view->registerTwigExtension(new ArrayExtension()); | ||
Craft::$app->view->registerTwigExtension(new WrapEmbedsExtension()); | ||
Craft::$app->view->registerTwigExtension(new CookieExtension()); | ||
Craft::$app->view->registerTwigExtension(new InflectionExtension()); | ||
Craft::$app->view->registerTwigExtension(new LinkingExtension()); | ||
Craft::$app->view->registerTwigExtension(new PathingVariablesExtension()); | ||
Craft::$app->view->registerTwigExtension(new WrapEmbedsExtension()); | ||
|
||
Event::on( | ||
CraftVariable::class, | ||
CraftVariable::EVENT_INIT, | ||
function (Event $event) { | ||
/** @var CraftVariable $variable */ | ||
$variable = $event->sender; | ||
$variable->set('kindling', KindlingVariable::class); | ||
} | ||
); | ||
|
||
} | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
namespace Imarc\Craft\Kindling; | ||
|
||
use Twig_Extension; | ||
use Twig_SimpleFunction; | ||
|
||
class LinkingExtension extends Twig_Extension | ||
{ | ||
/** | ||
* @return string | ||
*/ | ||
public function getName() | ||
{ | ||
return 'Kindling Linking Extension'; | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function getFunctions() | ||
{ | ||
return [ | ||
new Twig_SimpleFunction('linkify', [$this, 'linkify']), | ||
]; | ||
} | ||
|
||
/** | ||
*/ | ||
public function linkify($text_content) { | ||
|
||
$string = preg_replace( | ||
"~[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]~", | ||
"<a href=\"\\0\">\\0</a>", | ||
$text_content); | ||
|
||
return $string; | ||
} | ||
} |
File renamed without changes.
File renamed without changes.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
namespace Imarc\Craft\Kindling\variables; | ||
|
||
use Imarc\Craft\Kindling\Plugin; | ||
|
||
|
||
use Craft; | ||
|
||
class KindlingVariable | ||
{ | ||
/** | ||
* Provides public access to the craft()->userSession->setFlash method from your Twig template: | ||
* | ||
* {{ craft.kindling.setFlash(name, value) }} | ||
*/ | ||
public function setFlash($name, $value) | ||
{ | ||
craft()->userSession->setFlash($name, $value); | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* Debuging helper method returns a text string stating the microtime required to execute the script | ||
* | ||
* {{ craft.kindling.executionTime }} | ||
*/ | ||
public function executionTime() | ||
{ | ||
return 'Page executed in ' . (microtime(true) - $_SERVER["REQUEST_TIME_FLOAT"]) . ' seconds'; | ||
} | ||
} |