[prev in list] [next in list] [prev in thread] [next in thread] 

List:       smarty-dev
Subject:    Re: [SMARTY-DEV] Incline inclusion
From:       "Bob Silva" <bob () bravenet ! com>
Date:       2002-07-23 2:58:29
[Download RAW message or body]

Whoops, had a bug which overwrote the _tpl_vars with each child include.
Also noticed that for most people this will create a new smarty object for
each included file.
Didnt notice this at first because our object factory uses a pool to cache
the objects it creates.
The performance difference between using the pool and dynamic object
creation was minute and shouldnt
be an issue.

/*======================================================================*\
    Function: _compile_include_tag
    Purpose:  Compile {include ...} tag
\*======================================================================*/
    function _compile_include_tag($tag_args)
    {
        $attrs = $this->_parse_attrs($tag_args);
        $arg_list = array();

        if (empty($attrs['file'])) {
            $this->_syntax_error("missing 'file' attribute in include tag");
        }

        foreach ($attrs as $arg_name => $arg_value) {
            switch ($arg_name) {
                case 'file':
                    $include_file = $arg_value;
                    break;
                case 'assign':
                    $assign_var = $arg_value;
                    break;
                case 'inline':
                    $inline = true;
                    $_smarty_tpl_vars = $this->_tpl_vars;
                    break;
                case 'cache_id':
                    $cache_id = preg_replace('!\'|"!', '', $arg_value);
                    break;
                case 'compile_id':
                    $compile_id = preg_replace('!\'|"!', '', $arg_value);
                    break;
                default:
                    if (true === $inline) {
                        $this->_tpl_vars[$arg_name] = preg_replace('!\'|"!',
'', $arg_value);
                    } else {
                        if (is_bool($arg_value))
                            $arg_value = $arg_value ? 'true' : 'false';
                        $arg_list[] = "'$arg_name' => $arg_value";
                    }
                    break;
            }
        }

        if (true !== $inline) {
            $output = '<?php ';

            if (isset($assign_var)) {
       $output .= "ob_start();\n";
            }

            $output .=
                "\$_smarty_tpl_vars = \$this->_tpl_vars;\n" .
                "\$this->_smarty_include(".$include_file.",
array(".implode(',', (array)$arg_list)."));\n" .
                "\$this->_tpl_vars = \$_smarty_tpl_vars;\n" .
                "unset(\$_smarty_tpl_vars);\n";

            if (isset($assign_var)) {
       $output .= "<? \$this->assign(" . $assign_var . ",
ob_get_contents()); ob_end_clean();\n?>";
            }

            $output .= ' ?>';
        } else {
            $force_compile = $this->force_compile;
            $this->force_compile = true;
            $output .= $this->fetch(preg_replace('!\'|"!', '',
$include_file), $cache_id, $compile_id);
            $this->force_compile = $force_compile;
            $this->_tpl_vars = $_smarty_tpl_vars;
            unset($_smarty_tpl_vars);
        }

  return $output;

    }



----- Original Message -----
From: "Bob Silva" <bob@bravenet.com>
To: <smarty-dev@lists.php.net>
Sent: Monday, July 22, 2002 6:00 PM
Subject: [SMARTY-DEV] Incline inclusion


Sorry if this post shows up twice...sent the first one from the news
interface and it never showed (yet).

Would like to propose a method to include files inline (instead of
translating them into a php include call).

We are lucky enough at Bravenet to have a site-wide Zend Accelerator
license, so having a whack of
includes isnt a big worry of ours. However our site design is based on
components (some deeply nested)
and some pages were upwards of 10-15 included files just to build the page
with Smarty and our design goals.
To work around this, I added a parameter to the {include} directive to
include the compiled file output inline into
the compilation of the including page. This will have a minimal impact on
the compiling performance, but should
have significant improvements for busier sites.

File: index.tpl

{include file="header.tpl"}
{include file="navbar.tpl"}

Content of index page

{include file="footer.tpl"}

File: navbar.tpl

{include file="search_component.tpl"}
{include file="whatsnew_component.tpl"}
{include file="something_else"}


This is a simple example, but that would require at a minimum, 6 included
files to build the page (plus 2 more for the smarty libraries, plus whatever
other libraries you have in your framework.....adds up fast and makes a
difference in page processing times if your load is high enough).

The biggest drawback to this inline method is that you lose the
auto-compilation if a child changes.
To recognize changes in a child include (doesnt matter how deep), you would
need to modify the
top-level parent to get the children to recompile.


Just an idea I thought might be useful for larger sites looking for every
ounce of performance they can get.

/*======================================================================*\
    Function: _compile_include_tag
    Purpose:  Compile {include ...} tag
\*======================================================================*/
    function _compile_include_tag($tag_args)
    {
        $attrs = $this->_parse_attrs($tag_args);
        $arg_list = array();

        if (empty($attrs['file'])) {
            $this->_syntax_error("missing 'file' attribute in include tag");
        }

        foreach ($attrs as $arg_name => $arg_value) {
            switch ($arg_name) {
                case 'file':
                    $include_file = $arg_value;
                    break;
                case 'assign':
                    $assign_var = $arg_value;
                    break;
                case 'inline':
                    $inline = true;
                    break;
                case 'cache_id':
                    $cache_id = $arg_value;
                    break;
                case 'compile_id':
                    $compile_id = $arg_value;
                    break;
                default:
                    if (true === $inline) {
                        $_smarty_tpl_vars = $this->_tpl_vars;
                        $this->_tpl_vars[$arg_name] = preg_replace('!\'|"!',
'', $arg_value);
                    } else {
                        if (is_bool($arg_value))
                            $arg_value = $arg_value ? 'true' : 'false';
                        $arg_list[] = "'$arg_name' => $arg_value";
                    }
                    break;
            }
        }

        if (true !== $inline) {
            $output = '<?php ';

            if (isset($assign_var)) {
       $output .= "ob_start();\n";
            }

            $output .=
                "\$_smarty_tpl_vars = \$this->_tpl_vars;\n" .
                "\$this->_smarty_include(".$include_file.",
array(".implode(',', (array)$arg_list)."));\n" .
                "\$this->_tpl_vars = \$_smarty_tpl_vars;\n" .
                "unset(\$_smarty_tpl_vars);\n";

            if (isset($assign_var)) {
       $output .= "<? \$this->assign(" . $assign_var . ",
ob_get_contents()); ob_end_clean();\n?>";
            }

            $output .= ' ?>';
        } else {
            $force_compile = $this->force_compile;
            $this->force_compile = true;
            $output .= $this->fetch(preg_replace('!\'|"!', '',
$include_file), $cache_id, $compile_id);
            $this->force_compile = $force_compile;
            $this->_tpl_vars = $_smarty_tpl_vars;
            unset($_smarty_tpl_vars);
        }

  return $output;

    }



-- 
Smarty Development Mailing List (http://smarty.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic