UnphptagViewRenderer作ってみた

yiiのviewにあふれるPHPタグがなんとかならんかと思い作ってみたwww。使いかたはコメント参照。

<?php
/**
 * UnphptagViewRenderer
 *
 * configuration:
 * <pre>
 * array(
 *     'components'=>array(
 *         ......
 *         'viewRenderer'=>array(
 *             'class'=>'path.to.class.UnphptagViewRenderer',
 *         ),
 *     ),
 * )
 * </pre>
 *
 * echo:
 *   %{ Yii::app()->name} -> <?php echo Yii::app()->name; ?>
 * encode echo:
 *   %%{Yii::app()->name} -> <?php echo CHtml::encode(Yii::app()->name); ?>
 * code:
 *   #{ $name = Yii::app()->name } -> <?php $name = Yii::app()->name; ?>
 *
 */
class UnphptagViewRenderer extends CViewRenderer
{
	protected function generateViewFile($sourceFile, $viewFile)
	{
		$source = file_get_contents($sourceFile);
		file_put_contents($viewFile, $this->parse($source));
	}

	protected function parse($s) {
		$s = preg_replace('/%%\{([\s\S]*?)\}/', '<?php echo CHtml::encode($1); ?>', $s);
		$s = preg_replace('/%\{([\s\S]*?)\}/', '<?php echo $1; ?>', $s);
		$s = preg_replace('/#\{([\s\S]*?)\}/', '<?php $1; ?>', $s);
		return $s;
	}
}

こうなる

webapp/protected/views/layouts/main.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<meta name="language" content="en" />

	<!-- blueprint CSS framework -->
	<link rel="stylesheet" type="text/css" href="%{Yii::app()->request->baseUrl}/css/screen.css" media="screen, projection" />
	<link rel="stylesheet" type="text/css" href="%{Yii::app()->request->baseUrl}/css/print.css" media="print" />
	<!--[if lt IE 8]>
	<link rel="stylesheet" type="text/css" href="%{Yii::app()->request->baseUrl}/css/ie.css" media="screen, projection" />
	<![endif]-->

	<link rel="stylesheet" type="text/css" href="%{Yii::app()->request->baseUrl}/css/main.css" />
	<link rel="stylesheet" type="text/css" href="%{Yii::app()->request->baseUrl}/css/form.css" />

	<title>%%{$this->pageTitle}</title>
</head>

<body>

<div class="container" id="page">

	<div id="header">
		<div id="logo">%%{Yii::app()->name}</div>
	</div><!-- header -->

	<div id="mainmenu">
		#{ $this->widget('zii.widgets.CMenu',array(
			'items'=>array(
				array('label'=>'Home', 'url'=>array('/site/index')),
				array('label'=>'About', 'url'=>array('/site/page', 'view'=>'about')),
				array('label'=>'Contact', 'url'=>array('/site/contact')),
				array('label'=>'Login', 'url'=>array('/site/login'), 'visible'=>Yii::app()->user->isGuest),
				array('label'=>'Logout ('.Yii::app()->user->name.')', 'url'=>array('/site/logout'), 'visible'=>!Yii::app()->user->isGuest)
			),
		)); }
	</div><!-- mainmenu -->
	#{ if(isset($this->breadcrumbs)): }
		#{ $this->widget('zii.widgets.CBreadcrumbs', array(
			'links'=>$this->breadcrumbs,
		)); }<!-- breadcrumbs -->
	#{ endif }

	%{$content}

	<div class="clear"></div>

	<div id="footer">
		Copyright &copy; %{date('Y')} by My Company.<br/>
		All Rights Reserved.<br/>
		%{Yii::powered()}
	</div><!-- footer -->

</div><!-- page -->

</body>
</html>

webapp/protected/views/site/login.php

#{
$this->pageTitle=Yii::app()->name . ' - Login';
$this->breadcrumbs=array(
	'Login',
);
}

<h1>Login</h1>

<p>Please fill out the following form with your login credentials:</p>

<div class="form">
#{ $form=$this->beginWidget('CActiveForm', array(
	'id'=>'login-form',
	'enableClientValidation'=>true,
	'clientOptions'=>array(
		'validateOnSubmit'=>true,
	),
)); }

	<p class="note">Fields with <span class="required">*</span> are required.</p>

	<div class="row">
		%{ $form->labelEx($model,'username') }
		%{ $form->textField($model,'username') }
		%{ $form->error($model,'username') }
	</div>

	<div class="row">
		%{ $form->labelEx($model,'password') }
		%{ $form->passwordField($model,'password') }
		%{ $form->error($model,'password') }
		<p class="hint">
			Hint: You may login with <tt>demo/demo</tt> or <tt>admin/admin</tt>.
		</p>
	</div>

	<div class="row rememberMe">
		%{ $form->checkBox($model,'rememberMe') }
		%{ $form->label($model,'rememberMe') }
		%{ $form->error($model,'rememberMe') }
	</div>

	<div class="row buttons">
		%{ CHtml::submitButton('Login') }
	</div>

#{ $this->endWidget(); }
</div><!-- form -->