Last active
August 29, 2015 14:21
-
-
Save ytkhs/1d5a7206405105630996 to your computer and use it in GitHub Desktop.
CakePHP3.x with Smarty >= 3.1
This file contains hidden or 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 AppController extends Controller { | |
public $viewClass = 'App\View\SmartyView'; | |
// ...... | |
} |
This file contains hidden or 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 | |
/** | |
* CakePHP Smarty view class | |
* @package smartyview | |
* @subpackage view | |
* @since CakePHP v 3.0 | |
* @license MIT License | |
*/ | |
# app/src/View/SmartyView.php | |
namespace App\View; | |
use Cake\View\View; | |
use Cake\Core\App; | |
use Cake\Core\Configure; | |
use Cake\Event\EventManager; | |
use Cake\Network\Request; | |
use Cake\Network\Response; | |
# installed via composer @ bootstrap.php | |
# require_once(ROOT . '/vendor' . DS . 'autoload.php'); | |
use \Smarty; | |
/** | |
* CakePHP Smarty view class | |
* | |
* This class will allow using Smarty with CakePHP | |
* | |
* @package smartyview | |
* @subpackage view | |
* @since CakePHP v 3.0 | |
*/ | |
class SmartyView extends View | |
{ | |
protected $_ext = '.tpl'; | |
protected $_smarty = null; | |
public function __construct(Request $request = null, Response $response = null, EventManager $eventManager = null, array $viewOptions = []) | |
{ | |
$this->_smarty = new Smarty(); | |
$use_dir = TMP . 'smarty' . DS; | |
$compile_dir = $use_dir . 'compile' .DS; | |
$cache_dir = $use_dir . 'cache' . DS; | |
if (!file_exists($use_dir)) { | |
mkdir($use_dir); | |
} | |
if (!file_exists($compile_dir)) { | |
mkdir($compile_dir); | |
} | |
if (!file_exists($cache_dir)) { | |
mkdir($cache_dir); | |
} | |
$this->_smarty->auto_literal = false; | |
$this->_smarty->compile_dir = $compile_dir; | |
$this->_smarty->cache_dir = $cache_dir; | |
$this->_smarty->error_reporting = 'E_ALL & ~E_NOTICE'; | |
$this->_smarty->debugging = false; | |
$this->_smarty->escape_html = true; | |
$this->_smarty->caching = Smarty::CACHING_OFF; | |
$this->_smarty->force_compile = Configure::read('debug') ? true : false; | |
$this->_smarty->addConfigDir([App::path('View')[0] . 'Smarty' . DS . 'Configs']); | |
$this->_smarty->addPluginsDir([App::path('View')[0] . 'Smarty' . DS . 'Plugins']); | |
//$this->_smarty->addTemplateDir([APP . 'Template' . DS]); | |
parent::__construct($request, $response, $eventManager, $viewOptions); | |
} | |
protected function _evaluate($viewFile, $dataForView) | |
{ | |
foreach ($dataForView as $key => $val) { | |
$this->_smarty->assign($key, $val); | |
} | |
$this->_smarty->assignByRef('this', $this); | |
return $this->_smarty->fetch($viewFile); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment