|
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 |
cmanley Smarty Rookie
Joined: 13 Jan 2013 Posts: 5
|
Posted: Wed Oct 16, 2013 6:42 am Post subject: How to get current template from modifier (w dirty solution) |
|
|
I wrote a text translation modifier, that requires the name of the template file in which in was called.
Is there a proper way to access this information via $this or perhaps some global smarty variable?
So far, I could only come up with this dirty solution (made for Smarty v3) that uses a stack trace, so I hope there's some better way:
Code: |
/**
* Returns the string translated into the current language.
*
* @param string $key
* @return string
*/
function modifier_xlat($key) {
$template = null;
if (1) {
/* For lack of a better solution, in order to find the current template, scan the stack trace for this frame:
[file] => /usr/local/share/php/Smarty-3.1.14/libs/sysplugins/smarty_internal_template.php
[function] => fetch
[class] => Smarty_Internal_TemplateBase
[object] => Smarty_Internal_Template Object
... or this frame (same as $this) ...
[file] => /usr/local/share/php/Smarty-3.1.14/libs/sysplugins/smarty_internal_templatebase.php
[function] => fetch
[class] => Smarty
[object] => Smarty Object
*/
//$trace_options = version_compare(PHP_VERSION, '5.3.6', '>=') ? (DEBUG_BACKTRACE_PROVIDE_OBJECT | DEBUG_BACKTRACE_IGNORE_ARGS) : true;
$trace_options = defined('DEBUG_BACKTRACE_PROVIDE_OBJECT') ? (DEBUG_BACKTRACE_PROVIDE_OBJECT | DEBUG_BACKTRACE_IGNORE_ARGS) : true;
$trace_limit = 0;
$frames = version_compare(PHP_VERSION, '5.4', '>=') ? debug_backtrace($trace_options, $trace_limit) : debug_backtrace($trace_options);
foreach ($frames as $frame) {
if (@$frame['object'] && (@$frame['function'] == 'fetch')) {
if (is_a($frame['object'], 'Smarty_Internal_Template')) { // for template included from another template
$template = $frame['object'];
break;
}
elseif (is_a($frame['object'], 'Smarty_Internal_TemplateBase')) { // for template loaded by Smarty (== this)
if ($frame['object']->template_objects) {
foreach ($frame['object']->template_objects as $template_key => $template) {
break;
}
break;
}
}
}
}
unset($frames, $frame, $trace_options, $trace_limit);
}
if (!$template) {
throw new Exception('Failed to determine template name in which modifier ' . __FUNCTION__ . ' was called');
}
//error_log($template->template_resource);
//error_log($template->source->filepath);
require_once('site/classes/Translator.php');
$t = new Translator($template->source->filepath);
//error_log('translate file="' . $t->getFile() . '"');
return $t->xlat($key);
}
|
I have no idea if it works with Smarty v2. |
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
|
Back to top |
|
cmanley Smarty Rookie
Joined: 13 Jan 2013 Posts: 5
|
Posted: Mon Oct 21, 2013 1:17 pm Post subject: |
|
|
Thanks, that's a viable alternative. I hope it will become easier in future Smary versions. |
|
Back to top |
|
cmanley Smarty Rookie
Joined: 13 Jan 2013 Posts: 5
|
Posted: Thu May 28, 2015 9:56 pm Post subject: |
|
|
Just checking... any update on this since 2013?
Another question:
Is there a way to determine from within the modifier method, which object in $this->template_objects is the one belonging to the source file in which the modifier was called?
If so, then I can call ->source->filepath on that object to get the source file name.
That will be a whole lot faster and future proof than performing stack traces. |
|
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
|