Skip to content
This repository has been archived by the owner on Jan 21, 2020. It is now read-only.

Commit

Permalink
Merge pull request #2020 from fvovan/issue-2019
Browse files Browse the repository at this point in the history
add 'needed' param to smarty.load
  • Loading branch information
fvovan committed Dec 15, 2015
2 parents b3e1c92 + 2176834 commit 090ff3a
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 4 deletions.
14 changes: 10 additions & 4 deletions library/CM/SmartyPlugins/function.load.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
return $file->read();
$tplPath = $render->getLayoutPath($params['file'], $namespace, null, true, $needed);
if (null === $tplPath) {
return '';
}
return (new CM_File($tplPath))->read();
}
}
38 changes: 38 additions & 0 deletions tests/library/CM/SmartyPlugins/function.loadTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

require_once CM_Util::getModulePath('CM') . 'library/CM/SmartyPlugins/function.load.php';

class smarty_function_loadTest extends CMTest_TestCase {

/**
* @var Smarty_Internal_Template
*/
private $_template;

public function setUp() {
$smarty = new Smarty();
$render = new CM_Frontend_Render();
$this->_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));
}
}

0 comments on commit 090ff3a

Please sign in to comment.