Twig Filter in Plugin/PHP? 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-&gt;requireAjaxRequest();

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

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

    $this-&gt;returnJson( $content );
}


Is twig accessible in this manner or am I out of my mind? :X
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-&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 );
}


In your service:

public function renderMarkdown($content)
{
    $rendered = craft()-&gt;templates-&gt;render('path/to/template', array('content' =&gt; $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!