|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
douglassdavis Smarty Junkie
Joined: 21 Jan 2008 Posts: 541
|
Posted: Tue Feb 16, 2010 7:40 pm Post subject: Smarty 2 current directory plugin |
|
|
I use a system that is very modularized. There are many libraries of templates, but groups of templates are not always in the same directory.
It would be helpful if a "library" template is able to get its own directory, so that when it includes other templates in the same directory, it would would know where it is.
As of right now, I always have to assign each directory that will be used as a template var.
So, here is a compiler plugin to get the directory of the template being compiled.
I guess it would only work for windows and unix variants because of how they specify a root path.
EDIT: Newer system independent version in post below
Code: |
/* DEPRECATED. USE VERSION IN POST BELOW */
function smarty_compiler_current_dir($tag_attrs, &$compiler) {
$_params = $compiler->_parse_attrs($tag_attrs);
if (!isset($_params['assign'])) {
$compiler->_syntax_error("current_dir: missing 'assign' parameter", E_USER_WARNING);
return;
}
$template_dir = $compiler->template_dir;
$template_file = $compiler->_current_file;
if (stripos(PHP_OS, 'win') === 0) {
if (substr($template_file, 1,1)==':'
|| substr($template_file, 0,1)=='/'
|| substr($template_file, 0,1)=='\\') {
$file_absolute = true;
}
}
else if (substr( $template_file, 0, 1) == '/') {
$file_absolute = true;
}
if ($file_absolute) {
$path = $template_file;
}
else {
$path = $template_dir.'/'.$template_file;
}
$dir = dirname(realpath($path));
$dir = str_replace("'", "\\\'", $dir);
return "\$this->assign({$_params['assign']}, '{$dir}');";
}
|
Last edited by douglassdavis on Wed Sep 14, 2011 12:52 pm; edited 4 times in total |
|
Back to top |
|
mikarama Smarty n00b
Joined: 22 Nov 2010 Posts: 3
|
Posted: Mon Nov 22, 2010 3:30 pm Post subject: |
|
|
I had the same concern, but I would need something less system dependant.
Aren't there another options do to this ? |
|
Back to top |
|
douglassdavis Smarty Junkie
Joined: 21 Jan 2008 Posts: 541
|
Posted: Mon Nov 22, 2010 3:59 pm Post subject: |
|
|
I don't think there is another way in Smarty 2, although I welcome any improvements on this one. The main system dependent thing is to find out whether a path is absolute or relative. Works for me, but I use Linux most of the time..
Just thought of another non-system dependent approach:
Code: |
function smarty_compiler_current_dir($tag_attrs, &$compiler)
{
$_params = $compiler->_parse_attrs($tag_attrs);
if (!isset($_params['assign'])) {
$compiler->_syntax_error("current_dir: missing 'assign' parameter", E_USER_WARNING);
return;
}
$old_dir = getcwd();
chdir($compiler->template_dir);
$dir = dirname(realpath($compiler->_current_file));
chdir($old_dir);
return "\$this->assign({$_params['assign']}, '{$dir}');";
}
|
This one relies on the operating system to do most of the work.
use like: {current_dir assign=myvar}
Last edited by douglassdavis on Mon Dec 06, 2010 2:22 pm; edited 1 time in total |
|
Back to top |
|
mikarama Smarty n00b
Joined: 22 Nov 2010 Posts: 3
|
Posted: Mon Nov 22, 2010 4:23 pm Post subject: |
|
|
That's quite odd something as obvious isn't implemented.
Thanks for sharing your code, as I need a production stable solution, I will just kick my files somewhere i can easily access.
That is, outside the template folder ! odd !
Regards
mika
Edit : i'm sure your code would be very valid if I was using it on one server, but my code is to be released, and used on various servers. |
|
Back to top |
|
douglassdavis Smarty Junkie
Joined: 21 Jan 2008 Posts: 541
|
Posted: Mon Dec 06, 2010 2:20 pm Post subject: |
|
|
update: just tested latest version of the plugin... Works great and is system independent. |
|
Back to top |
|
mikarama Smarty n00b
Joined: 22 Nov 2010 Posts: 3
|
Posted: Thu Dec 09, 2010 11:04 pm Post subject: |
|
|
Hi
I'm not sure to anderstand what plugin version you're refering to ?
EDIT : Oh! I guess you edited the code |
|
Back to top |
|
|
|
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
|