Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use the Markdown class in Yii

Tags:

php

markdown

yii

I see there is a Markdown class CMarkdown, but how do I use it?

There's no examples anywhere and the methods aren't self explanatory.

Do I use it statically like CHtml, or weird like a Widget?
Or do I have to init it somewhere like a Model?

like image 951
Rudie Avatar asked Apr 20 '11 09:04

Rudie


2 Answers

You can use CMarkdown as a widget in a view like this:

$this->beginWidget('CMarkdown', array('purifyOutput'=>true));
echo $content;
$this->endWidget();

When the $content contains Markdown syntax it will be processed by the widget. The array contains values for the public properties of CMarkdown. The properties are documented in the class reference.

CMarkdown is used in the Yii blog example and you will find it in this view file.

like image 153
i.amniels Avatar answered Nov 08 '22 01:11

i.amniels


This works and is imo the easiest:

static function markdown( $str ) {
    $md = new CMarkdown;
    return $md->transform($str);
}

A static function in the parent Controller. Probably not the Yii way, but it's simple:

<?=self::markdown($post->body)?>
like image 6
Rudie Avatar answered Nov 08 '22 00:11

Rudie