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

Inline plugin to smarty 3

 
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
bordeux
Smarty Rookie


Joined: 27 Jul 2010
Posts: 10

PostPosted: Sat Dec 25, 2010 1:49 pm    Post subject: Inline plugin to smarty 3 Reply with quote

Code:
<?php
/**
* Smarty inline prefilter plugin
*
* File: prefilter.inline.php<br>
* Type: prefilter<br>
* Name: inline<br>
* Date: Dec 5, 2003<br>
* Purpose: resolve inlcuded templates at compile-time and include them
* Use: <code>$smarty->load_filter('pre','script_name');</code>
* from application.
* @author Messju Mohr <messju@lammfellpuschen.de>
* @version 0.3
* @param string (the template's sourcecode)
* @param Smarty_Compiler (the compiler)
*/


/*
* - tags like {inline file="foo.php"} are resolved
* - {inline file="foo.tpl" assign="foo"} assigns the contents
* instead of displaying them
* - {inline file="foo.tpl" var1=foo var2=bar} defines custom template
* variables to be used in the included tpl.
*/

function smarty_prefilter_inline($source, &$compiler) {
   $ld = $compiler->left_delimiter;
   $rd = $compiler->right_delimiter;

   if (empty($compiler->_inline_depth)) {
      $compiler->_inline_depth = 0;
      /* make compiler visbile to inline_callback-function */
      $GLOBALS['__compiler'] =& $compiler;
   }

   $compiler->_inline_depth++;
   $source = preg_replace_callback('!' . preg_quote($ld, '!') . 'inline(.*)'
   . preg_quote($rd, '!') . '!Us'
   , 'inline_callback', $source);

   if (--$compiler->_inline_depth==0) {
      /* clean up */
      unset($GLOBALS['__compiler']);
   }

   return $source;
}


function inline_callback($match) {
   global $__compiler;

   $ld = $__compiler->left_delimiter;
   $rd = $__compiler->right_delimiter;
   $source_content = '';

   /* get attributes to {inline...} */
   $_attrs = $__compiler->_parse_attrs($match[1]);

   /* "file" */
   if (!isset($_attrs['file'])) {
      $this->syntax_error('[inline] missing file-parameter');
      return false;
   }
   $resource_name = $__compiler->_dequote($_attrs['file']);
   unset($_attrs['file']);


   /* "assign" */
   if (isset($_attrs['assign'])) {
      $assign = $_attrs['assign'];
      unset($_attrs['assign']);

   } else {
      $assign = null;
   }

   /* handle all other attributes as custom-vars */
   if (!empty($_attrs)) {
      $source_content .= $ld.'php'.$rd;
      $source_content .= '$this->assign(array(';
      $_sep = '';
      foreach ($_attrs as $_name=>$_value) {
         $source_content .= "$_sep'$_name'=>$_value";
         $_sep = ',';
      }
      $source_content .= ')); '.$ld.'/php'.$rd;
   }

   /* read tpl-file */
   $params = array('resource_name'=>$resource_name);
   if ($__compiler->_fetch_resource_info($params)) {
      /* recursion */
      $source_content .= smarty_prefilter_inline($params['source_content'], $__compiler);
      /* handle assign */
      if (isset($assign)) {
         $source_content = $ld.'php'.$rd . 'ob_start();' . $ld.'/php'.$rd
         . $source_content
         . $ld.'php'.$rd . '$this->assign(' . $assign . ', ob_get_contents()); ob_end_clean();' . $ld.'/php'.$rd;
      }
   }

   /* return inline-replacement of tpl-file */
   return $source_content;

}


?>


Somebody will help me with the conversion for smarty 3?

This error:
Code:
Fatal error: Uncaught exception 'SmartyException' with message 'invalid template property '_inline_depth'.' in D:\wamp\www\Xvweb\core\libraries\smarty3\sysplugins\smarty_internal_template.php:912 Stack trace: #0 D:\wamp\www\Xvweb\plugins\smarty\prefilter.inline.php(32): Smarty_Internal_Template->__set('_inline_depth', 0) #1 D:\wamp\www\Xvweb\core\libraries\smarty3\sysplugins\smarty_internal_filter_handler.php(57): smarty_prefilter_inline('{$JSBinder[22]=...', Object(Smarty_Internal_Template)) #2 D:\wamp\www\Xvweb\core\libraries\smarty3\sysplugins\smarty_internal_templatecompilerbase.php(70): Smarty_Internal_Filter_Handler::runFilter('pre', '{$JSBinder[22]=...', Object(Smarty_Internal_Template)) #3 D:\wamp\www\Xvweb\core\libraries\smarty3\sysplugins\smarty_internal_template.php(260): Smarty_Internal_TemplateCompilerBase->compileTemplate(Object(Smarty_Internal_Template)) #4 D:\wamp\www\Xvweb\core\libraries\smarty3\sysplugins\smarty_internal_template.php(413): Smarty_Internal_Template->compileTemplateSource() #5 D:\wamp\www\ in D:\wamp\www\Xvweb\core\libraries\smarty3\sysplugins\smarty_internal_template.php on line 912
Back to top
View user's profile Send private message
U.Tews
Administrator


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

PostPosted: Sun Dec 26, 2010 8:11 pm    Post subject: Reply with quote

In Smarty3 you can merge the compiled code of subtemplates into the calling template like this

Code:
{include file='subtemplate.tpl' inline}


See http://www.smarty.net/docs/en/language.function.include.tpl
Back to top
View user's profile Send private message
bordeux
Smarty Rookie


Joined: 27 Jul 2010
Posts: 10

PostPosted: Mon Dec 27, 2010 10:55 pm    Post subject: Reply with quote

Hah! Smarty 3 is the best!!!!!!!!!!!!!!!!!!!!
Thank you!
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