Skip to content

Instantly share code, notes, and snippets.

@AugustMiller
Created March 13, 2017 04:31
Show Gist options
  • Save AugustMiller/c1be037c0e063e0c0538ee0262970d75 to your computer and use it in GitHub Desktop.
Save AugustMiller/c1be037c0e063e0c0538ee0262970d75 to your computer and use it in GitHub Desktop.
<? namespace Craft;
class HelpersPlugin extends BasePlugin
{
public function getName()
{
return Craft::t('Helpers');
}
public function getVersion()
{
return '1.0';
}
public function getDeveloper()
{
return 'oof. Studio';
}
public function getDeveloperUrl()
{
return 'http://oof.studio/';
}
public function hasCpSection()
{
return false;
}
public function addTwigExtension()
{
Craft::import('plugins.helpers.twigextensions.HelpersTwigExtension');
return new HelpersTwigExtension();
}
}
<?php namespace Craft;
class HelpersTwigExtension extends \Twig_Extension
{
public function getName()
{
return 'Helpers Twig Extension';
}
public function getFilters()
{
$filters = [];
$methods = [
'mdline' => 'markdownSingleLine'
];
foreach ($methods as $key => $method) {
$filters[$key] = new \Twig_Filter_Method($this, $method);
}
return $filters;
}
/*
* Twig proxy for Craft's built-in single-line markdown parsing function
*/
public function markdownSingleLine($md)
{
return StringHelper::parseMarkdownLine($md);
}
}
Standard markdown still works:
{{ entry.markdownField | md }} {# or `markdown` #}
This adds the following filter, which strips out block-level tags:
{{ entry.markdownField | mdline }}
@AugustMiller
Copy link
Author

Plugin structure should look like this:

helpers/
  - HelpersPlugin.php
  - twigextensions/
    - HelpersTwigExtension.php

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment