We would introduce a new cleaner Smarty 3 syntax, which is a much closer
syntax to PHP. As a matter of fact, it is identical except for a few
changes in delimiters and things.
Examples:
PHP: <?= $foo ?>
Smarty: {$foo} // same as Smarty 2
PHP: <?= $foo['bar'] ?>
Smarty: {$foo['bar']} // no more {$foo.bar}
PHP: <?= $foo[$bar][$foo['bar']] ?>
Smarty: {$foo[$bar][$foo['bar']] ?> // identical to PHP
PHP: <?php foreach($foo as $bar) { ... } ?>
Smarty: {foreach $foo as $bar} ... {/foreach} // just a delimiter
adjustment
PHP: <?php for($x = 0; $x<$y; $x++) { ... } ?>
Smarty: {for $x=0; $x<$y; $x++} ... {/for} // get rid of {section}
PHP: <?php if($foo == $bar && $blah !== 'ziggy') { ... } ?>
Smarty: {if $foo == $bar && $blah !== 'ziggy'} ... {/if}
Basically, the Smarty 3 variable syntax is identical to PHP, except for
a few semantic changes. We still follow the { } syntax instead of <?php
?>. BUT, you don't have to learn a bunch of new language constructs.
Everything follows straight over from PHP. This also helps the Smarty
compiler, it would basically be a prefilter to change over the Smarty
tags to PHP tags, then the PHP tokenizer does the majority of the work.
This would also make template syntax your choice: If you want to use
{$foo} or <?=$foo?>, that is up to you. You can use 100% PHP syntax in
your templates if you'd like. We could also keep some of the other nicer
Smarty 2 syntax options:
{if $foo eq $bar and $blah ne 'ziggy'} ... {/if}
This is still a thought in process, but I'd like to hear some of your
thoughts about it. I haven't thought through the entire impact yet, but
I think it would be a positive one.
is a catenation char really required?
{$foo} {$bar}
{$foo ' ' $bar}
{assign var='foobar' value=$foo ' ' $bar}
or the comma (like you can use in HP):
{$foo, ' ', $bar}
{assign var='foobar' value=$foo, ' ', $bar}
--
Sebastian Mendel
Current dot syntax may struggle in some places but to me that is an
argument for enhancing dot syntax rather than losing it. For example I
think both of the following are unambiguous:
{$foo[$bar.blah].baz}
{$foo.`$bar.blah`.baz}
> If we DID keep the smarty2 dot syntax, then how would catenation
> work?
>
Possibly {$foo . $bar} (ie whitespace around the dot) but I think that's
asking for trouble.
I did think about moving from dot syntax to a more object-like syntax
{$foo->bar}
{$foo->`$bar->blah`->baz}
{$foo->($bar->blah)->baz} Maybe more intuitive?
I don't see why the template designer should care whether the veriable
is an object or an array, the same syntax could support both.
> As in the question "why smarty", you still get all the benefits from the
> rest of the template framework (caching, customizable resources,
> security, etc.). This "dot" syntax really is the only major change that
> makes the tag syntax more flexible, but at the same time less clean.
>
As a PHP dev I am happy with a more PHP-based syntax, however we also
have to get template designers to do the work who really don't know PHP
and (little knowledge = dangerous thing) really shouldn't.
> Smarty3:
> {foreach $foo as $key => $item}
>
How are other loop variables handled within this construct, eg in Smarty 2:
{foreach name=bar from=$foo key=key item=item}
{$smarty.foreach.bar.iteration}
{if not $smarty.foreach.bar.last},{/if}
{/foreach}
(Minimal example, obviously there are other ways of handling such a
simple example.)
--
Mark Rogers // More Solutions Ltd (Peterborough Office) // 0845 45 89 555
Registered in England (0456 0902) at 13 Clarke Rd, Milton Keynes, MK1 1LG
The first one looks doable. So then the question, keep the dot syntax in
favor of PHP catenation syntax, and come up with another way to catenate?
>
>> If we DID keep the smarty2 dot syntax, then how would catenation
>> work?
>>
>>
>
> Possibly {$foo . $bar} (ie whitespace around the dot) but I think that's
> asking for trouble.
>
yes, trouble.
> I did think about moving from dot syntax to a more object-like syntax
> {$foo->bar}
> {$foo->`$bar->blah`->baz}
> {$foo->($bar->blah)->baz} Maybe more intuitive?
>
This just stirs the pot, no we're using object syntax for indexing. It's
not much better than just {$foo['bar']}
> I don't see why the template designer should care whether the veriable
> is an object or an array, the same syntax could support both.
>
>
>> As in the question "why smarty", you still get all the benefits from the
>> rest of the template framework (caching, customizable resources,
>> security, etc.). This "dot" syntax really is the only major change that
>> makes the tag syntax more flexible, but at the same time less clean.
>>
>>
>
> As a PHP dev I am happy with a more PHP-based syntax, however we also
> have to get template designers to do the work who really don't know PHP
> and (little knowledge = dangerous thing) really shouldn't.
>
>> Smarty3:
>> {foreach $foo as $key => $item}
>>
>>
>
> How are other loop variables handled within this construct, eg in Smarty 2:
> {foreach name=bar from=$foo key=key item=item}
> {$smarty.foreach.bar.iteration}
> {if not $smarty.foreach.bar.last},{/if}
> {/foreach}
> (Minimal example, obviously there are other ways of handling such a
> simple example.)
>
>
This hasn't been determined yet, but it may be just:
{foreach $foo as $key => $item}
{$smarty.foreach.iteration}
{$smarty.foreach.last}
{/foreach}
No naming involved, and only works within the loop.
Or maybe, if you use the php-style syntax, it is up to you to determine your own constructs with php-style declarations:
{foreach $foo as $key => $item}
The index is {$key}.
The iteration is {$key - 1}.
{if count($foo) < $key - 1}
This is the last row.
{/if}
{/foreach}
The first one looks doable. So then the question, keep the dot syntax in favor of PHP catenation syntax, and come up with another way to catenate?
I did think about moving from dot syntax to a more object-like syntax {$foo->bar} {$foo->`$bar->blah`->baz} {$foo->($bar->blah)->baz} Maybe more intuitive?This just stirs the pot, no we're using object syntax for indexing. It's not much better than just {$foo['bar']}
This hasn't been determined yet, but it may be just: {foreach $foo as $key => $item} {$smarty.foreach.iteration} {$smarty.foreach.last} {/foreach} No naming involved, and only works within the loop.
Or maybe, if you use the php-style syntax, it is up to you to determine your own constructs with php-style declarations: {foreach $foo as $key => $item} The index is {$key}. The iteration is {$key - 1}. {if count($foo) < $key - 1} This is the last row. {/if} {/foreach}
{foreach $foo as $idx => $var}
{$_iteration}
{$_last}
{/foreach}
Now we have a nice short variable, no loop naming, and works only within
the loop. (Now that we have a lexer, the above is possible.)
One issue is clashing with assigned tpl vars. We can either conform to
the idea that $_foo var naming is not allowed by assign(), or we can
make a different syntax, such as {($)last}, or the current smarty2
convention of {%last%}. I like the idea of the rule, no $_foo vars in
the tpl except for internal vars created by smarty. {$_foo} is short and
clean, and easily detected as internal in the tpl.
Then there is of course, the issue of nested loops. I think that the
scope of {$_foo} is the current loop:
{foreach $foo as $idx => $var}
{$_iteration}{* outside loop *}
{$_last}
{foreach $var as $idx2 => $var2}
{$_iteration} {* new iteration for this loop *}
{$_last}
{/foreach}
{$_iteration} {* back to outside loop *}
{/foreach}
We would (internally) push the vars on and off a stack so they don't
collide. Normally you wouldn't need access to internal vars from a
parent loop, but we could allow something like {$_parent.$_iteration},
assuming we keep the dot=array syntax.
Agreed. Although, I don't think there is going to be a problem letting
both syntaxes work. So:
{$foo.bar.blah}
Is the recommended syntax, however, PHP die-hards can use:
{$foo['bar']['blah']}
And will give the same result.
I think we unanimously agree, we should keep the dot syntax for array
access. That means, we should find a solution for catenation. Modifiers
are a work-around solution, but having catenation in the tpl syntax
would be a big bonus.
Javascript uses "+" for catentation, but that conflicts with template math.
We could make some new syntax:
{$foo(+)$bar}
{$foo(.)$bar}
{$foo..$bar}
Ideas?
Here we are replacing {foreach} with {for} ? Are we proposing ridding
{section} and {foreach}, and going with a new {for} syntax that covers
both bases? I like this syntax btw.
> …
> Or this one:
> …
> {for $x = 0; $x < $customers.length; $x++}
> <li>$customers[$x].name, $customer[$x].age</li>
> {/for}
> …
> Example 3:
> …
> $customers = array();
> $customers[‘Peter‘] = 33;
> $customers[‘Sally‘] = 32;
> …
> Smarty:
> …
> {for $key, $value in $customers}
> <li>$key, $value</li>
> {/for}
>
Good syntax for the {for} loop. Again, can we cover the requirements of
{section} and {foreach} with a new {for} syntax?
> Peter is {$customers.Peter} years old.
> …
>
> As far as I can see, there’s only one problem:
> If the usage of arrays and objects is the same, what happens if an
> object is given to a for loop? Does looping through the properties
> make any sense? Or do we get an error?
>
You would get the same error as PHP, likely. I'm still not solidified on
the idea of mixing the syntax for array and object access. There is
purpose to distinguishing between the two, and this keeps things much
simpler in the compiler.
{$foo.bar.blah}
{$foo->bar->blah}
{for $var in $myarray}
the key for $var is {$var@key}
the iteration is {$var@iteration}
{if $var@last}
last row...
{/if}
{/for}
Basically, the var name being looped is the loop name. Thoughts on that?
Again, conflicts with template math. However, {$foo..$bar} would not
conflict with anything, and is similar to the php catenation syntax.
{for $var in $myarray}
{$var:iteration}
{/for}
It is cleaner (and clearer) to tie the info to the loop. Especially in a
nested loop, it makes accessing external vars straight forward. It is
also much easier on the tokenizer. If we just do {$_index} then there is
nothing visually tying this to the loop, and we have to internally stack
them. Instead of having to name the loop, we can just use the var name
being used as the iterator.
{$foo.bar}syntax is already used for accessing array keys. I guess the
{$foo:bar} syntax could be though of as variable properties, such as
{$foo:length} could return the length of $foo (string or array). But
there are already ways to get at info like this such as {count($foo)} or
{$foo|count} or {$foo|strlen}
{$foo . $bar} is traditional array access, just like Smarty2.
{$foo & $bar} will be for catenation, since the "&" math operator is
hardly useful in a template.
Thoughts?
so bit operations are not required in templates?
why not using || as concat operator?
it would be the same as in ANSI SQL, at least not something competely new
also see:
http://en.wikipedia.org/wiki/Comparison_of_programming_languages_(strings)
i think there are some much better variants, which do not lead into
ambiguity
--
Sebastian Mendel
I have never seen a bit operation used for anything purposeful in a
template. If you REALLY needed bit operations, you could write a plugin
to handle it, or write your own compiler resource to your liking.
{$foo .. $bar}
But it kind of looks like a syntax error too :)
We are open to all ideas, bring 'em on!
~Cameron
~C
In smarty2 you can already to cat($foo,$bar) (providing you have the
function) or $foo|cat:$bar
Is that any different from this?
{$bar}{$baz}{$foo}
The only place I can see concatenation matters is in more complex
situations but I'm still not sure what they might be.
In what situations would
$bar$baz$foo
or
$bar"baz"$foo
be ambiguous?
Silly question: Do we need a concatenation operator?
--
Mark Rogers // More Solutions Ltd (Peterborough Office) // 0845 45 89 555
Registered in England (0456 0902) at 13 Clarke Rd, Milton Keynes, MK1 1LG
yes, this is my concern too
> On Oct 23, 6:38 pm, Monte Ohrt<mo...@ohrt.com> wrote:
>> yes you can use functions to do the trick, however it would be helpful
>> to have catenation as part of the template syntax.
>>
>> In smarty2 you can already to cat($foo,$bar) (providing you have the
>> function) or $foo|cat:$bar
>>
>> On Oct 23, 2008, at 11:28 AM, maggus.st...@googlemail.com wrote:
>>
>>
>>
>>> maybe one of the following could do the trick
>>> 1. {cat($foo, $bar, $baz)}
>>> 2. {strcat($foo, $bar, $baz)}
>>> 3. {concat($foo, $bar, $baz)}
>>> On Oct 22, 7:27 pm, "U.Tews"<uwe.t...@googlemail.com> wrote:
>>>> {math} is no longer existant as expresions can be used directly in
>>>> the
>>>> template anywhere. I think& is a good as it it will be unique in our
$foo.bar$foobar
could mean:
($foo.bar)$foobar
// $foo['bar'] . $foobar
or
$foo.(bar$foobar)
// $foo['bar' . $foobar]
> Silly question: Do we need a concatenation operator?
i asked the same ...
--
Sebastian Mendel
If we don't supply one, then so be it. Question answered. If we do
supply one, what do we use? "&" is a choice that doesn't conflict with
other template syntax, and is a lesser-used PHP operator that would
hardly be useful in a template (not nearly as useful as catenation.)
There is usually a way around catenation when the need arises, so
maybe it would be fine to leave it out.
example, if you want:
{assign var="foo" value=$foo&$bar.blah}
instead do:
{assign var="foo" value="$foo {$bar.blah}"}
etc.
using _ for concatenation is something i would strong disagree
btw. "It was a crazy idea anyway"
and it would require spaces around _ do not interfer with _ in var anmes
introducing requirement for spaces around operators is something i would
disagree too (even i personally prefer to write it with spaces all the time)
i think there are many good choices on the already mentioned wiki page,
better than &
[$var " foo " $foo.bar]
"$var foo $foo.bar"
$var~" foo "~$foo.bar
$var, " foo ", $foo.bar
$var || " foo " || $bar
(smarty could stick to 'or', 'and' instead of || and &&, but this would
break with PHP syntax ...)
btw. removing or at least replacing (using explicit functions) bit
operators in Smarty would be a not so bad idea ... "designers" could mix
up & and && not seeing the difference
> (in relation to what as andrei suggested once for php:
> http://www.gravitonic.com/software/php/ (Concatenation and object
> operator change))
>
> the original proposal would use . for object dereference not for array
> dereference.
> do we plan to have more arrays or more objects inside template in the
> future?
> the . should be used for the more common case, obviously.
>
> the best solution may be to use . for array *and* for object
> dereference. but that would be impossible to determine at compile time
> and hard to do efficiently at runtime.
Smarty could use always objects, whether arrays or not
ArrayObject
one syntax for arrays and objects would make life much easier, as
someone already wrote, designer doesn't need to take care if this var is
an object or array - and i personally often switched from at start
introduced arrays later to objects ...
--
Sebastian Mendel
are you familiar with the current Smarty syntax?
dot is used for arrays in Smarty, and if i follow the whole discussion
right, most people are happy with this
$foo.bar -> $foo['bar']
--
Sebastian Mendel
Outside the var and outside of the scope of Smarty itself, thus inside
the template... this cannot work, how shall Smarty distinguish between
"I will use this dot for catenation" and "I will show a dot between two
vars" :-).
Frank
there is no need for an operator
<p>{$foo} {$bar}</p>
or what do you mean?
--
Sebastian Mendel
For a simple output, this is ok. Bet he cannot use this as a single var
like $foobar later on.
I suggest using
{capture assign="foobar"}{$foo}{$bar}{/capture}
if nothing else works.
Frank
with extra {}, there would be no need for concatanation operator at all
{$var "baz"}
{$foo $bar}
would be the same like [], which do not interfere with {} as smarty tags
and i think [] would be a good choice,
{$foo[$bar "_a"]} -> $foo[$bar . '_a']
^ dot could be optional for arrays
{[$bar "_a"]} -> $bar . '_a'
{assign var="foo" value=[$foo "bar"]}
{assign var="foo" value=[$foo "bar"]}
{assign var="foo" value=$foo["bar"]}
{if [$foo "_a"] EQ 'bar'}
--
Sebastian Mendel
--
Sebastian Mendel
The other suggestions seem to either too close to existing syntaxes, or
just more complicated than it really needs to be. I'd rather not have a
catenation operator than require {} and [] groupings or spaces to make
it work. If we can't agree on a single-character operator, then let's
skip it.