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 1, 2  Next
 
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: Sun May 04, 2003 9:13 pm    Post subject: compile-time assign plugin (WAS: stupid naming question(s)) Reply with quote

i have written a plugin that works like assign, but can assign to subelements of variables:
(i know, it's a two-sided sword and stuff like this is disencouraged in templates. it's nothing for the distribution, it's just something to play with.)

examples:
{assign_c lvar=$foo value="123"}
{assign_c lvar=$foo1->bar.0 value=$value}
{assign_c lvar=$foo1.bar.0 value="`$value.foo`"}
etc.

my questions:

i don't know how to name "assign_c" (it's "assign_c" now, since the assignment is compiled into the template and not a plugin-call).
i don't want to name it "assign" since i still want to have access to the original assign-plugin.

i don't know how to name "lvar" (it's "lvar" now, since it is the (l)eft side of the assignment). i don't want to name it "var" since "var" has different semantics in all other plugins. in other plugins it's a variable-name, but here it is an actual variable.

any ideas for better names for these?
(btw: i can post the plugin here, if anybody is interested)

greetings
messju
Back to top
View user's profile Send private message Send e-mail Visit poster's website
AZTEK
Smarty Pro


Joined: 16 Apr 2003
Posts: 235
Location: Purdue University

PostPosted: Sun May 04, 2003 11:14 pm    Post subject: Reply with quote

I would really like to see it. Why not make it compatible with the assign function and go ahead and make lvar var.
_________________
"Imagine a school with children that can read and write, but with teachers who cannot, and you have a metaphor of the Information Age in which we live." -Peter Cochrane
Back to top
View user's profile Send private message Visit poster's website
boots
Administrator


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

PostPosted: Mon May 05, 2003 3:46 am    Post subject: Reply with quote

messju: please post it!!

I recently modified my VAR modifier to do the something similar in that I now support array dot notation. messju, how did you decide to handle arrays?

eg: assign to foo.bar and assign to foo.foobar. I end up with both foo.bar and foo.foobar being available. However, if I originally had foo = "bar", that gets overwritten by the new array. On the other-hand, foo = "bar" for an existing foo array will have a new non-associative element appended to foo.

How do you support $ syntax in the var name--or is that a dynamic name? I like the fact that you are supporting objects too--I only put in array support -- Please post your code!

I still like the modifier syntax [ eg: "my value"|VAR:"foo.bar" ] though it requires $this in modifiers and starts to look crampy without my optional space patch. BUT, it lets me inline expressions Smile.

Quote:
i know, it's a two-sided sword and stuff like this is disencouraged in templates.

I think that people who want to discourage things like this probably don't need Smarty anyway. Why bother with all of Smarty's mechanics just to get a string replacer? Besides, there are many techniques that Smarty can elaborate that haven't even been discussed here yet.

I'm working a Smarty template layer between my PHP code and my actual output templates. Using Smarty to template the application data representations is very handy. Features that implicitly format (such as blocks and modifiers as opposed to the explicit formatting of straight template text) are a very nice in-between.

If I get my designer to do this {$data|b} instead of <b>{$data}</b> then I have enabled not an instance of a document for a given format type, but a class of the document that can be re-interpreted as needed. I can output in various formats or even use it as a technique to scrape data to storage. Furthermore, it really puts the separation in place. Designers are working on layout only. The important part is not the HTML markup--it is the designer's decision that the $data should be "bold"--that's verbage.

I have another class of users creating content. I can either provide a SEPARATE toolset to them or I can create Smarty verbage for them. I'm toying with the latter Wink

I don't mind having many fetch's in my main php, one for each layer. Smarty is cool that way: each layer can be compiled and cached separately. This essentially means I get a "free" data cache in front of my db. Since several templates share the same data, it can lead to an overall savings without inordinates amount of application design. Smarty isn't a tiny library--I want to leverage its power Smile

I think this should go in the distribution. Also, I agree with Aztek -- use var instead of lvar.
Back to top
View user's profile Send private message
Wom.bat
Smarty Pro


Joined: 24 Apr 2003
Posts: 107
Location: Munich, Germany

PostPosted: Mon May 05, 2003 6:29 am    Post subject: Reply with quote

boots wrote:
If I get my designer to do this {$data|b} instead of <b>{$data}</b> then I have enabled not an instance of a document for a given format type, but a class of the document that can be re-interpreted as needed. I can output in various formats or even use it as a technique to scrape data to storage. Furthermore, it really puts the separation in place. Designers are working on layout only. The important part is not the HTML markup--it is the designer's decision that the $data should be "bold"--that's verbage.
well... I guess if you like things like these, you should _really_ try xml with xslt soon... I guess, on the long run, it's a little easier than smarty Smile

P.S: I love Smarty Rolling Eyes
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 7:18 am    Post subject: Reply with quote

nothing fancy, but the inclined reader may see the difference between var=$foo and lvar=$foo Smile

[php:1:003edc6f57]<?php

function smarty_compiler_assign_c($tag_args, &$smarty) {
$_attrs = $smarty->_parse_attrs($tag_args);

if (isset($_attrs['var'])) {
/* varname like in assign */
$_lval = '$this->_tpl_vars['.$_attrs['var'].']';

} elseif (isset($_attrs['lvar'])) {
/* variable */
if ($_attrs['lvar']{0}!='$') {
$smarty->_syntax_error('attribute "lvar" must be a variable',
E_USER_WARNING, __FILE__, __LINE__);
return;
}
$_lval = $_attrs['lvar'];

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

}

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 . ';';

}

?>
[/php:1:003edc6f57]


Last edited by messju on Tue Apr 27, 2004 8:50 am; edited 1 time in total
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 7:35 am    Post subject: Reply with quote

Quote:
I guess if you like things like these, you should _really_ try xml with xslt soon...


Hey! How can you say that? Yes, it provides a framework for getting those types of things done. However, I'm using Smarty BECAUSE of XSLT!! Eegawd that's a limited domain system. XML is okay, but XSLT--shiver. Rolling Eyes I did enough work with XSLT to know that I hate it and hope to never have to use it again. I've done a LOT more with XML and it is passible, but there are more interesting (and less syntax heavy) developments occuring in that space right now.

I love Smarty too! Using it in the standard way is A Good Idea and that's the normal mode. But not the only mode, even though I don't advocate other uses for everyone. With very little tweeking, I can do something like this:

{"Smarty says, ha ha" |link:"http://smarty.php.net" |b |VAR:"links.Smarty"}

{$links.Smarty} is now consistently defined. The line above is placed in a separate template which is fetched (if required) prior to the loading of templates that use {$links.Smarty} etc. I'm not claiming it is the most efficient solution or the one that you might choose for your high-performance, sites (though its better than you might think and can outperform a lot of so-called framework solutions). Mainly, it allows things to be done quickly and with a custom VERB vocabulary. XML and XSLT are merely descriptive ("merely" is perhaps unfair).

My example uses three (trivial) custom modifiers and two small (trivial) one-time patches to the compiler--all under my control. How do you do that in with XML/XSLT? No, don't answer! I'm sorry that I already know Wink

I think the W3C is designing for posterity: lets design things once and for all so that they can never change again. Not the worst of goals, but the truth is is that most things are throw-away and everything has a finite lifetime.

@messju: I see, a compiler function! Cool. A general question: where does the API line get crossed in terms of using internals in plugin functions? I'm looking at that $this->_tpl_vars knowing full well that that is the way I'd prefer to access _tpl_vars in my plugins. No criticism, I'm just wonder when it is okay to bend the rules.
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 7:49 am    Post subject: Reply with quote

@boots:

the line is crossed whenver an identifier begins with "_".
in my case this is $_tpl_vars and $smarty->_parse_attrs().

i know this may break at any time when there are changes to smarty's internals. but i'm quite sure i will notice when these changes are done so i can react accordingly. Wink

i asked mom and dad if i may play with these "_"...-guys, and they said it's okay when it doesn't make me get late for dinner Very Happy
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 7:51 am    Post subject: Reply with quote

heh heh. I thought you'd say that. Thanks for the code and eat well!!
Back to top
View user's profile Send private message
boots
Administrator


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

PostPosted: Mon May 05, 2003 8:33 am    Post subject: Reply with quote

oh yeah, the names Embarassed

name: assign_item, set or set_item, put ?
attr: use, to, in
eg. {put in= value=}

It's a neat trick but the $ syntax looks weird to me. It looks different than the way it is otherwise done in Smarty. Can the $ be tacked on prior to calling _parse_attrs?
Back to top
View user's profile Send private message
boots
Administrator


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

PostPosted: Mon May 05, 2003 11:24 am    Post subject: Reply with quote

I used messju's code to create a new cache_var compile time plugin that 'caches' assigned values that are active at compile time, directly into the template.

Assume:
$local.StdCfgFile.ref = "STUFF" was assigned prior to template compilation.

{cache_var var=$local.StdCfgFile.ref}<br/>

inserts lines like this into the compiled template:
[php:1:fd47602805]
<?php $this->_tpl_vars['local']['StdCfgFile']['ref'] = <<<EOD
STUFF
EOD;
?><br/>
[/php:1:fd47602805]

Now, if you are using caches, since each new cache page is generated from the compiled page, you only need assign data specific to that page--the rest of the assignments (that were made at compile time) are automatically available. This is true as long as you are not rebuilding pages Smile. You can control this further with compile id.

Its really a kind of static var for compiled templates. Note that once a value is compiled into a template, it will over-ride any assigns made before the cache_var call as you can see in the compiled code snippet above. It doesn't store references--just values.

At anyrate, it allows you, for example, to compile your application/ui data directly into your pages (similar to config files but with less call and load overhead) even though they are still created in your PHP code. In fact, it allows you to create templates that are NOTHING but cached vars which then can conceptually be used in the same way as config files.

What did messju say about his plugin? Not for distribution?? I guess that means double-so for this one Wink


Last edited by boots on Sat May 24, 2003 6:59 am; edited 3 times in total
Back to top
View user's profile Send private message
andre
Smarty Pro


Joined: 23 Apr 2003
Posts: 164
Location: Karlsruhe, Germany

PostPosted: Mon May 05, 2003 12:02 pm    Post subject: Reply with quote

Whould it be so cruel to do something like
Code:

{assign var="foo" value="123"}
{assign var="foo1->bar.0" value=$value}
{assign var="foo1.bar.0" value="`$value.foo`"}
???

This looks very logical to me. If I use foo=$bar I expect the property "foo" to have the value of "$bar" and not "$bar" as literal.
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 12:25 pm    Post subject: Reply with quote

@boots:

as i said, it was just for playing around with. nice if you like to do so, boots. if you have no problem in raising your minimal requirements from php-4.0.6 to php-4.2.0 you can use
var_export() ( http://www.php.net/manual/en/function.var-export.php ) and output_buffering to dump a php-representation of your strings, arrays, arrays-of-array etc. (no objects, though) into the template, instead of a here-doc. just a thought.

@andre:

this looks logical to me too. it may even be compatible to the current assign this way. {assign var="foo" value="bar"} will assign to the variable named "foo" then. after that {assign var="$foo" value="..."} will assign the variable named "bar". this his how assign works currently and it doesn't clash with the syntax you suggested.
i'll look into it, when i have time for 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 12:34 pm    Post subject: Reply with quote

@messju: Embarassed thanks for the tip. I've just started using var_dump recently and didn't think of it. Makes sense. Why do I need output buffering again? I'm just returning a string back to the compiler Smile

I like andre's idea as well--its makes sense.
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 12:52 pm    Post subject: Reply with quote

var_export not var_dump. output-buffering since var_export prints to the console (like print_r), but you want to return the output and not print it, as you say. i meant to use var_export inside the compiler-function, not inside the compiled tpl, of course.

greetings
messju
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 2:18 pm    Post subject: Reply with quote

messju wrote:
var_export not var_dump. output-buffering since var_export prints to the console (like print_r), but you want to return the output and not print it, as you say. i meant to use var_export inside the compiler-function, not inside the compiled tpl, of course.


gosh, I meant to type var_export. grr. Following your code (and the compiler plugin docs Smile ), I was going to simply return the result string and let the compiler deal with the buffering--isn't that the way it works?

along the lines of:

[php:1:6ddca50bc1]
$v = var_export($_rval, TRUE);
return "$k = $v;";
[/php:1:6ddca50bc1]

Anyways, it works for me--so far. Thanks for your help, you're a champ.


Last edited by boots on Sat May 24, 2003 7:00 am; edited 1 time in total
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 1, 2  Next
Page 1 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