/** * Compiles a text node in the vB_TemplateParser format (evalable). * * @param object DOM Node of type text * @param object vB_TemplateParser object * * @return string Evalable text */ public static function compile(vB_DomNode $main_node, vB_TemplateParser $parser) { return $parser->_escape_string($main_node->value); }
public static function compile(vB_DomNode $main_node, vB_TemplateParser $parser) { $true_value = ''; $else_value = ''; $elseif_conditions = array(); $write_location = 'true'; $child_nodes = $main_node->childNodes(); foreach (array_keys($child_nodes) AS $key) { // find the 3 types of conditional values: if (true), elseif, else (false) // use a switch with references so we can write to a single variable for each case $node =& $child_nodes["$key"]; switch ($write_location) { case 'true': $location_ref =& $true_value; break; case 'else': $location_ref =& $else_value; break; case 'elseif': $keys = array_keys($elseif_conditions); $key = end($keys); $location_ref =& $elseif_conditions[$key]['value']; } if ($node->type == 'text') { $location_ref .= $parser->_escape_string($node->value); } else if ($node->type == 'tag') { switch ($node->value) { case 'elseif': // found an elseif tag, make a new entry $elseif_conditions[] = array( 'condition' => $node->attributes['condition'], 'value' => '' ); $write_location = 'elseif'; break; case 'else': // move to else $write_location = 'else'; break; default: // any other tag -- this isn't related to this if $location_ref .= $parser->_default_node_handler($node); break; } } else if ($node->type == 'curly') { $location_ref .= $parser->_default_node_handler($node); } } // merge the 2 types with conditions (true, elseif) together $conditions = array_merge(array( 0 => array( 'condition' => $main_node->attributes['condition'], 'value' => $true_value )), $elseif_conditions); // now loop through these conditions. Elseif can not be handled by a ternary // operator, so emulate it via if{}else{if{}else{}. $output = "'';"; if (!function_exists('replace_template_variables')) { require_once(DIR . '/includes/functions_misc.php'); } $condition_value = replace_template_variables($condition_value, true); foreach ($conditions AS $condition) { $output .= ' if (' . replace_template_variables($condition['condition'], true) . ') { ' .$parser->outputVar . " .= '" . $condition['value'] . "';" . ' } else'; } $output .= ' { ' . $parser->outputVar . " .= '" . $else_value . "';" . ' }'; $output .= $parser->outputVar . " .= ''"; return $output; }