HTTP Header

Author Message

Nicolas R

Thursday 17 September 2009 9:02:45 am

Hello,

I would like to add headers items in the HTTP response header from a tpl (for instance expires:now).
Is it possible ? How can I do this ?

Thank you.

Nicolas

Gaetano Giunta

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 Business
Member of the Community Project Board

Powered by eZ Publish™ CMS Open Source Web Content Management. Copyright © 1999-2014 eZ Systems AS (except where otherwise noted). All rights reserved.

eZ debug

Timing: Jan 18 2025 11:38:52
Script start
Timing: Jan 18 2025 11:38:52
Module start 'layout'
Timing: Jan 18 2025 11:38:52
Module start 'content'
Timing: Jan 18 2025 11:38:53
Module end 'content'
Timing: Jan 18 2025 11:38:53
Script end

Main resources:

Total runtime0.6047 sec
Peak memory usage4,096.0000 KB
Database Queries54

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0060 587.7813152.6094
Module start 'layout' 0.00600.0027 740.390639.3984
Module start 'content' 0.00870.5945 779.7891531.1875
Module end 'content' 0.60310.0015 1,310.976612.2031
Script end 0.6046  1,323.1797 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00370.6148160.0002
Check MTime0.00130.2159160.0001
Mysql Total
Database connection0.00100.161210.0010
Mysqli_queries0.551591.2146540.0102
Looping result0.00060.0989520.0000
Template Total0.574094.920.2870
Template load0.00210.353920.0011
Template processing0.571894.565120.2859
Template load and register function0.00020.033310.0002
states
state_id_array0.00100.173310.0010
state_identifier_array0.00090.144620.0004
Override
Cache load0.00180.2963230.0001
Sytem overhead
Fetch class attribute can translate value0.00110.176320.0005
Fetch class attribute name0.00180.298630.0006
XML
Image XML parsing0.00080.125720.0004
class_abstraction
Instantiating content class attribute0.00000.001130.0000
General
dbfile0.00090.1507160.0001
String conversion0.00000.001340.0000
Note: percentages do not add up to 100% because some accumulators overlap

Templates used to render the page:

UsageRequested templateTemplateTemplate loadedEditOverride
1node/view/full.tplfull/forum_topic.tplextension/sevenx/design/simple/override/templates/full/forum_topic.tplEdit templateOverride template
2content/datatype/view/ezxmltext.tpl<No override>extension/community_design/design/suncana/templates/content/datatype/view/ezxmltext.tplEdit templateOverride template
6content/datatype/view/ezxmltags/paragraph.tpl<No override>extension/ezwebin/design/ezwebin/templates/content/datatype/view/ezxmltags/paragraph.tplEdit templateOverride template
2content/datatype/view/ezxmltags/line.tpl<No override>design/standard/templates/content/datatype/view/ezxmltags/line.tplEdit templateOverride template
1content/datatype/view/ezimage.tpl<No override>extension/sevenx/design/simple/templates/content/datatype/view/ezimage.tplEdit templateOverride template
2content/datatype/view/ezxmltags/literal.tpl<No override>extension/community/design/standard/templates/content/datatype/view/ezxmltags/literal.tplEdit templateOverride template
1print_pagelayout.tpl<No override>extension/community/design/community/templates/print_pagelayout.tplEdit templateOverride template
 Number of times templates used: 15
 Number of unique templates used: 7

Time used to render debug report: 0.0001 secs