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

Smarty 3.0 execute extended method

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


Joined: 11 Dec 2010
Posts: 30

PostPosted: Sat Dec 11, 2010 12:57 pm    Post subject: Smarty 3.0 execute extended method Reply with quote

Hey guys, Maybe it's my eye-sight but i've spent hours trying to hunt through docs and I can't figure out this problem.

So i have a class called Templates.class.php, which is...

Code:
<?php
require_once (CONF_DIR_LIB . 'Smarty/Smarty.class.php');

final class Templates extends Smarty {
 
   // Variables & Constants
   // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   
   private $_theme, $_ssl = false, $_home_url = CONF_ROOT;
   
   // Class Constructor Method
   // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   
   public function __construct($style, $ssl = false) {
    parent::__construct();
   
    $session = Config::Session();
    $this->_home_url = $ssl ? CONF_DOC_HOME : CONF_ROOT;
    $this->_theme = $session->getSession(Api::SESSION_NAME_THEME);
    $this->_lang = $session->getSession(Api::SESSION_NAME_LANG);
   
    $this->enableSecurity('Templates_Security');
    $this->error_reporting = E_ALL ^ E_NOTICE; // temporary; but i will take out later when i can be bothered
      $this->template_dir   = sprintf('%s/%s', CONF_DIR_TMPL . $this->_theme, $style);
      $this->compile_dir = sprintf('%sSmarty/generated/%s/%s/templates_c/%s', CONF_DIR_LIB, $this->_theme, $style, Config::$langLocale->language);
   
    if (!file_exists($this->compile_dir))
      @mkdir($this->compile_dir);
      
      $this->registerPlugin(Smarty::PLUGIN_MODIFIER, 'seo_url', array($this, 'modifier_seo_url'));
      $this->registerPlugin(Smarty::PLUGIN_MODIFIER, 'url', array($this, 'modifier_url'));
      $this->registerPlugin(Smarty::PLUGIN_MODIFIER, 'image', array($this, 'modifier_images'));
      $this->registerPlugin(Smarty::PLUGIN_MODIFIER, 'uploads', array($this, 'modifier_uploads'));
      $this->registerPlugin(Smarty::PLUGIN_MODIFIER, 'icons', array($this, 'modifier_images_icon'));
      $this->registerPlugin(Smarty::PLUGIN_MODIFIER, 'style', array($this, 'modifier_stylesheet'));
      $this->registerPlugin(Smarty::PLUGIN_MODIFIER, 'script', array($this, 'modifier_javascript'));
      $this->registerPlugin(Smarty::PLUGIN_MODIFIER, 'jquery', array($this, 'modifier_jquery'));
      $this->registerPlugin(Smarty::PLUGIN_MODIFIER, 'jstyle', array($this, 'modifier_jquery_style'));
   
    //$this->registerFilter(Smarty::FILTER_PRE, array($this, 'precompile_filter_language'));
   
    $this->assign('menuPages', Api::buildMenuPages(true));
    $this->assign('Locale', Config::$langLocale);
   }
   
   // Extended Methods
   // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
  function display($resource_name, $cache_id = null, $compile_id = null, $parent = null) {
    $this->assign(array(
      'breadCrumbs' => BreadCrumb::display(),
      'charset' => Config::$langLocale->html_charset
    ));
   
    parent::display(sprintf('%s.tpl', $resource_name), $cache_id, $compile_id, $parent);
  }
 
  public function precompile_filter_language($tmpl) {/*
    $session = Config::Session();
    $db = Config::MySQL();
   
    $labels = array();
      preg_match_all('~(/##(.*?)##/)~i', $tmpl, $labels);
      
      foreach ($labels[max(array_keys($labels))] as $label) {
         $t_replace = $db->qc(sprintf('
        SELECT
          lt_text
        FROM
          %s
        WHERE
          lt_label = %s
            AND la_id = %d
        ',
        CONF_DB_PREFIX . Api::MYSQL_TABLE_LANGUAGES_TEXT,
        $db->esc(trim($label)),
        $session->getSession(Api::SESSION_NAME_LANG)
      ));
     
      $replacement = DEVELOPER_MODE ? (empty($t_replace) ? "[ MISSING:{$label}]" : $t_replace) : $t_replace;
      $tmpl = stripslashes(str_ireplace("/##{$label}##/", $replacement, $tmpl));
      }
      
      return $tmpl;
   */}
 
  // Public Methods
   // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   
   public function page_title($str) {
      $this->assign('script_title', $str);
   }
 
   public function assign_error_message($errors) {
      $this->assign('error_message', Api::encodeOutputCharacters(is_array($errors) ? implode("\r\n", (array) $errors) : $errors));
   }
   
   public function assign_success_message($str) {
      $this->assign('success_message', Api::encodeOutputCharacters($str));
   }
   
  // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
   public function modifier_seo_url($link) {
      return $this->modifier_url(preg_replace('~[^a-z0-9_/%-]~i', '-', $link));
   }
   
   public function modifier_url($link) {
      return $this->_home_url . $link;
   }
   
   public function modifier_images($link) {
      return $this->modifier_url(sprintf('static/images/%s/%s', $this->_theme, $link));
   }
 
  public function modifier_uploads($link) {
      return $this->modifier_url(sprintf('static/uploads/%s', $link));
   }
 
  public function modifier_images_icon($link) {
      return $this->modifier_url(sprintf('static/images/%s/icons/%s', $this->_theme, $link));
   }
   
   public function modifier_stylesheet($src) {
      return '<link type="text/css" rel="stylesheet" href="' . $this->modifier_url('static/css/' . $src) . '" />';
   }
   
   public function modifier_javascript($src) {
      return '<script type="text/javascript" src="' . $this->modifier_url('static/js/' . $src) . '"></script>';
   }
   
   public function modifier_jquery($src, $theme = false) {
      $src = $this->modifier_url($theme ? "static/jquery/themes/{$this->_theme}/$src" : "static/jquery/$src");
      return '<script type="text/javascript" src="' . $src . '"></script>';
   }
   
   public function modifier_jquery_style($src) {
      return '<link type="text/css" rel="stylesheet" href="' . $this->modifier_url("static/jquery/themes/{$this->_theme}/$src") . '" />';
   }
 
}


I have a function, this function is called jquery, and sits in... Smarty/plugins/function.jquery.php

Code:
<?php
function smarty_function_jquery($params, $smarty) {
 
  var_dump(get_class($smarty)); // debugging, and returns : "string(24) "Smarty_Internal_Template""
  die();
 
   if (isset($params['theme'])) {
      $s = $smarty->modifier_jquery_style('jquery-ui.css');
      $s .= "\r\n" . $smarty->modifier_jquery_style('jquery.ui.all.css');
      $s .= "\r\n" . $smarty->modifier_jquery('jquery.js');
      $s .= "\r\n" . $smarty->modifier_jquery('jquery-ui.js');
   } else {
      $s = $smarty->modifier_jquery('jquery.js');
   }
   
   return $s;
}


Okay, simple problem, in Smarty 2.0 (i upgraded from) parameter 2, was "&$smarty", the reference which directly accessed the Templates class. however $smarty in Smarty 3.0 accesses Smarty_Internal_Template, How can i get access to my Templates object? ..or is there anything documented i have missed?

Thanks Smile
Back to top
View user's profile Send private message
TakingSides
Smarty Rookie


Joined: 11 Dec 2010
Posts: 30

PostPosted: Sat Dec 11, 2010 1:19 pm    Post subject: Reply with quote

Okay I've managed to find a solution, so i'll state it here, so if it helps others then cool, or if there's a better solution I'd love to hear it Smile

Code:
<?php
function smarty_function_jquery($params, $smarty) { 
   if (isset($params['theme'])) {
      $s = $smarty->parent->parent->modifier_jquery_style('jquery-ui.css');
      $s .= "\r\n" . $smarty->parent->parent->modifier_jquery_style('jquery.ui.all.css');
      $s .= "\r\n" . $smarty->parent->parent->modifier_jquery('jquery.js');
      $s .= "\r\n" . $smarty->parent->parent->modifier_jquery('jquery-ui.js');
   } else {
      $s = $smarty->parent->parent->modifier_jquery('jquery.js');
   }
   
   return $s;
}
Back to top
View user's profile Send private message
U.Tews
Administrator


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

PostPosted: Sat Dec 11, 2010 2:12 pm    Post subject: Reply with quote

Smarty3 does return in plugins the object of it's Smarty_Internal_Template class. But it does allow transparent acces to all properties and methods of the (extended) Smarty class object.

So your original code should work.

Your solution of using $smarty->parent->parent will fail depending of the level of subtemplates it was used.
Back to top
View user's profile Send private message
HJack
Smarty Rookie


Joined: 12 Aug 2008
Posts: 9

PostPosted: Thu Jun 30, 2011 3:19 pm    Post subject: Reply with quote

Quote:
it does allow transparent acces to all properties and methods of the (extended) Smarty class object


Seems it doesen't work Sad

Eg.
Code:
 class XYZ extends Smarty {
public function __construct($style, $ssl = false) {
    parent::__construct();
}

function myFunction(){
  // do something
}
}


then in plugin when using
Code:
$template->myFunction();


I get
Code:

SmartyException: unknown method "myFunction" in  .....smarty_internal_wrapper.php on line 117
Back to top
View user's profile Send private message
rodneyrehm
Administrator


Joined: 30 Mar 2007
Posts: 674
Location: Germany, border to Switzerland

PostPosted: Thu Jun 30, 2011 3:39 pm    Post subject: Reply with quote

that's because the plugin is passed the Smarty_Internal_Template instance, instead of the Smarty instance.

try: $template->smarty->myFunction();
Back to top
View user's profile Send private message Visit poster's website
HJack
Smarty Rookie


Joined: 12 Aug 2008
Posts: 9

PostPosted: Thu Jun 30, 2011 4:04 pm    Post subject: Reply with quote

Quote:
$template->smarty->myFunction();

SOLVED!!!! Thank you!!!! Very Happy Very Happy Very Happy Very Happy
Back to top
View user's profile Send private message
U.Tews
Administrator


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

PostPosted: Thu Jun 30, 2011 6:31 pm    Post subject: Reply with quote

HJack

Which version of Smarty3 did you use?

The transparent access to Smarty methods does still work.

I have just tested your "$template->myFunction();" with Smarty 3.0 and Smarty 3.1
Back to top
View user's profile Send private message
U.Tews
Administrator


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

PostPosted: Thu Jun 30, 2011 6:45 pm    Post subject: Reply with quote

Did you use function names with underscore like my_function?

I just found out that the transparent access does fail in Smarty3.0 if function names with underscore are used.
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 -> Plugins 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