Extend The Cviewaction

Hi

How can modify the content that will be rendered in CViewAction?

I assume I have to extend the CViewAction and override a method but which one and how ?

Thanks!

Any Idea ?

Note: replying to your own post makes it look as if someone gave you a valid response.

Can you state more specifically what you are trying to do?

Hi Emily

Thanks for your Note. But I have noticed the no-answered for a long time posts, become answered when the poster request again :)

I have a lot of static pages and I want to replace and modified a lot of specific words at the output by some patterns dynamically

For example: lets we have a short text like that

<p>The Yii framework is the best one!</p>

I want to become automatically

<p>The [color="#FF0000"]Y[/color][color="#2E8B57"]i[/color][color="#0000FF"]i[/color] framework is the best one!</p>

I could achieve that manually or create controller/model/actions with database but I prefer to do that by

CViewAction for specific reasons.

What is your suggestion?

Thanks :)

Dear Friend

We can make use of CController::afterRender.




protected function afterRender($view,&$output){

		if($this->action->id=="page") //only with rendering the static pages, not for all actions.

		   $output=strtr($output,array("YII"=>"<span style=\"color:red;\">Y</span><span style=\"color:blue;\">I</span><span style=\"color:green;\">I</span>"));

	

		}



Note that $output parameter is passed as a reference.

Even after rendering the view the changes are perceived.

If you have got success with overriding CViewAction,kindly share with us.

Regards.

Exactly that I want! :)

It works, thanks with giving vote :)

Dear Friend

I tried to extend the logic to individual page views.

Here I tried to workaround two events associated with CViewAction(onBeforeRender and onAfterRender).

But I miserably failed.

Then I extended the CViewAction overriding the CViewAction::run method.

components/MyViewAction.php




class MyViewAction extends CViewAction

{

	public function run()

	{

		$this->resolveView($this->getRequestedView());

		$controller=$this->getController();

		if($this->layout!==null)

		{

			$layout=$controller->layout;

			$controller->layout=$this->layout;

		}


		$this->onBeforeRender($event=new CEvent($this));

		if(!$event->handled)

		{

			if($this->renderAsText)

			{

				$text=file_get_contents($controller->getViewFile($this->view));

				$controller->renderText($text);

			}

			else

			{

				$output=$controller->render($this->view,array(),true);//Rather than rendering it I captured it.

				

				echo strtr($output,$this->controller->viewTranslate);//Modifying it and then rendering it.

			}

			$this->onAfterRender(new CEvent($this));

		}


		if($this->layout!==null)

			$controller->layout=$layout;

	}

	

		

	

}



SiteController.php




class SiteController extends Controller

{   

	public $viewTranslate=array();//Here I declared the property to hold the translational messages at page level.

	

	public function actions()

	{

		return array(

			'captcha'=>array(

				'class'=>'CCaptchaAction',

				'backColor'=>0xFFFFFF,

			),

			'page'=>array(

				'class'=>'MyViewAction',//This is the derived class.

			),

		);

	}



views/site/pages/febrileSeizures.php




<?php

$this->viewTranslate=array(    //here we are mapping out the individual words for modified rendering.

'febrile'=>'<b><u>febrile</u></b>',

'seizures'=>'<b><u>seizures</u></b>',

'seizure'=>'<b><u>seizure</u></b>',

'convulsion'=>'<b><u>convulsion</u></b>',


);

?>


<h3>FEBRILE SEIZURES</h3>

A simple febrile convulsion is usually associated with a core temperature that increases rapidly to 39°C or greater. The seizure is usually generalized, is tonic-clonic and lasts a few seconds to 10-min, and is followed by a brief postictal period of drowsiness. A febrile seizure is described as atypical or complicated when the duration is longer than 15 min, when repeated convulsions occur within the same day, or when focal seizure activity or focal findings are present during the postictal period.


Approximately 30-50% of children have recurrent seizures with later episodes of fever and a small minority have numerous recurrent seizures. Although children with simple febrile seizures are at no greater risk of later epilepsy than the general population, some factors are associated with increased risk. These include the presence of atypical features of the seizure or postictal period, a positive family history of epilepsy, an initial febrile seizure before 9 mo of age, delayed developmental milestones, or a pre-existing neurologic disorder. The incidence of epilepsy is approximately 9% when several risk factors are present, compared with an incidence of 1% in children who have febrile convulsions and no risk factors.


During the acute evaluation, a physician's most important responsibility is to determine the cause of the fever and to rule out meningitis. If any doubt exists about the possibility of meningitis, a lumbar puncture with examination of the cerebrospinal fluid (CSF) is indicated. Seizure-induced CSF abnormalities are rare in children and all patients with abnormal CSF after a seizure should be thoroughly evaluated for other causes. The possibility of viral meningoencephalitis should also be kept in mind, especially that caused by herpes simplex. Viral infections of the upper respiratory tract, roseola, and acute otitis media are most frequently the causes of febrile convulsions.


Aside from glucose determination, laboratory testing such as serum electrolytes and toxicology screening should be ordered based on individual clinical circumstances such as evidence of dehydration. An electroencephalogram (EEG) is not warranted after a simple febrile seizure but may be useful for evaluating patients with an atypical feature or with other risk factors for later epilepsy. Similarly, neuroimaging is also not useful for children with simple febrile convulsions, but may be considered for children with atypical features, including focal neurologic signs or pre-existing neurologic deficits.



3740

febrileSeizures.png

Now we can customize th things at individual page levels.

The same thing can be acheived at controller level which we have done earlier.

But one question is that why we are doing such level bothering?

The same canbe acheived at individual pages with strtr function.

Regards.

You are really searching that very deeper! :)

I prefer to make by easy way.

If we want to do that by parameters then here a way


public $patterns;


protected function afterRender($view,&$output){

if($this->action->id=="page") //only with rendering the static pages, not for all actions.

foreach ($patterns as $pattern)

$output=strtr($output,$pattern);

}

}



in controller (or view)


$this->patterns = array();

$this->patterns[] = array('Yii'=>'<strong>Yii</strong>');

$this->patterns[] = array('is'=>'<span style="color:red">is</span>');

$this->patterns[] = array('best'=>'<span style="color:blue">best</span>');