Guest User

class.smarty.php

a guest
Apr 18th, 2012
48
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php if (!defined('APPLICATION')) exit();
  2. /*
  3. Copyright 2008, 2009 Vanilla Forums Inc.
  4. This file is part of Garden.
  5. Garden is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
  6. Garden is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  7. You should have received a copy of the GNU General Public License along with Garden.  If not, see <http://www.gnu.org/licenses/>.
  8. Contact Vanilla Forums Inc. at support [at] vanillaforums [dot] com
  9. */
  10.  
  11. class Gdn_Smarty {
  12.    /// Constructor ///
  13.  
  14.    /// Properties ///
  15.  
  16.    /**
  17.     * @var Smarty The smarty object used for the template.
  18.     */
  19.    protected $_Smarty = NULL;
  20.  
  21.    /// Methods ///
  22.  
  23.    /**
  24.     * Render the given view.
  25.     *
  26.     * @param string $Path The path to the view's file.
  27.     * @param Controller $Controller The controller that is rendering the view.
  28.     */
  29.    public function Render($Path, $Controller) {
  30.       $Smarty = $this->Smarty();
  31.  
  32.       // Get a friendly name for the controller.
  33.       $ControllerName = get_class($Controller);
  34.       if (StringEndsWith($ControllerName, 'Controller', TRUE)) {
  35.          $ControllerName = substr($ControllerName, 0, -10);
  36.       }
  37.  
  38.       // Get an ID for the body.
  39.       $BodyIdentifier = strtolower($Controller->ApplicationFolder.'_'.$ControllerName.'_'.Gdn_Format::AlphaNumeric(strtolower($Controller->RequestMethod)));
  40.       $Smarty->assign('BodyID', $BodyIdentifier);
  41.       //$Smarty->assign('Config', Gdn::Config());
  42.  
  43.       // Assign some information about the user.
  44.       $Session = Gdn::Session();
  45.       if($Session->IsValid()) {
  46.          $User = array(
  47.             'Name' => $Session->User->Name,
  48.             'CountNotifications' => (int)GetValue('CountNotifications', $Session->User->CountNotifications, 0),
  49.             'CountUnreadConversations' => (int)GetValue('CountUnreadConversations', $Session->User, 0),
  50.             'CountDiscussions' => (int)GetValue('CountDiscussions', $Session->User, 0),
  51.             'CountBookmarks' => (int)GetValue('CountBookmarks', $Session->User, 0),
  52.             'CountDrafts' => (int)GetValue('CountDrafts', $Session->User, 0),
  53.             'CountComments' => (int)GetValue('CountComments', $Session->User, 0),
  54.             'SignedIn' => TRUE);
  55.       } else {
  56.          $User = FALSE; /*array(
  57.             'Name' => '',
  58.             'CountNotifications' => 0,
  59.             'SignedIn' => FALSE);*/
  60.       }
  61.       $Smarty->assign('User', $User);
  62.  
  63.       // Make sure that any datasets use arrays instead of objects.
  64.       foreach($Controller->Data as $Key => $Value) {
  65.          if($Value instanceof Gdn_DataSet) {
  66.             $Controller->Data[$Key] = $Value->ResultArray();
  67.          } elseif($Value instanceof stdClass) {
  68.             $Controller->Data[$Key] = (array)$Value;
  69.          }
  70.       }
  71.      
  72.       $Controller->Data['BodyClass'] = GetValue('CssClass', $Controller->Data, '', TRUE);
  73.  
  74.       $Smarty->assign('Assets', (array)$Controller->Assets);
  75.       $Smarty->assign('Path', Gdn::Request()->Path());
  76.  
  77.       // Assigign the controller data last so the controllers override any default data.
  78.       $Smarty->assign($Controller->Data);
  79.  
  80.       $Smarty->Controller = $Controller; // for smarty plugins
  81.       $Smarty->security = TRUE;
  82.       $Smarty->security_settings['IF_FUNCS'] = array_merge($Smarty->security_settings['IF_FUNCS'],
  83.          array('CheckPermission', 'MultiCheckPermission', 'GetValue', 'SetValue', 'Url'));
  84.       $Smarty->secure_dir = array($Path);
  85.       $Smarty->display($Path);
  86.    }
  87.  
  88.    /**
  89.     * @return Smarty The smarty object used for rendering.
  90.     */
  91.    public function Smarty() {
  92.       if(is_null($this->_Smarty)) {
  93.          $Smarty = Gdn::Factory('Smarty');
  94.  
  95.          $Smarty->cache_dir = PATH_LOCAL_CACHE . DS . 'Smarty' . DS . 'cache';
  96.          $Smarty->compile_dir = PATH_LOCAL_CACHE . DS . 'Smarty' . DS . 'compile';
  97.          $Smarty->plugins_dir[] = PATH_LIBRARY . DS . 'vendors' . DS . 'SmartyPlugins';
  98.  
  99.          $this->_Smarty = $Smarty;
  100.       }
  101.       return $this->_Smarty;
  102.    }
  103. }
RAW Paste Data