Last Updated: February 25, 2016
·
1.691K
· pete-otaqui

Parse Markdown in Laravel 4 Blade Templates

Problem: you want to use Markdown for some content in the database, and to display it in your blade template.

Solution: composer require 'michelf/php-markdown' and then add this to your app/start/global.php:


Blade::extend(function($view, $compiler) {
    $pattern = $compiler->createMatcher('markdown');
    $replace = '<?php echo \Michelf\Markdown::defaultTransform$2; ?>';
    return preg_replace($pattern, $replace, $view);
});

// usage, in a blade template:
// @markdown( $some->property )
// or
// @markdown('some string *with style*')