Created
April 5, 2012 13:49
-
-
Save tjtjtj/2311133 to your computer and use it in GitHub Desktop.
PHPTAL View Renderer for YiiFramework
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class PHPTALViewRenderer extends CApplicationComponent implements IViewRenderer | |
{ | |
/** | |
* @var string Path alias to PHPTAL.php | |
*/ | |
public $PHPTALPathAlias = 'application.vendors.PHPTAL'; | |
/** | |
* @var string Template files extension | |
*/ | |
public $fileExtension = '.html'; | |
/** | |
* @var array PHPTAL configration options | |
* @see http://phptal.org/manual/en/split/configuration.html | |
* | |
* example: | |
* array( | |
* 'setTemplateRepository' => array(), | |
* 'setPhpCodeDestination' => 'app/runtime/PHPTAL_temp/', | |
* 'setForceReparse => false, | |
* ), | |
*/ | |
public $options = array(); | |
/** | |
* @var string Context Controller name | |
*/ | |
public $contextPath = 'this'; | |
/** | |
* @var string Yii::app()->user name | |
*/ | |
public $userPath = 'webuser'; | |
/** | |
* @var string Yii::t() first argument | |
*/ | |
public $yiiTranslatorDefaultCategory = 'app'; | |
public $yiiTranslatorCategoryName = 't'; | |
private $_basePath; | |
private $_basePathLength; | |
function init() | |
{ | |
require_once Yii::getPathOfAlias($this->PHPTALPathAlias).'/PHPTAL.php'; | |
Yii::registerAutoloader(array('PHPTAL', 'autoloadRegister'), true); | |
$app = Yii::app(); | |
$defaultOptions = array( | |
'setPhpCodeDestination' => $app->getRuntimePath() . '/PHPTAL_temp/', | |
); | |
$this->options = array_merge($defaultOptions, $this->options); | |
// make temp dir | |
$tmpdir = $this->options['setPhpCodeDestination']; | |
if ($tmpdir && !is_dir($tmpdir)) { | |
mkdir($tmpdir, 0777, true); | |
} | |
/** @var $theme CTheme */ | |
$theme = $app->getTheme(); | |
if ($theme === null) { | |
$this->_basePath = $app->getBasePath(); | |
} else { | |
$this->_basePath = $theme->getBasePath(); | |
} | |
$this->_basePathLength = strlen($this->_basePath) ; | |
return parent::init(); | |
} | |
public function renderFile($context, $sourceFile, $data, $return) | |
{ | |
// create and config | |
$sourceFile = "protected/".substr($sourceFile, $this->_basePathLength); | |
$phptal = new PHPTAL($sourceFile); | |
foreach($this->options as $setter => $val) { | |
$phptal->$setter($val); | |
} | |
$phptal->setTranslator(new PHPTALViewRendererYiiTranslator()); | |
// setup | |
$phptal->set($this->contextPath, $context); | |
$phptal->set($this->userPath, Yii::app()->user); | |
foreach($data as $key => $val) { | |
$phptal->set($key, $val); | |
} | |
// rendering | |
$result = $phptal->execute(); | |
if ($return) { | |
return $result; | |
} | |
echo $result; | |
} | |
} | |
//TODO だめだここなんとかしないと | |
require_once Yii::getPathOfAlias('application.vendors.PHPTAL').'/PHPTAL/TranslationService.php'; | |
/** | |
* TODO これでYii::tをラップできるがYii:tのような手軽さはない。 | |
* Yii::tへの手段を複数用意するべきか。 | |
* | |
* example: | |
* <div i18n:translate="string:{attribute} is invalid."> | |
* <span i18n:name="yiiTCategoryKey" tal:replace="string:yii"/> | |
* <span i18n:name="attribute" tal:replace="model/name"/> | |
* {attribute} is invalid. | |
* </div> | |
*/ | |
class PHPTALViewRendererYiiTranslator implements PHPTAL_TranslationService | |
{ | |
private $_categoryKey; | |
private $_category; | |
private $_context = array(); | |
public function __construct($categoryKey='yiiTCategoryKey', $category='yii') | |
{ | |
$this->_categoryKey = $categoryKey; | |
$this->_category = $category; | |
} | |
public function setEncoding($encoding) | |
{ | |
// DOT NOTHING | |
} | |
public function setLanguage() | |
{ | |
// DOT NOTHING | |
} | |
public function setVar($key, $value_escaped) | |
{ | |
if ($key === $this->_categoryKey) | |
$this->_category = $value_escaped; | |
else | |
$this->_context[$key] = $value_escaped; | |
} | |
public function translate($key, $htmlescape = true) | |
{ | |
return Yii::t($this->_category, $key, $this->_context); | |
} | |
public function useDomain($domain) | |
{ | |
// DOT NOTHING | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
いろいろ仕掛かり中...