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

Smarty 3.0 Alpha 1: Proof of Concept
Goto page Previous  1, 2, 3, 4, 5, 6, 7  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 -> Smarty 3
View previous topic :: View next topic  
Author Message
mohrt
Administrator


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

PostPosted: Wed Oct 22, 2008 2:59 pm    Post subject: Reply with quote

Some interesting things happening in alpha lately.

First, we are proposing to deprecate both {section} and {foreach} in favor of a new {for} function for looping. Examples:

Code:
{for $var in $myarray}
    key is {$var:key}
    index is {$var:index}
    iteration is {$var:iteration}
    total is {$var:total}
{/for}


Notice we use {$var:foo} to access array properties, and the iteration var is used as the identifier (no need to "name" a for loop.)

Another example:

Code:
{for $x=0; $x<100; $x++}
   {$myarray[$x]}
{/for}


This is the way to loop over multiple variables.

The dot "." is still going to be used for array access, since the syntax is highly favored by template designers. However, PHP-style array access is also permissible:

Code:
{$foo.bar.baz}
{$foo['bar']['baz']}


Both of the above are identical.

Since the dot "." is used for array access, catenation will be done with the "&" symbol:

Code:
{$foo & $bar}


And yes, spaces will be allowed in tag syntax too, so this is fine:

Code:
{$foo . bar . blah}
{$foo | escape}
Back to top
View user's profile Send private message Visit poster's website
conner_bw
Smarty Rookie


Joined: 21 Dec 2007
Posts: 17

PostPosted: Thu Oct 23, 2008 9:45 pm    Post subject: Reply with quote

Hi, please look at my project sux0r.org which wraps and extends Smarty and SmartyValidate classes. I'm a fan of Smarty, I use it everyday, so don't take the following 2 suggestions as flamebait, but rather constructive criticism.

1. Location of templates, templates_c, and cache are in the wrong place.

Separating business logic from template logic in the real world means keep the designers out of the code that actually does real work.

Currently, Smarty encourages coders to put their templates in a subdirectory of the code. That's fine for the coder, saves time, in not clicking around looking for templates. But when it comes to working with a team, on large projects, this means designers are mucking around in the same directories structure as the coder. Also setting permissions on templates_c and cache on a per directory basis.

In sux0r, I look for Smarty templates in two places. First, in a directory called templates located off the root, and if I can't find it there I cascade down and look in the subdirectory i.e. how it is now. Furthermore, templates_c and cache are also moved to subdirectories in a separate temporary directory. Making it easier to manage permissions and not mess up CVS/SVN with a bunch of individual .ignore lists.

Of course, the fact that I was able to do this with Smarty in the first place is a good thing. It shows that it's very flexible. The fact that it's not the default is counter-intuitive.

2) Renderer objects, not "plug-ins"

A few months ago I stumbled on a suggestion in this forum for object notation in Smarty. I figured out thanks to someone smarter than me that instead of writing a bunch of plug-ins I could just do

Code:

        $this->r = new Renderer();
        $this->tpl->assign_by_ref('r', $this->r);


And when in the TPL files I could just do:

Code:

{$r->myFunction()}
{$r->myVar}
{$r->myArray.value}
{insert name="someInsertFunction" someVar=$r->text.someVar}
{$r->isSubscribed($foo.rss_feeds_id)}
{foreach from=$r->feeds(true, $users_id) item=foo}


Etc.

This allows me to create and control what I put in renderer objects, with the option of extending each other. If I needed a new function I could just add it to my Renderer object(s). I could use static variables in my functions to prevent them from being called twice redundantly, a ton of possibilities.

Furthermore, all "inline code" gets discouraged. If you need some "display logic" it goes in the Renderer() object. So a designer doesn't have to look at the code in the template. It's in an intermediary object.

Again, the fact that I can do this with Smarty in the first place is a big plus. Kudos to being awesome But right now I have to mix and match $r->functions() and "smarty_random_globalspace_functions()" which feels like 1999. Smarty is an Object, I fail to see why such a big chunk of it remains in global space.

Ok, thanks for reading. Good luck with the new version, keep it flexible!

EDIT:

Quote:

Since all plugins are now classes (did I mention that yet?), They all extend
Smarty_Internal_PluginBase, which makes $this->smarty a reference to the Smarty
object instance. This is available to all plugins.


Just noticed the above, perhaps everything I wrote is redundant. Excellent! I will check out the Beta when it's available.
Back to top
View user's profile Send private message
Fenn
Smarty Rookie


Joined: 28 May 2003
Posts: 11

PostPosted: Fri Oct 24, 2008 8:47 am    Post subject: Reply with quote

mohrt wrote:
Quote:
I just checked out the latest Smarty 3a SVN. Did you get rid of the idea of multiple Smarty instances? Was there a reason for that? I kinda liked the idea, however, I can think of one or two problems it might introduce.


I got rid of multiple instances because it caused problems with autoload (no way to pass id to reference which instance.) For now, it is a singleton instance. There should be no reason to run multiple instances of Smarty anyways. We'll see what crops up.


Actually I've found having multiple instances of Smarty handy on occasion. For example, the CMS I built for our company spits out the them markup stored in a database into a $doc_content template var for simple pages. But when The Powers That Be demanded that the CMS could also be used for surveys a different approach was needed. A survey needs more structure than HTML markup alone, it also needs to know what questions are being asked and how to validate the responses, etc.

The method of my survey class that generates the markup for the survey uses a Smarty template to build the markup from the list of questions the editor has specified. I then use fetch() to return it as a HTML string. So the first time somebody views a new survey the script requires 2 Smarty instances, one to turn the form into markup, and another to put the generated markup into the template presented to the user.

Singletons have a lot of problems, which ultimately boil down to the fact that a singleton is to all intents and purposes a fancy global var. It also changes the accepted behaviour of the class model, in that you expect to get a new instance of a class, not a reference to the only existing one.

http://www.oreillynet.com/cs/user/view/cs_msg/23417
Back to top
View user's profile Send private message
mohrt
Administrator


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

PostPosted: Fri Oct 24, 2008 3:29 pm    Post subject: Reply with quote

If you notice in alpha, you can create multiple instances of template objects:

Code:
$myTpl = $smarty->createTemplate('foo.tpl');
$myTpl->assign('foo','bar');
echo $myTpl->getRenderedTemplate();


The Smarty singleton itself is just a subclass of a template object, and encapsulates all this internal going on with familiar smarty syntax.

Therefore, even though there is one singleton, you can spawn multiple tpl objects from that singleton.
Back to top
View user's profile Send private message Visit poster's website
rodneyrehm
Administrator


Joined: 30 Mar 2007
Posts: 674
Location: Germany, border to Switzerland

PostPosted: Tue Oct 28, 2008 2:44 pm    Post subject: Reply with quote

First off: way to go. What I've seen thus far looks

Why was PHP's autoload abandoned? What's bothering you about the way SPL autoload works? Not that I miss it, but i don't get the point of getting ridd of it, since it perfectly suits my needs.

There's already been questions about caching gzipped output and saving the Content-Type header along the cached output. What i'm missing here is a rather flexible approach to the If-Modified-Since issues. In Smarty2 caching headers were practically ignored if {includes} or non-caching functions/blocks were used inside the template. I would *really much* like a way to specify if the contents of those "dynamic" parts really require the loading of the page, or could just return a freaking timestamp telling the last-modified time. That way you actually *could* build cache-friendly applications, without losing the benefits of Smarty's great flexibility. If dynamic contents could tell their (real) modification times, deploying cache-friendly pages would not be rocket-science and thus far more applied than they are today. I'd appreciate it, if constructs obstructing the very simple handling of If-Modified-Since/Not Modified could be queried if they actually changed since the given timestamp. All constructs now seem to be separate classes, therefore it should seem fairly easy to have them implement a contentChangedSince()-method. Programmers intending to make use of the-more-useful-HTTP-stuff could have their proprietary constructs react to If-Modified-Since headers. Getting what i'm talking about?

It would also be nice if a non-caching function nested inside a non-caching block would not result in the function being called without arguments, when reading from cache. (A bug in Smarty2 that drove me crazy like nothing else)

Some people use Smarty to generate all sorts of output. X/HTML, Javascript, JSON, XML, younameit. I pretty much hate building XML-docs without my DOM. I don't get the use of Smarty for merely calling json_encode from within the template. I feel the urge to run away when seeing PDFs generated the same way. Yet i still do all that absurd crap - just to have one single interface for controlling the output my application generates. I would love to see some functionality to handle the same data being distributed in various formats (html,json,rss,atom,xml,pdf,..). Primarily functionality that acts like another cache-group on top of the same template-resource (so flushing one template's cache-group gets ridd of all formats derived from that data-set). Seeing "self-compiled" (php-only) templates in Smarty3 should blast my initial points into oblivion, since no text-based template has to be launched to utilze smarty's output cache.


In short:
* Enhance dynamic functions/blocks with the functionality to determine if a »304 Not Modified« can be sent, although the function/block might be declared dynamic.
* Provide abstraction for different formats for the same data-set, with the goal of simplifying the caching of the same data in different formats. (practically introducing "template-groups")
Back to top
View user's profile Send private message Visit poster's website
mohrt
Administrator


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

PostPosted: Tue Oct 28, 2008 3:01 pm    Post subject: Reply with quote

Quote:
Why was PHP's autoload abandoned?


Autoloaded methods do not get the benefits of a PHP op-code cache, and that is crucial to your application's performance. It's pretty easy to get around anyways.

Quote:
In short:
* Enhance dynamic functions/blocks with the functionality to determine if a »304 Not Modified« can be sent, although the function/block might be declared dynamic.
* Provide abstraction for different formats for the same data-set, with the goal of simplifying the caching of the same data in different formats. (practically introducing "template-groups")


Since all templates, cache files, functions, etc. are objects, it will be pretty easy to pass meta-data around with them. Your points should be straight forward to address. Some of them are that I know of (such as dynamic variables to non-cached funcs.) Take a closer look when we get to beta, we'll see what we have covered. If it's not there, it's easy to extend and get what you want.
Back to top
View user's profile Send private message Visit poster's website
rodneyrehm
Administrator


Joined: 30 Mar 2007
Posts: 674
Location: Germany, border to Switzerland

PostPosted: Tue Oct 28, 2008 3:39 pm    Post subject: Reply with quote

mohrt wrote:
Autoloaded methods do not get the benefits of a PHP op-code cache, and that is crucial to your application's performance. It's pretty easy to get around anyways.


Never heard that one before. Maybe i should reconsider my use of autoload... thanks for the hint, though.

mohrt wrote:
Since all templates, cache files, functions, etc. are objects, it will be pretty easy to pass meta-data around with them. Your points should be straight forward to address. Some of them are that I know of (such as dynamic variables to non-cached funcs.)


Oh, even cache-files are supposed to be objects? Smarty3 seems to be getting more interesting by the minute Wink

mohrt wrote:
Take a closer look when we get to beta, we'll see what we have covered. If it's not there, it's easy to extend and get what you want.


I'll keep peeking in - thanks for the reassurance, looking forward to the beta.
Back to top
View user's profile Send private message Visit poster's website
mohrt
Administrator


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

PostPosted: Tue Oct 28, 2008 4:18 pm    Post subject: Reply with quote

Quote:
Oh, even cache-files are supposed to be objects?


Cache files, regardless of their resource type (files, db entries etc.) will contain meta-data, so you can think of them as a serialized object.
Back to top
View user's profile Send private message Visit poster's website
DarkBorg
Smarty n00b


Joined: 30 Oct 2008
Posts: 1

PostPosted: Thu Oct 30, 2008 3:14 pm    Post subject: Reply with quote

Will smarty be completely camelCase? Would be cool, this is one thing about Smarty 2.0 that bothers me a bit...

I took a look at the Sourcecode, looks sexy Smile
Back to top
View user's profile Send private message
douglassdavis
Smarty Junkie


Joined: 21 Jan 2008
Posts: 541

PostPosted: Wed Nov 12, 2008 2:07 pm    Post subject: Reply with quote

I'm pretty excited about 3.0. Is there a comprehensive list of all of the anticipated features anywhere?

Also, if I can contribute to coding or anything else, let me know how.


thanks.
Back to top
View user's profile Send private message
mohrt
Administrator


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

PostPosted: Tue Dec 16, 2008 11:57 pm    Post subject: Reply with quote

douglassdavis wrote:
I'm pretty excited about 3.0. Is there a comprehensive list of all of the anticipated features anywhere?

Also, if I can contribute to coding or anything else, let me know how.


thanks.


There is a README with the alpha, although it isn't a complete feature list. You can contribute by testing the alpha version and posting your findings to the developer list (or here.)
Back to top
View user's profile Send private message Visit poster's website
mniskanen
Smarty Rookie


Joined: 26 Apr 2003
Posts: 20
Location: Lieksa,Finland

PostPosted: Thu Jan 15, 2009 12:23 pm    Post subject: Reply with quote

mohrt wrote:
For now, it is a singleton instance. There should be no reason to run multiple instances of Smarty anyways. We'll see what crops up.


I have written a fully blown web application framework that makes use of Smarty 2.x. It includes a smarty plugin called "cmsmodule" which creates the respectived cms module class instance, makes it load the parameters and finally calls its fetch() method. My applications make use of several plugins, say:
Code:

<body>
...
{cmsmodule name="menu_vertical" src="db" dbtable="mainmenu"}
...
{cmsmodule name="news" dbtable="mynews"}
</body>



Now each of thes modules work a bit like this
Code:

class news_module extends cms_module {
include libs/Smarty.class.php" ;
  function fetch(){
    $_smarty = new Smarty();
    ... fill the template...
    return $_smarty->fetch("news.tpl"); // a small, simple template
  }
}


Most modules create a Smarty instance for thein own private purposes and not interfere with the "mother" Smarty instance (at least with Smarty 2.x). Will the "singleton" approach break my framework completery? That is: do I have to take into account the singleton when naming template variables, for instance, so that filling stuff for one module will not trash my main.tpl?

Markku
Back to top
View user's profile Send private message
U.Tews
Administrator


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

PostPosted: Thu Jan 15, 2009 3:16 pm    Post subject: Reply with quote

Markku

This will not be a problem at all. Smarty3 alows to assign variables to individual templates. Your class could look like this:

Code:

class news_module extends cms_module extends Smarty_Internal_Base {
  function fetch(){
     $_tpl = $this->smarty->createTemplate("news.tpl");  // create template object
    ... fill the template...
    $_tpl->assign('foo','bar');  // assign variables private for this template
    .....
    return $this->smarty->fetch($_tpl); // a small, simple template
  }
}
Back to top
View user's profile Send private message
mniskanen
Smarty Rookie


Joined: 26 Apr 2003
Posts: 20
Location: Lieksa,Finland

PostPosted: Thu Jan 15, 2009 4:05 pm    Post subject: Reply with quote

U.Tews wrote:
Markku

This will not be a problem at all. Smarty3 alows to assign variables to individual templates.


Whew, thank you. That looks very nice indeed and I feel very, very relieved now. I think I will have a good look at the current SVN stuff as soon as I have the time.

My modules being relatively small due to my minimalist approach I do not think it should take much time to convert them to the new style. And the main Smarty class looks very small indeed now. Having used Smarty for more than six years now I am happy to see the new development in progress. I will keep monitoring the status from now on!

Markku
Back to top
View user's profile Send private message
c960657
Smarty Regular


Joined: 07 May 2003
Posts: 75
Location: Copenhagen, Denmark

PostPosted: Mon Feb 16, 2009 10:06 pm    Post subject: Reply with quote

Perhaps I am joining the discussion a bit late, but hopefully not too late Smile I have not studied Smarty3 closely yet, so my comments are mostly based on experiences with Smarty2.

  1. It would be convenient if custom modifiers had access to the Smarty and Template instances like template functions have.

  2. When I make custom template functions, I often add support for an assign parameter like the one for {cycle}, i.e. {cycle values="a,b" assign=myvar}. I suggest adding an assign modifier modifier that can be used with all template functions for assigning the output to a variable, i.e. {cycle|assign:"myvar" assign=myvar}.

  3. If you have a big HTML file, it is useful to split it up and put the individual parts in seperate files that are included using {include}. To do this in Smarty2, the parent template file must have its own path hardcoded, e.g. {include file="very/long/subdir/structure/part1.tpl"}, or it can look it up using {assign var=dir value=$smarty.template|dirname}{include file="$dirname/part1.tpl"}. It would be nice with an easier way to do inclusions using relative paths.

  4. I have a large number of loosely coupled projects that each offer some Smarty plugins. In order to allow Smarty to discover these plugins, I must either set $smarty->plugins_dir to a big array of all projects, or I can set it to one dir that contains symlinks to all other projects. Neither is easy/cheap to do at runtime, so I currently use a command-line script to setup these paths whenever I add a project. It would be convenient with some kind of callback mechanism (a la autoload for classes) that is invoked when Smarty needs the path to a particular template and/or a callback when the compiler is invoked. This would also allow the automatic creation of compile_dir's at runtime and other expensive operations that are only done in the relatively rare occasion that a template is compiled.

  5. In Smarty2, custom resource types are 2nd class citizens compared to the file resource type that is treated specially many places in Smarty.class.php. I hope Smarty3 will have a cleaner cut so that custom resource types become as powerful as file resource types (it looks like this is already the case).

  6. As I understand resource types, they can be considered "virtual filesystems", i.e. places where templates are stored. This understanding seems incompatible with the new php resource type - php resources are files in the filesystem just like file resources, just with a different syntax/semantics. I suggest an alternative approach: Resource types are just places to store files. Whether the file is interpreted as Smarty template code ({html_table loop=$foo}), pure PHP (<?php bar(); ?>) or perhaps some future XML-Smarty-variant (like the syntax used in ASP.NET, <smarty:html_table loop="$foo" />) is determined by something similar to a MIME type reported by the resource implementation. This pseudo-MIME-type (or "syntax type") maps to individual compilers (in the basic setup this is the existing Smarty_Internal_Compiler and an almost empty class that basically just passes PHP files through). The file based resource can return the type based on the file extension (.tpl, .php and .xtpl for the three examples) - or perhaps it can be contained in the template itself (like the XML prolog, <?xml version="1.0" charset="ISO-8859-1"?>). The string based resource type can use a syntax similar to the one used by the data: URI scheme, e.g. "string:type=tpl,{$foo}" or "string:type=php,<?php echo time() ?>".

  7. A general method for returning metadata about each resource (as described above) would also make it possible to specify settings per file that can currently only be specified globally, e.g. $left_delimiter, $right_delimiter, $compile_check etc. These can either be specified by the resource implementation (similar to HTTP headers), or specified inline (like the XML prolog).

  8. I'll be happy to discuss or elaborate these suggestions, or to tell about how we use Smarty in the company I work for. I'll probably have more comments when I get a chance to think more about this Smile

To what extent is Smarty3 supposed to be backwards compatible with Smarty2?
Back to top
View user's profile Send private message Visit poster's website
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 -> Smarty 3 All times are GMT
Goto page Previous  1, 2, 3, 4, 5, 6, 7  Next
Page 3 of 7

 
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