| Thursday 17 September 2009 9:51:32 am 
                                                                 Eezy: with a template operator. two files in extension/myextension/autoloads, plus one in extension/myextension/settings 
$eZTemplateOperatorArray = array(
    array
    (
        'script' => 'extension/fdobase/autoloads/fdobasetemplateoperators.php',
        'class' => 'myTemplateOperators',
        'operator_names' => array( 'http_header' ) )
    );
...
    function namedParameterList() {
        return array(
            'http_header' => array(
                'header' => array(
                    'type' => 'string',
                    'required' => true ),
                'status_code' => array(
                    'type' => 'int',
                    'required' => false,
                    'default' => 0 ) ) );
    function modify( &$tpl, &$operatorName, &$operatorParameters, &$rootNamespace, &$currentNamespace, &$operatorValue, &$namedParameters )
    {
        switch ( $operatorName )
        {
            case 'http_header':
            {
                $header = $namedParameters['header'];
                $status_code = $namedParameters['status_code'];
                if ( $status_code != 0 )
                {
                    header( $header, true, $status_code );
                }
                else
                {
                    header( $header );
                }
                $operatorValue = '';
            } break;
        }
    }
...
And you're done. You can do the same for querying / altering session vars from within templates - very handy at times. Just take care about one thing: if you put this in a view template it will not work, because of the view cache - by default it only works from within the pagelayout and its includes. 
2 workarounds:- disable view cache for the objects that want to set headers (bad)
 - use the persistent_variable to pass the http header from the view template to the pagelayout template, and have the pagelayout template emit the http header (better)
 example of such a pagelayout: 
{def $persistent_var = hash()}
{if is_set($module_result.content_info.persistent_variable)}
    {set $persistent_var = merge($persistent_var, $module_result.content_info.persistent_variable)}
{/if}
{if is_set( $persistent_var.http_redirect )}
  {if is_set( $persistent_var.http_redirect_status )}
    {http_header(concat('Location: ', $persistent_var.http_redirect), $persistent_var.http_redirect_status)}
  {else}
    {http_header(concat('Location: ', $persistent_var.http_redirect))}
  {/if}
{elseif is_set( $persistent_var.http_content_type )}
    {http_header(concat('Content-type: ', $persistent_var.http_content_type))}
    {$module_result.content}
{else}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
...
And voilà, you can now do redirects from node view templates and even output pdf, xml or other content-types! Principal Consultant International BusinessMember of the Community Project Board
 |