<p>This was the only SE question I could find on the topic of parsing Markdown in a plugin, and making a template to run the Markdown filter seems way too roundabout. I found a better solution: </p>

<p>If you're working in <strong>Craft 3</strong>, Yii/Craft already includes this Markdown parser, which you can use like:</p>

<pre><code>$parser = new \cebe\markdown\Markdown();
$parsedContent = $parser-&gt;parse($content);
</code></pre>

CC BY-SA 4.0
2019-09-06T15:50:07.080Z
2019-09-06T15:50:07.080Z
Ryan
<p>In one of your plugin services, you can render a template that handles the markdown filter and return the results as an HTML string. You can then send that HTML to wherever you want to preview.</p>

<p>In your controller: </p>

<pre><code>public function actionPreviewContentAjax()
{
    $this-&gt;requireAjaxRequest();

    $content = craft()-&gt;request-&gt;getPost('textarea_content');

    $html = craft()-&gt;pluginName_serviceName-&gt;renderMarkdown($content);

    $return = array('html' =&gt; $html);

    $this-&gt;returnJson( $return );
}
</code></pre>

<p>In your service:</p>

<pre><code>public function renderMarkdown($content)
{
    $rendered = craft()-&gt;templates-&gt;render('path/to/template', array('content' =&gt; $content));
    return $rendered;
}
</code></pre>

<p>All you would need is a small template that renders your content. If you have this template in your plugin folder, be sure to adjust the template path as <a href="http://buildwithcraft.com/docs/plugins/templates#plugin-template-paths-explained" rel="nofollow">described here</a>.</p>

<p>Template: </p>

<pre><code>{{content|markdown}}
</code></pre>

<p>If you need more help with the writing <a href="http://buildwithcraft.com/docs/plugins/services" rel="nofollow">services</a>, let me know! </p>

CC BY-SA 3.0
2015-04-08T18:48:22.277Z
Worked like a charm. This approach will become handy in lots of other ways too. Many thanks, Aaron!
zamo.tox
CC BY-SA 3.0
2015-04-08T17:22:20.287Z
2015-04-08T17:22:20.287Z
Aaron Berkowitz
<p>Is it possible to access twig filters in a plugin? I want to be able to preview how text will look in a textarea using Markdown for a discussion board, it'd be awesome to use ajax to send the content to the controller then return the filtered version for preview, something like:</p>

<pre><code>public function actionPreviewContentAjax()
{
    $this-&gt;requireAjaxRequest();

    $content = craft()-&gt;request-&gt;getPost('textarea_content');

    // somehow apply twig's "| markdown" filter to $content

    $this-&gt;returnJson( $content );
}
</code></pre>

<p>Is twig accessible in this manner or am I out of my mind? :X</p>

<plugin-development><filter><controller><php><ajax>
Twig Filter in Plugin/PHP?
# Twig Filter in Plugin/PHP?

(Asked by: username_0 on 2015-04-08)

Is it possible to access twig filters in a plugin? I want to be able to preview how text will look in a textarea using Markdown for a discussion board, it'd be awesome to use ajax to send the content to the controller then return the filtered version for preview, something like:
public function actionPreviewContentAjax()
{
    $this->requireAjaxRequest();

    $content = craft()->request->getPost('textarea_content');

    // somehow apply twig's "| markdown" filter to $content

    $this->returnJson( $content );
}

Is twig accessible in this manner or am I out of my mind? :X

**Tags:** plugin-development, filter, controller, php, ajax

---

## 2 answers

### Answer by username_2 on 2015-04-08. Score: 4 | Accepted answer

In one of your plugin services, you can render a template that handles the markdown filter and return the results as an HTML string. You can then send that HTML to wherever you want to preview.
In your controller: 
public function actionPreviewContentAjax()
{
    $this->requireAjaxRequest();

    $content = craft()->request->getPost('textarea_content');

    $html = craft()->pluginName_serviceName->renderMarkdown($content);

    $return = array('html' => $html);

    $this->returnJson( $return );
}

In your service:
public function renderMarkdown($content)
{
    $rendered = craft()->templates->render('path/to/template', array('content' => $content));
    return $rendered;
}

All you would need is a small template that renders your content. If you have this template in your plugin folder, be sure to adjust the template path as described here.
Template: 
{{content|markdown}}

If you need more help with the writing services, let me know!

> **Comments:**
> **username_0**: Worked like a charm. This approach will become handy in lots of other ways too. Many thanks, Aaron!  

---

### Answer by username_1 on 2019-09-06. Score: 1

This was the only SE question I could find on the topic of parsing Markdown in a plugin, and making a template to run the Markdown filter seems way too roundabout. I found a better solution: 
If you're working in Craft 3, Yii/Craft already includes this Markdown parser, which you can use like:
$parser = new \cebe\markdown\Markdown();
$parsedContent = $parser->parse($content);


---

