Output buffering and included templates

87 views
Skip to first unread message

mkn

unread,
Apr 11, 2013, 9:25:38 AM4/11/13
to smarty-d...@googlegroups.com
I had a peculiar problem where I could not clean the output buffer in an application module. I finally traced down the problem to 

{include file=$user_template}

which flushed the output buffer. When I added the contents of the file to the main template instead of including them everything worked well.

I suppose this behaviour is intentional? If so, is there a way to disable the flushing/restarting output buffering when including a template?

Markku

uwe.tews

unread,
Apr 11, 2013, 11:48:35 AM4/11/13
to smarty-d...@googlegroups.com
Each subtemplate has it's own output buffer for it's content. So the output buffer get nested,

I'm not 100% sure what you problem is.

If you wanted to clean all output buffers from with the subtemplate you must call ob_get_level to get the nesting level, call ob_end_clean the coresponding number of times and then call ob_start same number of times to recreate the original nesting level with empty buffers.

mkn

unread,
Apr 11, 2013, 2:30:07 PM4/11/13
to smarty-d...@googlegroups.com
Thanks, Uwe,

I will try that... I have so far never needed the ob nesting stuff or even known anything  about it :) Some background information if you are interested :

I have a "CmsModule" framework that relies heavily on Smarty templates. To make things simple I am using the main template as a "controller" because the number of things to do is normally pretty limited. The URLS are of the type:

etc... and it is very easy to do the routing from the the template:

{if $action eq "products"} {cmsmodule name='products'}{/if}

and parse, run and return the rest of the stuff in the Smarty-loaded and initiated module - the extension and the class stuff does most of that automatically.

I have isolated the most common ajax calls to another template that I load when the url path start with "ajax_"
and that, of course is not a problem - I can ob_clean() and output the data.

Now I had a case where I needed to include another template in the middle of the chain when some parts of the page
have already been rendered and one tiny functionality needed some ajaxian output and cleaning the output buffer
(under certain rare circumstances).

That is where I stumbled upon the problem. An included template that loaded a class that in turn needed to clean the 
output buffer simply to be able to echo "1" or "0" to the calling ajax routine.

So far no problems have arisen, the framework and its predecessors have been used in hundreds of web applications.
Yes, Smarty can be used as a controller  as well :)

Anyhow, thanks for your input, I will learn and test the nesting stuff as soon as possible.

Markku

Reply all
Reply to author
Forward
0 new messages