Re: Smarty 1.4.0 modifications
| From: | Monte Ohrt | Date: | Mon, 14 May 2001 14:07:43 +0000 |
| Subject: | Re: Smarty 1.4.0 modifications | ||
| References: | 1 | Groups: | php.smarty.general |
| Request: | Send a blank email to php-smarty-general+get-759@lists.php.net to get a copy of this message | ||
Hi Ivo,
Ivo Jansch wrote:
>
> Hi,
>
> I've made some modifications to Smarty 1.4.0. Please have a look to see
> if you can put this in 'official' Smarty.
>
> 1) In the _fetch_template_source function, I added a line
> of code that appends the template extension if the user
> forgot it:
>
> if
> (substr($resource_name,-(strlen($this->tpl_file_ext)))!=$this->tpl_file_ext)
> $resource_name.=$this->tpl_file_ext;
>
> (I use this because I must be able to do an
> {include file=$template} where $template contains the
> name of the template without extension)
If you look in the 1.4.0 release notes, you will see that $tpl_file_ext
is no longer required. Since Smarty doesn't traverses the template
directory up front, there is no more need for requiring it. Template
resources can have anything in them, with or without the $tpl_file_ext.
This isn't something we'll put in Smarty, as it will break things when
the template resource does not end in the same extention.
>
> 2) In _smarty_include, I added the following lines (just above the
> extract($this->_tpl_vars) line):
>
> if (is_array($_smarty_include_vars["local"]))
> {
> $this->_tpl_vars = array_merge($this->_tpl_vars,
> $this->_tpl_vars["local"]);
> }
>
> This has the following result: If you include a template,
> you can make variables appear 'local' to that template, even
> when the data is in some complex datastructure in the main
> template.
There is a better way to handle this without modifying Smarty:
Example main.tpl:
This is the book: {$book.title}
<br>{include file=author.tpl author=$book.author}
And this is the movie: {$movie.title}
<br>{include file=author.tpl author=$movie.author}
author.tpl:
<b>{$author.name}</b> {$author.birthdate}
This way you don't introduce "local" into the variable namespace, and
you can pass as many stuctures as you like.
Monte
then in the template
>
> Example main.tpl:
>
> This is the book: {$book.title}
> <br>{include file=author.tpl local=$book.author}
>
> And this is the movie: {$movie.title}
> <br>{include file=author.tpl local=$movie.author}
>
> author.tpl:
>
> <b>{$name}</b> {$birthdate}
>
> All variables from $book.author or $movie.author are now 'local'
> to the author template, and the person designing author.tpl does
> not need to know whether he's creating a template for $book.author
> or $movie.author or...
>
>
> Since these 2 modifications don't break anything and might be useful for
> other users too, you might want to consider putting this in the code.
> (Which would also save me from having to re-apply these modifications to
> each new Smarty version you release :)
>
> Greetings,
> Ivo
>