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

How to get current template from modifier (w dirty solution)

 
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 -> General
View previous topic :: View next topic  
Author Message
cmanley
Smarty Rookie


Joined: 13 Jan 2013
Posts: 5

PostPosted: Wed Oct 16, 2013 6:42 am    Post subject: How to get current template from modifier (w dirty solution) Reply with quote

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
View user's profile Send private message
U.Tews
Administrator


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

PostPosted: Wed Oct 16, 2013 7:21 pm    Post subject: Reply with quote

You can pass this information as additional parameter to the modifier.

Code:
{$foo|xlat:$smarty.template}


See
http://www.smarty.net/docs/en/language.variables.smarty.tpl#language.variables.smarty.template
Back to top
View user's profile Send private message
cmanley
Smarty Rookie


Joined: 13 Jan 2013
Posts: 5

PostPosted: Mon Oct 21, 2013 1:17 pm    Post subject: Reply with quote

Thanks, that's a viable alternative. I hope it will become easier in future Smary versions.
Back to top
View user's profile Send private message
cmanley
Smarty Rookie


Joined: 13 Jan 2013
Posts: 5

PostPosted: Thu May 28, 2015 9:56 pm    Post subject: Reply with quote

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
View user's profile Send private message
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 -> General All times are GMT
Page 1 of 1

 
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