Clint Liang's

I share my successful stories and things I learned about life, coding, startup and entrepreneurship

Yii Blog Markdown Syntax Code Block Fix

A couple of days ago, I decided to write something on my blog based on Yii. By using Markdown syntax I was able to format my tutorials. However, the code block in my tutorial could not be displayed properly. If you have the same problem as mine, here’s a solution I figured out.

  1. Make sure you have the latest Yii v1.1.7.
  2. Make sure you insert a new line before your code block.
  3. Instead of using widget with CMarkdown, use CMarkdownParser.

In protected/views/post/_view.php replace the code:

<?php
    $this->beginWidget('CMarkdown', array('purifyOutput'=>false));
    echo $data->content;
    $this->endWidget();
?>

With:

<?php
    $markdownParser = new CMarkdownParser;  
    echo $markdownParser->safeTransform($data->content);
?>

Here’s an example html code block that works for me:

<p>foo</p>

Hope that helps.