Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

{include} extract attribute

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Feature Requests
View previous topic :: View next topic  
Author Message
snaxor
Smarty n00b


Joined: 06 Jul 2011
Posts: 3

PostPosted: Tue Oct 11, 2011 4:37 am    Post subject: {include} extract attribute Reply with quote

{include} could have an optional extract attribute that would take an associative array (or object) and do the equivalent of php's extract command before passing the variables to the included template.

For example:

test.php
Code:
$test = array();
$test['title'] = 'This is a Title';
$test['text'] = 'This is the body text';

$smarty = new Smarty();
$smarty->assign('test', $test);
$smarty->display('main.tpl');


main.tpl
Code:
<h1>Extract Test</h1>
{include 'example.tpl' extract=$test}


example.tpl

Code:
<h2>{$title}</h2>
<p>{$text}</p>


This is similar to doing $smarty->assign($array) within the tpl.

The main problem i could see is a potential variable clash if someone previously was using an extract variable when using {include}.

I did a test where I modified smarty_internal_compile_include.php (revision 4213). It seemed to work ok. Starting at line 174 (I denoted what I changed with ++ADDED to --):

Code:

//++ADDED
        if (isset($_attr['extract'])) {
            $_extract = $_attr['extract'];
        }
//--
        // delete {include} standard attributes
        unset($_attr['file'], $_attr['assign'], $_attr['cache_id'], $_attr['compile_id'], $_attr['cache_lifetime'], $_attr['nocache'], $_attr['caching'], $_attr['scope'], $_attr['inline'],
//++ADDED
            $_attr['extract']);
//--

        // remaining attributes must be assigned as smarty variable
        if (!empty($_attr)) {
            if ($_parent_scope == Smarty::SCOPE_LOCAL) {
                // create variables
                foreach ($_attr as $key => $value) {
                    $_pairs[] = "'$key'=>$value";
                }
                $_vars = 'array('.join(',',$_pairs).')';
                $_has_vars = true;
            } else {
                $compiler->trigger_template_error('variable passing not allowed in parent/global scope', $compiler->lex->taglineno);
            }
        } else {
            $_vars = 'array()';
            $_has_vars = false;
        }
//++ADDED
        if(!empty($_extract)){
            $_vars = "array_merge((array) $_extract, $_vars)";
            $_has_vars = true;
        }
//--
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Feature Requests All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP