Two patches for Smarty post 2.3.1
| From: | Bharat Mediratta | Date: | Tue, 19 Nov 2002 23:38:06 +0000 |
| Subject: | Two patches for Smarty post 2.3.1 | ||
| Groups: | php.smarty.dev | ||
| Request: | Send a blank email to php-smarty-dev+get-337@lists.php.net to get a copy of this message | ||
I've been using Smarty for a few months on a project of mine and am
quite happy with it. However, I've had to patch it in two ways to
enable certain special functionality that I need.
I don't know exactly what your patch procedure is, and I'm willing
to maintain these two patches separately if nobody else has a need
for them. But since I'm happy with Smarty and I'd like to help it
keep moving in a direction that I can use, I'm sending them in here.
Comments, criticisms, alternative suggestions are welcome.
-Bharat
__________
First, the use_sub_dirs functionality does not work cleanly for me.
My code provides Smarty to add-on modules, each of which can have
its own templates directory. In order for the templates to be able
to {include} each other, each module must set the $smarty->templates_dir
appropriately before doing a fetch. This means that when I go to
fetch a template, it typically looks like this:
$smarty->template_dir = dirname(__FILE__) . "/templates";
$smarty->fetch("file.tpl");
so that it uses the templates/ directory for the current module.
The problem with this is that the $auto_source variable in
Smarty::_get_auto_filename() no longer has the complete path, which
means that with use_sub_dirs off, the compiled template for this file
will be:
file.tpl
instead of
^path^to^templates^file.tpl
So if multiple modules have a "file.tpl" (entirely likely), then they
wind up sharing the same file. Yes, I could also require them to rewrite
their templates_c directory but there are logistical reasons why that
would be a bad idea. Furthermore, if we do use full paths then it's
possible the the paths that are created can be *very* long on systems
that have deep directory trees.
My fix for this is twofold.
1. Relative template file names are now be prepended with the
template_dir so that they are fully qualified.
2. There is a new option ($smarty->short_template_c_names) that
will compress the directory down to a unique value, so:
^path^to^templates^file.tpl
would become:
f4fd7f7c0cb8e2bc^file.tpl
This has been tested on Unix and Windows with 2.3.1
__________
Second patch. Smarty lacks good support for registering class methods as
callbacks. You must register functions in the global namespace. However,
I want to minimize the number of functions I declare in the global
namespace,
so I opted to have a single callback method which delegates any calls from
Smarty to the appropriate class. Ie, I want to say:
foreach (array('Url', 'Text', 'Date', 'Style') as $method) {
$smarty->register_function('gallery' . $method,
'galleryTemplateCallback');
}
This means that any calls to {galleryUrl} or {galleryText} will go to the
galleryTemplateCallback() function. It will delegate to the appropriate
listener (there may be several such listeners). The problem is that the
callback does not indicate which template function was actually invoked,
so I've tweaked the compiler to include a new argument, "__function" that
will always contain the name of the template function that was invoked.
This lets me have a single function like this:
function galleryTemplateCallback($params, $content) {
// blah
// blah
print $delegate->invoke($params['__function'], $params, $content);
}
This is a very abbreviated version just for clarity. To really see how
this code delegates control to theme and localization code, you can
check out the Gallery source code (see the "gallery2" module in CVS at
gallery.sourceforge.net).
This has been tested on Unix and Windows with 2.3.1