21 if (!class_exists(
'Smarty')) {
22 throw new ConfigurationException(
23 'wcmf\lib\presentation\view\impl\SmartyView requires '.
24 'Smarty. If you are using composer, add smarty/smarty '.
25 'as dependency to your project');
38 private static $sharedView =
null;
39 private static $actionKeyProvider =
null;
40 private static $logger =
null;
48 if (self::$logger ==
null) {
51 $this->view = new \Smarty();
52 $this->view->error_reporting = E_ALL;
55 $this->view->addPluginsDir(
56 dirname(dirname(dirname(dirname(dirname(__FILE__))))).
'/application/views/plugins/'
60 $this->view->loadFilter(
'pre',
'removeprids');
61 $this->view->loadFilter(
'output',
'trimwhitespace');
65 $this->view->setTemplateDir(WCMF_BASE);
66 $cacheDir = WCMF_BASE.
'/smarty/';
67 $this->view->setCompileDir($cacheDir.
'templates_c/');
68 $this->view->setCacheDir($cacheDir.
'cache/');
77 $this->view->compile_check = $compileCheck;
85 $this->view->caching = $caching ? \Smarty::CACHING_LIFETIME_CURRENT : \Smarty::CACHING_OFF;
93 $this->view->cache_lifetime = $cacheLifeTime;
102 $this->view->setCompileDir(WCMF_BASE.$cacheDir.
'templates_c/');
103 $this->view->setCacheDir(WCMF_BASE.$cacheDir.
'cache/');
106 $fileUtil->mkdirRec($this->view->getCompileDir());
107 $fileUtil->mkdirRec($this->view->getCacheDir());
115 $this->view->addPluginsDir(WCMF_BASE.$pluginsDir);
123 foreach ($outputFilter as $filter) {
124 $this->view->loadFilter(
'output', $filter);
132 $this->view->assign($name, $value);
139 return $this->view->getTemplateVars($name);
146 return $this->view->getTemplateVars();
153 $this->view->clearAllAssign();
159 public function render($tplFile, $cacheId=
null, $cacheLifetime=
null, $display=
true) {
160 $oldCacheLifetime = $this->view->cache_lifetime;
161 if ($cacheLifetime !==
null) {
162 $this->view->cache_lifetime = intval($cacheLifetime);
165 $result = $this->view->display($tplFile, $cacheId);
168 $result = $this->view->fetch($tplFile, $cacheId);
170 if ($cacheLifetime !==
null) {
171 $this->view->cache_lifetime = $oldCacheLifetime;
179 public static function clearCache($tplFile=
null, $cacheId=
null) {
180 if (self::$sharedView ==
null) {
184 $tpl = self::$sharedView->view->createTemplate($tplFile, $cacheId);
185 return $tpl->smarty->clearCache($tplFile, $cacheId);
189 $fileUtil->emptyDir(self::$sharedView->view->getCompileDir());
190 return self::$sharedView->view->clearAllCache();
199 public static function isCached($tplFile, $cacheId=
null) {
200 if (self::$sharedView ==
null) {
203 $tpl = self::$sharedView->view->createTemplate($tplFile, $cacheId);
204 return $tpl->isCached();
211 if (!self::isCached($tplFile, $cacheId)) {
214 $tpl = self::$sharedView->view->createTemplate($tplFile, $cacheId);
215 return (
new \DateTime())->setTimeStamp($tpl->cached->timestamp);
221 public static function getTemplate($controller, $context, $action) {
223 if (self::$actionKeyProvider ==
null) {
228 if (self::$logger->isDebugEnabled()) {
229 self::$logger->debug(
'SmartyView::getTemplate: '.$controller.
"?".$context.
"?".$action.
' -> '.$actionKey);
233 $view = $config->getValue($actionKey,
'views',
false);
235 catch (\Exception $ex) {
setCacheDir($cacheDir)
Set the caching directory If not existing, the directory will be created relative to WCMF_BASE.
setCacheLifetime($cacheLifeTime)
Set the time a view should be cached.
setPluginsDir($pluginsDir)
Set a additional plugins directory.
__construct()
Constructor.
static getBestMatch(ActionKeyProvider $actionKeyProvider, $resource, $context, $action)
Get an action key that matches a given combination of resource, context, action best.
static getTemplate($controller, $context, $action)
View is used by Controller to handle the view presentation in MVC pattern.
setCompileCheck($compileCheck)
Set whether the view should check for template modifications or not.
static isCached($tplFile, $cacheId=null)
ConfigurationException signals an exception in the configuration.
static getCacheDate($tplFile, $cacheId=null)
setCaching($caching)
Set whether views should be cached.
FileUtil provides basic support for file functionality like HTTP file upload.
static getLogger($name)
Get the logger with the given name.
ConfigActionKeyProvider searches for action keys in the application configuration.
static getInstance($name, $dynamicConfiguration=[])
View defines the interface for all view implementations.
An action key is a combination of a resource, context and action that is represented as a string.
setOutputFilter($outputFilter)
Set additional output filters.
static clearCache($tplFile=null, $cacheId=null)
LogManager is used to retrieve Logger instances.
ObjectFactory implements the service locator pattern by wrapping a Factory instance and providing sta...
render($tplFile, $cacheId=null, $cacheLifetime=null, $display=true)