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

compile-time assign plugin (WAS: stupid naming question(s))
Goto page Previous  1, 2
 
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 -> Plugins
View previous topic :: View next topic  
Author Message
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Mon May 05, 2003 2:40 pm    Post subject: Reply with quote

you are so right, of course. i overlooked the second parameter to var_export, that's much easier that ob_start() etc. around it. thanks for pointing me to that.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Mon May 05, 2003 10:19 pm    Post subject: Reply with quote

Alright, so how do I get compiled modifiers Smile I realize that this is nearly useless since it is only useful for cases where modifiers are used on literals and "ahem" static data. But I can think of some uses for it Wink

Further, are block compiled functions possible? Readily possible, that is.
Back to top
View user's profile Send private message
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Mon May 05, 2003 10:47 pm    Post subject: Reply with quote

there are no compiled modifiers, sorry.

regarding block-compiled-functions, i use a simple trick and register 2 functions:
$smarty->register_compiler_function('foo', 'foo_start');
$smarty->register_compiler_function('/foo', 'foo_end');

syntactically {foo} ... {/foo} looks like a block then. but you have to do your own stacking and checks of correct nestion (if this word even exists, but i think you know what i mean Smile ) .

btw i have upgraded my assign_c:

compiler.setvar.php:
[php:1:f99d63791c]<?php

function smarty_compiler_setvar($tag_args, &$smarty) {

/* handle left side (aka var) */
if (preg_match('!(.*)var=('.$smarty->_qstr_regexp.'|\S+?)(.*?)!Us', $tag_args, $_match)) {
$_lval = $_match[2];
$_attrs = $smarty->_parse_attrs($_match[1] . $_match[3]);
} else {
$smarty->_syntax_error("missing or invalid 'var' parameter ",
E_USER_WARNING, __FILE__, __LINE__);
return;
}

/* potential lvalue: let's see if if becomes a variable */
$_lval_pot = $smarty->_parse_var_props('$'.$smarty->_dequote($_lval));

if ($_lval_pot{0}=='$') {
/* okay we take it */
$_lval = $_lval_pot;
# echo $_lval, "\n";

} else {
/* no, we take it as a variable-name */
$_lval = '$this->_tpl_vars['.$smarty->_parse_var_props($_lval).']';
# echo 'pot: ', $_lval_pot, ' actual:', $_lval, "\n";

}

[... the $_rval-handling didn't change ...]

}

?>[/php:1:f99d63791c]

i called it setvar for now. it handles:
{setvar var=foo->bar value=1}
{setvar var=foo.0->bar value=1}
etc. it also handles:
{setvar var=$foo.0->bar value=1}
but then the *value* of "$foo.0->bar" will be taken as a varname.

these varnames don't work recursivly (that is: you can not do
{setvar var=foo value="bar->prop"}{setvar var=$foo value=...}
and hope to set a property of $bar. the varname has to be a simple
varname as the one in assign)

overall i think compiler.setvar.php and function.assign.php are compatible in most cases. setvar can just do a little more but behaves identical to assign in simple cases.

i also renamed it to assign and tested it superficially in a real-live project that uses some assigns (nothing funky, but a good smoke-test) with no problems.

now i have it and i don't know what to do with it, but it was fun doing it Smile
Back to top
View user's profile Send private message Send e-mail Visit poster's website
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Mon May 05, 2003 11:31 pm    Post subject: Reply with quote

Quote:
now i have it and i don't know what to do with it, but it was fun doing


Well, I like it and it is a good example of writing a more sophisticated compiler function than the one in the manual Smile

I like the new code--easy to extend. I was thinking that I will add another parameter to enable a 'static' var option. Also going to add parse support for at least dot notation in the *value* of $foo. I figure that since the overhead will only be bourne at compile time, its not too big of a sacrifice and gives more of a punch to indirect varsmakes the static option a bit more useful, too.

Messju, I thought I got your two function trick, but I don't see how that captures the intervening block. hmmm. I suppose if there were compiled block plugins, more could be extracted from the core.

One thing that I'm trying to figure out how to do is define a series of modifiers that can then be applied to an arbitrary var or literal. Kind of like a macro. But I'm finding it hard when not at the compiler level because of quotes being embedded in quotes.

Anyways, your fun has led to a lot of fun for me too--thanks!!
Back to top
View user's profile Send private message
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Tue May 06, 2003 7:56 am    Post subject: Reply with quote

to capture the block between your compiler-{foo} and -{/foo} you have to emit "ob_start(); ... ob_get_contents(); ob_end_clean();" statements into your compiled template.

a series of modifiers is just a list of nested functions
$var|foo|bar|baz is (simplified) sth. like baz(bar(foo($var)))
Back to top
View user's profile Send private message Send e-mail Visit poster's website
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Wed May 07, 2003 6:18 am    Post subject: Thread Now in Plugins Forum Reply with quote

I moved this thread to the plugins forum where it seems more appropriate and less likely to get lost Smile
Back to top
View user's profile Send private message
douglassdavis
Smarty Junkie


Joined: 21 Jan 2008
Posts: 541

PostPosted: Fri Mar 20, 2009 2:49 pm    Post subject: problem Reply with quote

I am trying the setvar plugin.

{setvar var=already_shown[$key] value=1}

This doesn't seem to have any effect. Any suggestions?
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Fri Mar 20, 2009 5:08 pm    Post subject: Reply with quote

setvar doesn't exist by default, you have to create the plugin. See a few posts up.
Back to top
View user's profile Send private message Visit poster's website
douglassdavis
Smarty Junkie


Joined: 21 Jan 2008
Posts: 541

PostPosted: Sun Mar 22, 2009 3:28 pm    Post subject: Reply with quote

I copied the plugin and installed exactly as it is above.

However, it has no effect here:

Code:

in php:

...
$smarty->assign('already_shown', array());
...

in smarty delimeters are <{ }>:

<{assign var=key value=5}>
<{setvar var=already_shown[$key] value=1}>
<{$already_shown|@var_dump}>



This code will print out:

array(0) { }


All of these have the same output as above:
<{setvar var=already_shown.5 value=1}>
<{setvar var=already_shown.$key value=1}>

Is there some kind of special syntax I should be using? Or did I do something wrong. Does this work for any one else?
Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Sun Mar 22, 2009 3:40 pm    Post subject: Reply with quote

I have never tried this. But in the Wiki is a "set" plugin which does similar things.

<{set var=already_shown.$key value=1}> should work with it.
Back to top
View user's profile Send private message
douglassdavis
Smarty Junkie


Joined: 21 Jan 2008
Posts: 541

PostPosted: Sun Mar 22, 2009 3:59 pm    Post subject: Reply with quote

U.Tews wrote:
I have never tried this. But in the Wiki is a "set" plugin which does similar things.

<{set var=already_shown.$key value=1}> should work with it.


I tried the set plugin here:

http://smarty.incutio.com/?page=Set


This code:

<{assign var=key value=6}>
<{set var=already_shown.$key value=1}>
<{$already_shown|@var_dump}>

produces:


Parse error: syntax error, unexpected '=' in <snip>^%%E0^E00^E00DA181%%setvar.php.tpl.php on line 7
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Sun Mar 22, 2009 5:54 pm    Post subject: Reply with quote

I'm pretty sure in both cases:

Code:
{setvar var=already_shown.$key value=1}


The param "var" is looking for a plain string that is a varname. I'll bet it may not support arrays. Try assigning just a plain var and see what happens.

Code:
{setvar var=foo value=1}
Back to top
View user's profile Send private message Visit poster's website
douglassdavis
Smarty Junkie


Joined: 21 Jan 2008
Posts: 541

PostPosted: Mon Mar 23, 2009 12:56 pm    Post subject: Reply with quote

Ok, I see what I am doing wrong. Both of these work:

<{setvar var="already_shown.$key" value=1}>
<{setvar var="already_shown[$key]" value=1}>

I did not realize that it required some code from page 1 and some from page 2. Just in case some one else wants to use this, here is what the code should look like (correct me if I'm wrong).

Code:

function smarty_compiler_setvar($tag_args, &$smarty) {

    /* handle left side (aka var) */
    if (preg_match('!(.*)var=('.$smarty->_qstr_regexp.'|\S+?)(.*?)!Us', $tag_args, $_match)) {
        $_lval = $_match[2];
        $_attrs = $smarty->_parse_attrs($_match[1] . $_match[3]);
    } else {
        $smarty->_syntax_error("missing or invalid 'var' parameter ",
                               E_USER_WARNING, __FILE__, __LINE__);
        return;
    }

    /* potential lvalue: let's see if if becomes a variable */
    $_lval_pot = $smarty->_parse_var_props('$'.$smarty->_dequote($_lval));

    if ($_lval_pot{0}=='$') {
        /* okay we take it */
        $_lval = $_lval_pot;
        # echo $_lval, "\n";
       
    } else {
        /* no, we take it as a variable-name */
        $_lval = '$this->_tpl_vars['.$smarty->_parse_var_props($_lval).']';
        # echo 'pot: ', $_lval_pot, ' actual:', $_lval, "\n";

    }



    /* handle right side (aka value) */
    if (isset($_attrs['value'])) {
        /* scalar value */
        $_rval = $_attrs['value'];

    } elseif (isset($_attrs['values'])) {
        /* list of array-values */
        $_delim = (isset($_attrs['delim'])) ? $_attrs['delim'] : "','";
        $_rval = 'explode(' . $_delim . ', ' . $_attrs['values'] . ')';

        if (isset($_attrs['keys'])) {
            /* optional list of array-keys */
            $_code = "\$_values = $_rval; $_lval = array(); ";
            $_code .= " foreach(explode($_delim, ".$_attrs['keys'].") as \$_i=>\
$_key) {";
            $_code .= " ${_lval}[\$_key] = \$_values[\$_i]; }";
            return $_code;

        }

    } else {
        $smarty->_syntax_error('missing attribute "value"',
                               E_USER_WARNING, __FILE__, __LINE__);
        return;

    }

    return $_lval . '=' . $_rval . ';';
   
}


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 -> Plugins All times are GMT
Goto page Previous  1, 2
Page 2 of 2

 
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