Skip to content

Commit

Permalink
Merge pull request #3 from imarc/refactor-and-add-variables
Browse files Browse the repository at this point in the history
Refactor and add variables
  • Loading branch information
BillBushee authored Jan 3, 2019
2 parents 6438ad9 + eda560d commit 128cb0b
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 3 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ If only one parameter is passed, it returns the value of the cookie if it
exists. If more two or more parameters are passed, it has the same parameters
as PHP's own [setcookie()](http://php.net/manual/en/function.setcookie.php).

Linkify
---------------------

A simple twig filter, `linkify`, that searches a text string for URLs and email addresses and wraps them in anchor tags. Remember to use the |raw filer on the output.

`{{ linkify(tweet.text)|raw }}`

Inflection Extension
--------------------

Expand Down Expand Up @@ -109,3 +116,15 @@ Wrap Embeds Extension
This is a simple twig filter, `wrapembeds`, that searches for `<iframe>` tags
in the text passed in, and if it finds any, wraps them in a div with a class of
"responsive_video".


Template Variables
---------

setFlash provides a template alias to call craft()->userSession->setFlash($name, $value). Useful for passing variables to the following page, for example passing the page number from the list to detail view

`{% setFlash('page', 3) %}`

executionTime Outputs the time required for the server to execute the page output

`{{ craft.kindling.executionTime }}`
22 changes: 19 additions & 3 deletions src/Plugin.php
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.
38 changes: 38 additions & 0 deletions src/twigextensions/LinkingExtension.php
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.
32 changes: 32 additions & 0 deletions src/variables/KindlingVariable.php
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';
}
}

0 comments on commit 128cb0b

Please sign in to comment.