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

Include with own custom template resource doesn’t get assign

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


Joined: 10 Aug 2010
Posts: 22

PostPosted: Tue Feb 07, 2012 7:19 pm    Post subject: Include with own custom template resource doesn’t get assign Reply with quote

It seems that I can’t populate simple smarty variables like {$value} in a included template using my own custom template resource. I see the content but not anything which should be propagated automatically.
What do I need to do get the assigned variables down to included resources?

After ->assign('mainData', 'data to test')

Code:

<html>
<head>
<title></title>
</head>
<body>
<p>main</p>
<p>{$mainData}</p>


{include file="step1.tpl"}

{include file="module:step2"}
</body>
</html>


and step1.tpl
Code:

<p>step1</p>
<p>{$mainData}</p>


custom template resource reads and outputs step2.tpl
Code:

<p>step2</p>
<p>{$mainData}</p>


The html output is:
Code:

<html>
<head>
<title></title>
</head>
<body>
<p>main</p>
<p>data to test</p>


<p>step1</p>
<p>data to test</p>

<p>step2</p>
<p></p>
</body>
</html>


Is there a special trick I need to do in the custom template resource?
Back to top
View user's profile Send private message
U.Tews
Administrator


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

PostPosted: Tue Feb 07, 2012 7:41 pm    Post subject: Reply with quote

How does the code for your custom resource look like?
Back to top
View user's profile Send private message
kk
Smarty Rookie


Joined: 10 Aug 2010
Posts: 22

PostPosted: Tue Feb 07, 2012 7:45 pm    Post subject: Reply with quote

like this:

Code:
<?php
class Smarty_Resource_Module extends Smarty_Resource_Custom {
   /**
   * Fetch a template and its modification time from database
   *
   * @param string $name template name
   * @param string $source template source
   * @param integer $mtime template modification timestamp (epoch)
   * @return void
   */
   protected function fetch($moduleName, &$tplOutput, &$mtime){
       global $__page, $__dbC;

       $moduleDetails = array('TPLFile' => false, 'TPLFetchName' => '', 'PHPFile' => false, 'PHPClassName' => '', 'DBContent' => false);
      $tplOutput = ''; // general setting which will change accordingly
      $location = $__page->getTemplateDir();
      $tplDBName = $this->rstrtrim($this->rstrtrim($moduleName, '.tpl'), '.php');
      $resourceSmarty = new templateEngine();
      $updTS = true;
      $mtime = time(); // general setting which will change accordingly
      $moduleReturnValues = '';

      $sql = '
         SELECT moduleName, moduleStatus, mcbID, mcbVersion, mcbType,moduleCreatedTS, moduleModifiedTS, mcbCreatedTS, mcbModifiedTS, mcbCodeName, mcbTemplate, mcbContent
            FROM modules
            LEFT JOIN moduleCodeBases
               ON (modules.moduleID = moduleCodeBases.moduleID)
            WHERE modules.moduleSiteID = ' . $_SESSION['domainID'] . ' AND moduleName = "' . $tplDBName . '" AND mcbStatus = 2 AND moduleStatus = 1
            ORDER BY mcbVersion DESC';

      $recs = $__dbC->query($sql);
      if($recs->num_rows > 0){ // found valid enrty in DB
         $sr = $recs->fetch_assoc(); // take the first entry with the highest version number

         if(!is_null($sr['mcbCodeName']) && !empty($sr['mcbCodeName'])){
            $moduleDetails['PHPFile'] = $this->findFile(array(PATH . $_SESSION['domainRoot'] . '/' . MODULE_DIR), $sr['mcbCodeName'] . '.php');
            $slashPos = strrpos($sr['mcbCodeName'], '/');
            if($slashPos === false){
               $moduleDetails['PHPClassName'] = $sr['mcbCodeName'];
            } else {
               $moduleDetails['PHPClassName'] = substr($sr['mcbCodeName'], $slashPos + 1);
            }
         }
         if(!is_null($sr['mcbTemplate']) && !empty($sr['mcbTemplate'])){
            $moduleDetails['TPLFile'] = $this->findFile($location, $sr['mcbTemplate'] . '.tpl');
            $moduleDetails['TPLFetchName'] = $sr['mcbTemplate'] . '.tpl';
         }
         if(!is_null($sr['mcbContent']) && !empty($sr['mcbContent'])){
            $moduleDetails['DBContent'] = true;
         }

         if($moduleDetails['PHPFile']){ // run code if exists
            include_once $moduleDetails['PHPFile'];
            $pageCaller = new $moduleDetails['PHPClassName'];
            if(method_exists($pageCaller, 'main')){
               $moduleReturnValues = call_user_func(array($pageCaller, 'main'), &$resourceSmarty);
               $updTS = false;
               if(!is_null($moduleReturnValues) || $moduleReturnValues !== false){
                  if(is_array($moduleReturnValues)){
                     while(list($key, $val) = each($moduleReturnValues)){
                        $resourceSmarty->assign($key, $val);
                     }
                  }
               }
            }
         }

         if($moduleDetails['TPLFile'] === false){
            if($moduleDetails['DBContent'] === false){
               if(is_string($moduleReturnValues)){
                  $tplOutput = $resourceSmarty->fetch('string:' . $moduleReturnValues);
               } else {
                  //$tplOutput = 'No template output created for: ' . $moduleName;
               }
            } else {
               $tplOutput = $resourceSmarty->fetch('string:' . $sr['mcbContent']);
               if($updTS){
                  $mtime = $this->getUpdTS($sr);
               }
            }
         } else {
            $tplOutput = $resourceSmarty->fetch($moduleDetails['TPLFetchName']); // output from template file
            if($updTS){
               $mtime = filemtime($moduleDetails['TPLFile']);
            }
         }
      } else { // no entry in DB
         //$tplOutput = 'No template found: ' . $moduleName;
      }
   }

   private function rstrtrim($str, $remove){
      $str    = (string)$str;
      $remove = (string)$remove;

      if(empty($remove)){
         return rtrim($str);
      }

      $len = strlen($remove);
      $offset = strlen($str)-$len;
      while($offset > 0 && $offset == strpos($str, $remove, $offset)){
         $str = substr($str, 0, $offset);
         $offset = strlen($str)-$len;
      }

      return rtrim($str);
   }

   private function getUpdTS($moduleDetails = array()){
      if(!is_null($sr['mcbModifiedTS']) && !empty($sr['mcbModifiedTS'])){
         $modtime = strtotime($sr['mcbModifiedTS']);
      } else if(!is_null($sr['moduleModifiedTS']) && !empty($sr['moduleModifiedTS'])){
         $modtime = strtotime($sr['moduleModifiedTS']);;
      } else if(!is_null($sr['mcbCreatedTS']) && !empty($sr['mcbCreatedTS'])){
         $modtime = strtotime($sr['mcbCreatedTS']);;
      } else if(!is_null($sr['moduleCreatedTS']) && !empty($sr['moduleCreatedTS'])){
         $modtime = strtotime($sr['moduleCreatedTS']);;
      } else {
         $modtime = time();
      }
      return $modtime;
   }

   private function findFile($locations, $fileName){
      $locationCnt = count($locations);
      $fileFound = false;
      for($i = 0; $i < $locationCnt; $i++){
         if(file_exists($locations[$i] . $fileName)){
            $fileFound = $locations[$i] . $fileName;
            break;
         }
      }
      return $fileFound;
   }
}
?>


Whereby line 82 does the read of the template file.
Back to top
View user's profile Send private message
U.Tews
Administrator


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

PostPosted: Tue Feb 07, 2012 9:13 pm    Post subject: Reply with quote

Did you check the returned timestamp in $mtime. May be something is wrong there that the template is never recompiled.

Does $smarty->force_compile = true; change any thing?

I will send also a PM.
Back to top
View user's profile Send private message
kk
Smarty Rookie


Joined: 10 Aug 2010
Posts: 22

PostPosted: Tue Feb 07, 2012 9:33 pm    Post subject: Reply with quote

No cached or compiled version on the system.
First try with given code produces this problem.
Back to top
View user's profile Send private message
U.Tews
Administrator


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

PostPosted: Tue Feb 07, 2012 9:43 pm    Post subject: Reply with quote

If you get hmtl output compiled template files must have been produced.....
Back to top
View user's profile Send private message
kk
Smarty Rookie


Joined: 10 Aug 2010
Posts: 22

PostPosted: Tue Feb 07, 2012 9:46 pm    Post subject: Reply with quote

not before the code is running...
Back to top
View user's profile Send private message
U.Tews
Administrator


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

PostPosted: Tue Feb 07, 2012 10:28 pm    Post subject: Reply with quote

You said you get

Quote:
<html>
<head>
<title></title>
</head>
<body>
<p>main</p>
<p>data to test</p>


<p>step1</p>
<p>data to test</p>

<p>step2</p>
<p></p>
</body>
</html>


displayed, so compiled template files must have been produced.
Back to top
View user's profile Send private message
kk
Smarty Rookie


Joined: 10 Aug 2010
Posts: 22

PostPosted: Tue Feb 07, 2012 11:46 pm    Post subject: Reply with quote

Of course I have compiled code after the event.
Before that no cached or compiled code was on the system.
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 -> Smarty 3 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