Head generation with multiple templates

From: Date: Fri, 30 Jan 2004 12:39:35 +0000
Subject: Head generation with multiple templates
Groups: php.smarty.general 
Request: Send a blank email to php-smarty-general+get-10899@lists.php.net to get a copy of this message
I know Smarty is designed to allow any text type data to be templated, and, therefore, handling specifics of HTML shouldn't be done. However, when using multiple templates that may not know what the others contain, handling Javascript and CSS is a difficult task. I propose a few new plugins (one function, one block, one output filter) that allows multiple templates within a page to assign data back into a portion of the page that's already been outputted through the use of an output filter. It is my suggestion that these plugins be included in Smarty. function.resourceoutput.php:<<< <?php function smarty_function_resourceoutput($params,&$smarty) { if(!isset($smarty->resource['token'])) {
    $x = time();
    $smarty->resource['token'] = '@@@@' . $x . md5($x) . $x . '@@@@';
} return $smarty->resource['token'] . $params['name'] . $smarty->resource['token']; } ?>
block.resource.php:<<< <?php function smarty_block_resource($params,$content,&$smarty) { $smarty->resource['data'][$params['name']] .= $content; return; } ?>
outputfilter.resource.php:<<< <?php function smarty_outputfilter_resource($tpl_output, &$smarty) {
    if(is_array($smarty->resource['data'])) {
        foreach(array_keys($smarty->resource['data']) as $resource) {
            $rplcval = $smarty->resource['data'][$resource];
            $rplctok = $smarty->resource['token'] . $resource . $smarty->resource['token'];
            $tpl_output = preg_replace('/' . $rplctok . '/', $rplcval, $tpl_output);
        }
    }
    return $tpl_output;
} ?>
Then, to use them, your template might look like this:<<< This stuff must be at the top ---------- Hi I'm at the top. {resourceoutput name="TOP"} --------------------------------------- Here's the middle of my page. But the following stuff should be at the top: {resource name="TOP"} I belong at the top. {/resource} and here's some more stuff. But the following stuff should also be at the top: {resource name="TOP"} I go up top also. {/resource} and some more stuff. And we're done.
And, if the output filter is registered, you end up with the following:<<< This stuff must be at the top ---------- Hi I'm at the top. I belong at the top. I go up top also. --------------------------------------- Here's the middle of my page. But the following stuff should be at the top: and here's some more stuff. But the following stuff should also be at the top: and some more stuff. And we're done.
And that's it. It works. I've taken caution to generate a very safe token that, under no circumstances should ever actually appear in the output rendering. If it is, however, it will also require that a resource of that name be used at some point. If it hasn't, it still won't replace the token. So, I think all in all it's safe. There is one major drawback... overhead. I'm generating an md5() of time() in order to create a unique token. This happens only once per page, so it shouldn't be too bad. Secondly, since the output filter runs every time, the preg_replace() will be called every time. That's an expensive function to use and it will be called once for every defined resource so, we may want to find a faster way to do this. Comments? Suggestions? Corrections? Improvements? Flames? -- Jim

« previous php.smarty.general (#10899) next »