Adding SCOPE param to INCLUDE tag

From: Date: Thu, 27 Nov 2003 16:07:10 +0000
Subject: Adding SCOPE param to INCLUDE tag
Groups: php.smarty.dev 
Request: Send a blank email to php-smarty-dev+get-1941@lists.php.net to get a copy of this message
Hi guys, for a job of mine was convenient having a scope global when including further templates. e.g. (very shortly) - index.php: [cut] $smarty->display('index.tpl'); - index.tpl: {include file="inner.tpl" scope="global"} - inner.tpl: {assign var="foo" value="any"} For my purposes it was convenient that *foo* was reachable in *index.tpl* after inclusion with the value setted in *inner.tpl*. So I thought to add a *scope* param in the *include* tag. Here's how I did it: Edit Smarty_Compiler.class.php (version 2.6.0) function _compile_include_tag($tag_args) { $attrs = $this->_parse_attrs($tag_args); // ADD NEXT LINE (default value of the param scope = local). $scope_action = "\$this->_tpl_vars = array_merge(\$_smarty_tpl_vars,\$GLOBALS[\"_smarty_tpl_vars_temp\"]);\n"; $arg_list = array(); if (empty($attrs['file'])) {
    $this->_syntax_error("missing 'file' attribute in include tag", E_USER_ERROR, __FILE__, __LINE__);
} foreach ($attrs as $arg_name => $arg_value) {
    if ($arg_name == 'file') {
      $include_file = $arg_value;
      continue;
    } else if ($arg_name == 'assign') {
      $assign_var = $arg_value;
      continue;
    }
    // ADD: Startin block
    else if( $arg_name == 'scope' )
    {
      $scope = @$this->_dequote($arg_value);
      if( $scope != 'local' &&
          $scope != 'parent' &&
          $scope != 'global')
        $this->_syntax_error("invalid 'scope' attribute value");
      if( $scope == 'parent' )
        $scope_action = "";
      if( $scope == 'global' )
      {
        $scope_action = "".
        "\$array_diff = array_diff( array_keys(\$this->_tpl_vars), array_keys(\$_smarty_tpl_vars) );".
          "foreach( \$array_diff as \$key=>\$value ){".
            "\$GLOBALS[\"_smarty_tpl_vars_temp\"][\$value] = \$this->_tpl_vars[\$value];\n".
          "}";
      }
    }
    // Ending block
    if (is_bool($arg_value))
      $arg_value = $arg_value ? 'true' : 'false';
    // REPLACE THIS LINE WITH THE NEXT TWO
    // $arg_list[] = "'$arg_name' => $arg_value";
    if( $arg_name != 'scope' )
      $arg_list[] = "'$arg_name' => $arg_value";
} $output = '<?php '; if (isset($assign_var)) {
    $output .= "ob_start();\n";
} $output .=
    "\$_smarty_tpl_vars = \$this->_tpl_vars;\n" .
    "\$this->_smarty_include(".$include_file.", array(".implode(',', (array)$arg_list)."));\n" .
    // REPLACE THIS LINE WITH THE NEXT ONE
    // "\$this->_tpl_vars = \$_smarty_tpl_vars;\n" .
    $scope_action.
    "unset(\$_smarty_tpl_vars);\n";
if (isset($assign_var)) {
    $output .= "\$this->assign(" . $assign_var . ", ob_get_contents()); ob_end_clean();\n";
} $output .= ' ?>'; return $output; } The value accepted for the *scope* param are: global|parent|local. default value is *local*. Hope it may help. Cheers Luca

« previous php.smarty.dev (#1941) next »