global variable
  • Is there any way to assign a var in a file and then get it working in other by including the first one?
    I have front.tpl and front-topfeatures.tpl. In front-topfeaturs.tpl I want to assign a var like:
    {{ assign var="featured_cond" value="`$featured_cond` number not `$gimme->article->number` " }}
    and then use it in front.tpl.
    You can set scope="global" in smarty but it does not work in Newscoop.
  • 8 Comments sorted by
  • It doesn't work because variable scopes are available only in Smarty
    3, in Newscoop we use Smarty 2. There is already a plan to start using
    the version 3 but unfortunately it won't be ready for 3.6.0 as it
    requires lots of testing time to make sure the template engine will
    work ok after upgrading.

    There must be some other way to do what you want, let's see if any of
    our gurus in templating can help you out with this :-)


    On Fri, Aug 26, 2011 at 6:57 PM, Tomasz Rondio
    <newscoop-support@lists.sourcefabric.org[/email][/email]> wrote:
    >
    > Is there any way to assign a var in a file and then get it working in other by including the first one?
    > I have front.tpl and front-topfeatures.tpl. In front-topfeaturs.tpl I want to assign a var like:
    > {{ assign var="featured_cond" value="`$featured_cond` number not `$gimme->article->number` " }}
    > and then use it in front.tpl.
    > You can set scope="global" in smarty but it does not work in Newscoop.
    >
  • I have a solution!

    {{ include file="_tpl/front-topfeatures.tpl" scope="global" }}
  • It's not a solution. I did a mistake. Sorry.
    So the problem is not solved.
  • It seems that it is impossible to do that in smarty 2.
    Only way is to add this function to Smarty_Compiler.class.php

    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;
    }
  • Vote Up0Vote Down Micz FlorMicz Flor
    Posts: 184Administrator
    nice one! this could also help to make templates easier to translate for multilingual publications - and using smarty variables for text on the page?
  • Hi Tomasz,

    As I said in my previous message, scope is only available in Smarty 3 :-)

    So, this seems to work, but it's a patch to Smarty 2 basically, so I
    would suggest something which is important: Take a look at how "scope"
    works in Smarty 3, and if you decide to use this patch in Newscoop,
    please make it work in the same way as "scope" in Smarty 3, because we
    will soon switch from Smarty 2 to 3, and we wouldn't like to break
    your templates in case you implement "scope" in a different way :-)

    Good job,

    All Best,


    On Mon, Aug 29, 2011 at 10:03 AM, Micz Flor
    <newscoop-support@lists.sourcefabric.org> wrote:
    >
    > nice one! this could also help to make templates easier to translate for multilingual publications - and using smarty variables for text on the page?
    >


    --
    Holman Romero
    Senior Software Engineer, Sourcefabric
    holman.romero@sourcefabric.org

    Salvátorská 10
    110 00 Praha 1, Czech Republic
    +420 608910633

    http://www.sourcefabric.org
  • This patch works exacly like scopes in smarty 3. I didn't used it to be sure that template is compatible with any Newscoop installation.
  • Great, then you're safe ;-)


    On Tue, Aug 30, 2011 at 10:26 AM, Tomasz Rondio
    <newscoop-support@lists.sourcefabric.org> wrote:
    >
    > This patch works exacly like scopes in smarty 3. I didn't used it to be sure that template is compatible with any Newscoop installation.
    >


    --
    Holman Romero
    Senior Software Engineer, Sourcefabric
    holman.romero@sourcefabric.org

    Salvátorská 10
    110 00 Praha 1, Czech Republic
    +420 608910633

    http://www.sourcefabric.org