From 9b5097024fdcc3fc5eb0d3ac77c1661b231ad337 Mon Sep 17 00:00:00 2001 From: Vladimir Finko Date: Mon, 14 Dec 2015 18:56:53 +0100 Subject: [PATCH 1/3] add 'needed' param to smarty.load --- library/CM/SmartyPlugins/function.load.php | 12 ++++-- .../CM/SmartyPlugins/function.load.php | 38 +++++++++++++++++++ 2 files changed, 47 insertions(+), 3 deletions(-) create mode 100644 tests/library/CM/SmartyPlugins/function.load.php diff --git a/library/CM/SmartyPlugins/function.load.php b/library/CM/SmartyPlugins/function.load.php index 0b2d86ccb..811dce4bd 100644 --- a/library/CM/SmartyPlugins/function.load.php +++ b/library/CM/SmartyPlugins/function.load.php @@ -6,14 +6,20 @@ function smarty_function_load(array $params, Smarty_Internal_Template $template) $namespace = isset($params['namespace']) ? $params['namespace'] : null; $parse = isset($params['parse']) ? (bool) $params['parse'] : true; + $needed = isset($params['needed']) ? (bool) $params['needed'] : true; if ($parse) { - $tplPath = $render->getLayoutPath($params['file'], $namespace); + $tplPath = $render->getLayoutPath($params['file'], $namespace, null, null, $needed); + if (null === $tplPath) { + return ''; + } $params = array_merge($template->getTemplateVars(), $params); return $render->fetchTemplate($tplPath, $params); } else { - $tplPath = $render->getLayoutPath($params['file'], $namespace, null, true); - $file = new CM_File($tplPath); + $file = new CM_File($render->getLayoutPath($params['file'], $namespace, null, true, $needed)); + if (null === $file) { + return ''; + } return $file->read(); } } diff --git a/tests/library/CM/SmartyPlugins/function.load.php b/tests/library/CM/SmartyPlugins/function.load.php new file mode 100644 index 000000000..8ba5e07a7 --- /dev/null +++ b/tests/library/CM/SmartyPlugins/function.load.php @@ -0,0 +1,38 @@ +_template = $smarty->createTemplate('string:'); + $this->_template->assignGlobal('render', $render); + } + + public function testRender() { + $paramsFail = array( + 'file' => 'badFileName.tpl', + ); + + $exception = $this->catchException(function () use ($paramsFail) { + smarty_function_load($paramsFail, $this->_template); + }); + + $this->assertInstanceOf('CM_Exception_Invalid', $exception); + $this->assertSame('Cannot find `badFileName.tpl` in modules `CM` and themes `default`', $exception->getMessage()); + + $paramsPass = array( + 'file' => 'badFileName.tpl', + 'needed' => false, + ); + + $this->assertSame('', smarty_function_load($paramsPass, $this->_template)); + } +} From 58eb84b54e77fed269ac43fb6e6e1ab74795981f Mon Sep 17 00:00:00 2001 From: Vladimir Finko Date: Tue, 15 Dec 2015 12:16:32 +0100 Subject: [PATCH 2/3] rename file --- .../CM/SmartyPlugins/{function.load.php => function.loadTest.php} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename tests/library/CM/SmartyPlugins/{function.load.php => function.loadTest.php} (100%) diff --git a/tests/library/CM/SmartyPlugins/function.load.php b/tests/library/CM/SmartyPlugins/function.loadTest.php similarity index 100% rename from tests/library/CM/SmartyPlugins/function.load.php rename to tests/library/CM/SmartyPlugins/function.loadTest.php From 2176834bec6a01771c5462df772ae8e7c9a3a28e Mon Sep 17 00:00:00 2001 From: Vladimir Finko Date: Tue, 15 Dec 2015 12:22:55 +0100 Subject: [PATCH 3/3] fix 'nonParse' execution path --- library/CM/SmartyPlugins/function.load.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/library/CM/SmartyPlugins/function.load.php b/library/CM/SmartyPlugins/function.load.php index 811dce4bd..0e0849d36 100644 --- a/library/CM/SmartyPlugins/function.load.php +++ b/library/CM/SmartyPlugins/function.load.php @@ -16,10 +16,10 @@ function smarty_function_load(array $params, Smarty_Internal_Template $template) $params = array_merge($template->getTemplateVars(), $params); return $render->fetchTemplate($tplPath, $params); } else { - $file = new CM_File($render->getLayoutPath($params['file'], $namespace, null, true, $needed)); - if (null === $file) { + $tplPath = $render->getLayoutPath($params['file'], $namespace, null, true, $needed); + if (null === $tplPath) { return ''; } - return $file->read(); + return (new CM_File($tplPath))->read(); } }