From: Paul S. O. <ps...@us...> - 2004-01-09 17:22:56
|
Update of /cvsroot/phpbb/phpBB2/includes In directory sc8-pr-cvs1:/tmp/cvs-serv6083/includes Modified Files: template.php Log Message: Add DEFINE/UNDEFINE ... user vars bust be preceeded by $ and be uppercase, string vars must be in single quotes. Can be used in loops, something.$VAR, etc. Use with standard IF, IF $something ==, etc. Index: template.php =================================================================== RCS file: /cvsroot/phpbb/phpBB2/includes/template.php,v retrieving revision 1.55 retrieving revision 1.56 diff -C2 -r1.55 -r1.56 *** template.php 29 Nov 2003 23:42:10 -0000 1.55 --- template.php 9 Jan 2004 17:22:53 -0000 1.56 *************** *** 28,31 **** --- 28,33 ---- derived from an LGPL application may be relicenced under the GPL, this applies to this source + + DEFINE directive inspired by a request by Cyberalien */ *************** *** 270,274 **** // We're adding a new iteration to this block with the given // variable assignments. ! $str[$blocks[$blockcount]][] = $vararray; } else --- 272,276 ---- // We're adding a new iteration to this block with the given // variable assignments. ! $str[$blocks[$blockcount]][] = &$vararray; } else *************** *** 277,281 **** // Add a new iteration to this block with the variable assignments // we were given. ! $this->_tpldata[$blockname][] = $vararray; } --- 279,283 ---- // Add a new iteration to this block with the variable assignments // we were given. ! $this->_tpldata[$blockname][] = &$vararray; } *************** *** 326,331 **** $code = preg_replace($match_php_tags, '', $code); ! // Pull out all block/statement level elements and seperate ! // plain text preg_match_all('#<!-- PHP -->(.*?)<!-- ENDPHP -->#s', $code, $matches); $php_blocks = $matches[1]; --- 328,332 ---- $code = preg_replace($match_php_tags, '', $code); ! // Pull out all block/statement level elements and seperate plain text preg_match_all('#<!-- PHP -->(.*?)<!-- ENDPHP -->#s', $code, $matches); $php_blocks = $matches[1]; *************** *** 384,387 **** --- 385,396 ---- break; + case 'DEFINE': + $compile_blocks[] = '<?php ' . $this->compile_tag_define($blocks[2][$curr_tb], true) . ' ?>'; + break; + + case 'UNDEFINE': + $compile_blocks[] = '<?php ' . $this->compile_tag_define($blocks[2][$curr_tb], false) . ' ?>'; + break; + case 'INCLUDE': $temp = ''; *************** *** 439,449 **** // This one will handle varrefs WITH namespaces ! preg_match_all('#\{(([a-z0-9\-_]+?\.)+?)([a-z0-9\-_]+?)\}#is', $text_blocks, $varrefs); for ($j = 0; $j < sizeof($varrefs[1]); $j++) { ! $namespace = $varrefs[1][$j]; ! $varname = $varrefs[3][$j]; ! $new = $this->generate_block_varref($namespace, $varname); $text_blocks = str_replace($varrefs[0][$j], $new, $text_blocks); --- 448,458 ---- // This one will handle varrefs WITH namespaces ! preg_match_all('#\{(([a-z0-9\-_]+?\.)+?)(\$)?([A-Z0-9\-_]+?)\}#', $text_blocks, $varrefs); for ($j = 0; $j < sizeof($varrefs[1]); $j++) { ! $namespace = $varrefs[2][$j]; ! $varname = $varrefs[4][$j]; ! $new = $this->generate_block_varref($namespace, $varname, true, $varrefs[3][$j]); $text_blocks = str_replace($varrefs[0][$j], $new, $text_blocks); *************** *** 462,465 **** --- 471,475 ---- } $text_blocks = preg_replace('#\{([a-z0-9\-_]*?)\}#is', "<?php echo \$this->_tpldata['.'][0]['\\1']; ?>", $text_blocks); + $text_blocks = preg_replace('#\{\$([a-z0-9\-_]*?)\}#is', "<?php echo \$this->_tpldata['DEFINE']['.']['\\1']; ?>", $text_blocks); return; *************** *** 534,538 **** $token = &$tokens[$i]; ! switch (strtolower($token)) { case '!': --- 544,548 ---- $token = &$tokens[$i]; ! switch ($token) { case '!': *************** *** 622,628 **** default: ! if (preg_match('#^(([a-z0-9\-_]+?\.)+?)?([A-Z]+[A-Z0-9\-_]+?)$#s', $token, $varrefs)) { ! $token = (!empty($varrefs[1])) ? $this->generate_block_data_ref(substr($varrefs[1], 0, strlen($varrefs[1]) - 1), true) . '[\'' . $varrefs[3] . '\']' : '$this->_tpldata[\'.\'][0][\'' . $varrefs[3] . '\']'; } break; --- 632,638 ---- default: ! if (preg_match('#^(([a-z0-9\-_]+?\.)+?)?(\$)?([A-Z]+[A-Z0-9\-_]+)$#s', $token, $varrefs)) { ! $token = (!empty($varrefs[1])) ? $this->generate_block_data_ref(substr($varrefs[1], 0, -1), true, $varrefs[3]) . '[\'' . $varrefs[4] . '\']' : (($varrefs[3]) ? '$this->_tpldata[\'DEFINE\'][\'.\'][\'' . $varrefs[4] . '\']' : '$this->_tpldata[\'.\'][0][\'' . $varrefs[4] . '\']'); } break; *************** *** 633,636 **** --- 643,689 ---- } + function compile_tag_define($tag_args, $op) + { + preg_match('#^(([a-z0-9\-_]+?\.)+?)?\$([A-Z][A-Z0-9_\-]*?) = (\'?)(.*?)(\'?)$#', $tag_args, $match); + + if (empty($match[3]) || empty($match[5])) + { + return; + } + + // Are we a string? + if ($match[4] && $match[6]) + { + $match[5] = "'" . addslashes(str_replace(array('\\\'', '\\\\'), array('\'', '\\'), $match[5])) . "'"; + } + else + { + preg_match('#(true|false|\.)#i', $match[5], $type); + + switch (strtolower($type[1])) + { + case 'true': + case 'false': + $match[5] = strtoupper($match[5]); + break; + case '.'; + $match[5] = doubleval($match[5]); + break; + default: + $match[5] = intval($match[5]); + break; + } + } + + if ($op) + { + return (($match[1]) ? $this->generate_block_data_ref(substr($match[1], 0, -1), true, true) . '[\'' . $match[3] . '\']' : '$this->_tpldata[\'DEFINE\'][\'.\'][\'' . $match[3] . '\']') . ' = ' . $match[5] . ';'; + } + else + { + return 'unset(' . (($match[1]) ? $this->generate_block_data_ref(substr($match[1], 0, -1), true, true) . '[\'' . $match[3] . '\']' : '$this->_tpldata[\'DEFINE\'][\'.\'][\'' . $match[3] . '\']') . ');'; + } + } + function compile_tag_include($tag_args) { *************** *** 717,721 **** * NOTE: expects a trailing "." on the namespace. */ ! function generate_block_varref($namespace, $varname) { // Strip the trailing period. --- 770,774 ---- * NOTE: expects a trailing "." on the namespace. */ ! function generate_block_varref($namespace, $varname, $echo = true, $defop = false) { // Strip the trailing period. *************** *** 723,735 **** // Get a reference to the data block for this namespace. ! $varref = $this->generate_block_data_ref($namespace, true); // Prepend the necessary code to stick this in an echo line. // Append the variable reference. $varref .= "['$varname']"; ! $varref = "<?php echo $varref; ?>"; return $varref; - } --- 776,787 ---- // Get a reference to the data block for this namespace. ! $varref = $this->generate_block_data_ref($namespace, true, $defop); // Prepend the necessary code to stick this in an echo line. // Append the variable reference. $varref .= "['$varname']"; ! $varref = ($echo) ? "<?php echo $varref; ?>" : $varref; return $varref; } *************** *** 742,751 **** * NOTE: does not expect a trailing "." on the blockname. */ ! function generate_block_data_ref($blockname, $include_last_iterator) { // Get an array of the blocks involved. $blocks = explode('.', $blockname); $blockcount = sizeof($blocks) - 1; ! $varref = '$this->_tpldata'; // Build up the string with everything but the last child. --- 794,803 ---- * NOTE: does not expect a trailing "." on the blockname. */ ! function generate_block_data_ref($blockname, $include_last_iterator, $defop) { // Get an array of the blocks involved. $blocks = explode('.', $blockname); $blockcount = sizeof($blocks) - 1; ! $varref = '$this->_tpldata' . (($defop) ? '[\'DEFINE\']' : ''); // Build up the string with everything but the last child. |