How to Show Html?

I am new to Yii and starting a new project using this great platform.

Created a CJuiDialog and a message page which shows a message, identical to site/error page.

	public function actionMessage()


            $this->render('message', array( 'message'=>$_GET['message']));



used this way:

$this->redirect(array('site/message','message'=>'Your message was correctly sent to the website administrator.'));

Everything is ok if I send text, but when I send HTML code is shown as it is, how can I show HTML code as is expected to be shown?

You’ll want to construct your HTML string in your controller before the render() call and then in your view:

<?php echo Yii::app()->format->html( $sHTML ); ?>

Where $sHTML was passed to the view in render() the same way that you’re passing $message already.

See CFormatter.

Is it possible to pass a variable with the text formatted or the format to be done at the view?

It doesn’t look a great idea to pass html via get, better to pass some code and, in base of this code, display the html:


In the view:

if (isset($_GET['messagge']) && ($_GET['messagge']=='sent')):?>

Your message was correctly sent to the website administrator.

<?php endif;?>

Just for start.

Also, take a look at flash message for this pourpose, you don’t have to create a special page for it.

Yes, when you call render( ‘modelName/viewName’, array( ‘varA’=>$varA, ‘varB’=>varB, ) ) each of the key=>value pairs in the 2nd argument are usable in the view, named as the key. So if you passed in ‘blah’=>$varA in your controller, then in your view you would use $blah.

I agree. Putting html in your url is ugly and could cause weird behavior in the worst case. It’s cleaner to build your HTML string in the controller and pass it to the view in a variable. You can use renderPartial() to keep your controller code cleaner:

$sHTML = $this->renderPartial( 'modelA/viewX', NULL, true ); // renderPartial can return a string

$this->render( 'modelA/viewY', array( 'sInputHTML'=>$sHTML, ) );

This way you keep your format and logic separate, woo! And in ‘viewY’ in my example you would use the Yii::app()->format->html( $sInputHTML );

Got it, thanks all for the help