mirror of
https://github.com/phpbb/phpbb.git
synced 2025-06-27 21:58:52 +00:00
Merge pull request #881 from Fyorl/ticket/10970
Allow INCLUDE template macros to accept paths of the form {FOO}/a/{BAR}/c
This commit is contained in:
commit
f0cfae1f01
11 changed files with 83 additions and 19 deletions
|
@ -361,6 +361,43 @@ class phpbb_template_filter extends php_user_filter
|
||||||
return $text_blocks;
|
return $text_blocks;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parse paths of the form {FOO}/a/{BAR}/b
|
||||||
|
*
|
||||||
|
* Note: this method assumes at least one variable in the path, this should
|
||||||
|
* be checked before this method is called.
|
||||||
|
*
|
||||||
|
* @param string $path The path to parse
|
||||||
|
* @param string $include_type The type of template function to call
|
||||||
|
* @return string An appropriately formatted string to include in the
|
||||||
|
* template or an empty string if an expression like S_FIRST_ROW was
|
||||||
|
* incorrectly used
|
||||||
|
*/
|
||||||
|
private function parse_dynamic_path($path, $include_type)
|
||||||
|
{
|
||||||
|
$matches = array();
|
||||||
|
$replace = array();
|
||||||
|
$is_expr = true;
|
||||||
|
|
||||||
|
preg_match_all('#\{((?:' . self::REGEX_NS . '\.)*)(\$)?(' . self::REGEX_VAR . ')\}#', $path, $matches);
|
||||||
|
foreach ($matches[0] as $var_str)
|
||||||
|
{
|
||||||
|
$tmp_is_expr = false;
|
||||||
|
$var = $this->get_varref($var_str, $tmp_is_expr);
|
||||||
|
$is_expr = $is_expr && $tmp_is_expr;
|
||||||
|
$replace[] = "' . $var . '";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!$is_expr)
|
||||||
|
{
|
||||||
|
return " \$_template->$include_type('" . str_replace($matches[0], $replace, $path) . "', true);";
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Compile variables
|
* Compile variables
|
||||||
*
|
*
|
||||||
|
@ -774,15 +811,9 @@ class phpbb_template_filter extends php_user_filter
|
||||||
private function compile_tag_include($tag_args)
|
private function compile_tag_include($tag_args)
|
||||||
{
|
{
|
||||||
// Process dynamic includes
|
// Process dynamic includes
|
||||||
if ($tag_args[0] == '{')
|
if (strpos($tag_args, '{') !== false)
|
||||||
{
|
{
|
||||||
$var = $this->get_varref($tag_args, $is_expr);
|
return $this->parse_dynamic_path($tag_args, '_tpl_include');
|
||||||
|
|
||||||
// Make sure someone didn't try to include S_FIRST_ROW or similar
|
|
||||||
if (!$is_expr)
|
|
||||||
{
|
|
||||||
return "if (isset($var)) { \$_template->_tpl_include($var); }";
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return "\$_template->_tpl_include('$tag_args');";
|
return "\$_template->_tpl_include('$tag_args');";
|
||||||
|
@ -796,6 +827,11 @@ class phpbb_template_filter extends php_user_filter
|
||||||
*/
|
*/
|
||||||
private function compile_tag_include_php($tag_args)
|
private function compile_tag_include_php($tag_args)
|
||||||
{
|
{
|
||||||
|
if (strpos($tag_args, '{') !== false)
|
||||||
|
{
|
||||||
|
return $this->parse_dynamic_path($tag_args, '_php_include');
|
||||||
|
}
|
||||||
|
|
||||||
return "\$_template->_php_include('$tag_args');";
|
return "\$_template->_php_include('$tag_args');";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -883,14 +919,9 @@ class phpbb_template_filter extends php_user_filter
|
||||||
private function compile_tag_include_js($tag_args)
|
private function compile_tag_include_js($tag_args)
|
||||||
{
|
{
|
||||||
// Process dynamic includes
|
// Process dynamic includes
|
||||||
if ($tag_args[0] == '{')
|
if (strpos($tag_args, '{') !== false)
|
||||||
{
|
{
|
||||||
$var = $this->get_varref($tag_args, $is_expr);
|
return $this->parse_dynamic_path($tag_args, '_js_include');
|
||||||
if (!$is_expr)
|
|
||||||
{
|
|
||||||
return " \$_template->_js_include($var, true);";
|
|
||||||
}
|
|
||||||
return '';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Locate file
|
// Locate file
|
||||||
|
|
|
@ -538,7 +538,11 @@ class phpbb_template
|
||||||
// Locate file
|
// Locate file
|
||||||
if ($locate)
|
if ($locate)
|
||||||
{
|
{
|
||||||
$file = $this->locator->get_first_file_location(array($file), true, true);
|
$located = $this->locator->get_first_file_location(array($file), false, true);
|
||||||
|
if ($located)
|
||||||
|
{
|
||||||
|
$file = $located;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else if ($relative)
|
else if ($relative)
|
||||||
{
|
{
|
||||||
|
|
|
@ -23,6 +23,18 @@ class phpbb_template_includephp_test extends phpbb_template_template_test_case
|
||||||
$this->assertEquals("Path is relative to board root.\ntesting included php", $this->display('test'), "Testing INCLUDEPHP");
|
$this->assertEquals("Path is relative to board root.\ntesting included php", $this->display('test'), "Testing INCLUDEPHP");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function test_includephp_variables()
|
||||||
|
{
|
||||||
|
$this->setup_engine(array('tpl_allow_php' => true));
|
||||||
|
|
||||||
|
$cache_file = $this->template->cachepath . 'includephp_variables.html.php';
|
||||||
|
|
||||||
|
$this->run_template('includephp_variables.html', array('TEMPLATES' => 'templates'), array(), array(), "Path includes variables.\ntesting included php", $cache_file);
|
||||||
|
|
||||||
|
$this->template->set_filenames(array('test' => 'includephp_variables.html'));
|
||||||
|
$this->assertEquals("Path includes variables.\ntesting included php", $this->display('test'), "Testing INCLUDEPHP");
|
||||||
|
}
|
||||||
|
|
||||||
public function test_includephp_absolute()
|
public function test_includephp_absolute()
|
||||||
{
|
{
|
||||||
$path_to_php = dirname(__FILE__) . '/templates/_dummy_include.php.inc';
|
$path_to_php = dirname(__FILE__) . '/templates/_dummy_include.php.inc';
|
||||||
|
|
|
@ -20,11 +20,14 @@ class phpbb_template_template_includejs_test extends phpbb_template_template_tes
|
||||||
$scripts = array(
|
$scripts = array(
|
||||||
'<script src="' . $this->test_path . '/templates/parent_and_child.js?assets_version=1"></script>',
|
'<script src="' . $this->test_path . '/templates/parent_and_child.js?assets_version=1"></script>',
|
||||||
'<script src="' . $this->test_path . '/parent_templates/parent_only.js?assets_version=1"></script>',
|
'<script src="' . $this->test_path . '/parent_templates/parent_only.js?assets_version=1"></script>',
|
||||||
'<script src="' . $this->test_path . '/templates/child_only.js?assets_version=1"></script>'
|
'<script src="' . $this->test_path . '/templates/child_only.js?assets_version=1"></script>',
|
||||||
|
'<script src="' . $this->test_path . '/templates/subdir/parent_only.js?assets_version=1"></script>',
|
||||||
|
'<script src="' . $this->test_path . '/templates/subdir/subsubdir/parent_only.js?assets_version=1"></script>',
|
||||||
|
'<script src="' . $this->test_path . '/templates/subdir/parent_only.js?assets_version=1"></script>',
|
||||||
);
|
);
|
||||||
|
|
||||||
// Run test
|
// Run test
|
||||||
$cache_file = $this->template->cachepath . 'includejs.html.php';
|
$cache_file = $this->template->cachepath . 'includejs.html.php';
|
||||||
$this->run_template('includejs.html', array('PARENT' => 'parent_only.js'), array(), array(), implode('', $scripts), $cache_file);
|
$this->run_template('includejs.html', array('PARENT' => 'parent_only.js', 'SUBDIR' => 'subdir', 'EXT' => 'js'), array(), array(), implode('', $scripts), $cache_file);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -183,6 +183,13 @@ class phpbb_template_template_test extends phpbb_template_template_test_case
|
||||||
array(),
|
array(),
|
||||||
'value',
|
'value',
|
||||||
),
|
),
|
||||||
|
array(
|
||||||
|
'include_variables.html',
|
||||||
|
array('SUBDIR' => 'subdir', 'VARIABLE' => 'value'),
|
||||||
|
array(),
|
||||||
|
array(),
|
||||||
|
'value',
|
||||||
|
),
|
||||||
array(
|
array(
|
||||||
'loop_vars.html',
|
'loop_vars.html',
|
||||||
array(),
|
array(),
|
||||||
|
|
1
tests/template/templates/include_variables.html
Normal file
1
tests/template/templates/include_variables.html
Normal file
|
@ -0,0 +1 @@
|
||||||
|
<!-- INCLUDE {SUBDIR}/variable.html -->
|
|
@ -2,4 +2,7 @@
|
||||||
<!-- INCLUDEJS {PARENT} -->
|
<!-- INCLUDEJS {PARENT} -->
|
||||||
<!-- DEFINE $TEST = 'child_only.js' -->
|
<!-- DEFINE $TEST = 'child_only.js' -->
|
||||||
<!-- INCLUDEJS {$TEST} -->
|
<!-- INCLUDEJS {$TEST} -->
|
||||||
{SCRIPTS}
|
<!-- INCLUDEJS subdir/{PARENT} -->
|
||||||
|
<!-- INCLUDEJS {SUBDIR}/subsubdir/{PARENT} -->
|
||||||
|
<!-- INCLUDEJS {SUBDIR}/parent_only.{EXT} -->
|
||||||
|
{SCRIPTS}
|
||||||
|
|
2
tests/template/templates/includephp_variables.html
Normal file
2
tests/template/templates/includephp_variables.html
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
Path includes variables.
|
||||||
|
<!-- INCLUDEPHP ../tests/template/{TEMPLATES}/_dummy_include.php.inc -->
|
0
tests/template/templates/subdir/parent_only.js
Normal file
0
tests/template/templates/subdir/parent_only.js
Normal file
0
tests/template/templates/subdir/subsubdir/parent_only.js
Normal file
0
tests/template/templates/subdir/subsubdir/parent_only.js
Normal file
1
tests/template/templates/subdir/variable.html
Normal file
1
tests/template/templates/subdir/variable.html
Normal file
|
@ -0,0 +1 @@
|
||||||
|
{VARIABLE}
|
Loading…
Add table
Reference in a new issue