Forums / General / HTTP Header

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

eZ debug

Timing: Jan 18 2025 10:40:14
Script start
Timing: Jan 18 2025 10:40:14
Module start 'content'
Timing: Jan 18 2025 10:40:14
Module end 'content'
Timing: Jan 18 2025 10:40:14
Script end

Main resources:

Total runtime0.2278 sec
Peak memory usage2,048.0000 KB
Database Queries141

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0127 588.6484180.8594
Module start 'content' 0.01270.0135 769.507893.9063
Module end 'content' 0.02630.2014 863.4141521.9609
Script end 0.2277  1,385.3750 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00612.6983200.0003
Check MTime0.00160.6810200.0001
Mysql Total
Database connection0.00160.705510.0016
Mysqli_queries0.158669.62081410.0011
Looping result0.00150.65991390.0000
Template Total0.200988.210.2009
Template load0.00090.413110.0009
Template processing0.200087.791310.2000
Override
Cache load0.00060.278710.0006
Sytem overhead
Fetch class attribute can translate value0.00291.263910.0029
XML
Image XML parsing0.00090.399010.0009
General
dbfile0.00230.9974200.0001
String conversion0.00000.003030.0000
Note: percentages do not add up to 100% because some accumulators overlap

CSS/JS files loaded with "ezjscPacker" during request:

CacheTypePacklevelSourceFiles
CSS0extension/community/design/community/stylesheets/ext/jquery.autocomplete.css
extension/community_design/design/suncana/stylesheets/scrollbars.css
extension/community_design/design/suncana/stylesheets/tabs.css
extension/community_design/design/suncana/stylesheets/roadmap.css
extension/community_design/design/suncana/stylesheets/content.css
extension/community_design/design/suncana/stylesheets/star-rating.css
extension/community_design/design/suncana/stylesheets/syntax_and_custom_tags.css
extension/community_design/design/suncana/stylesheets/buttons.css
extension/community_design/design/suncana/stylesheets/tweetbox.css
extension/community_design/design/suncana/stylesheets/jquery.fancybox-1.3.4.css
extension/bcsmoothgallery/design/standard/stylesheets/magnific-popup.css
extension/sevenx/design/simple/stylesheets/star_rating.css
extension/sevenx/design/simple/stylesheets/libs/fontawesome/css/all.min.css
extension/sevenx/design/simple/stylesheets/main.v02.css
extension/sevenx/design/simple/stylesheets/main.v02.res.css
JS0extension/ezjscore/design/standard/lib/yui/3.17.2/build/yui/yui-min.js
extension/ezjscore/design/standard/javascript/jquery-3.7.0.min.js
extension/community_design/design/suncana/javascript/jquery.ui.core.min.js
extension/community_design/design/suncana/javascript/jquery.ui.widget.min.js
extension/community_design/design/suncana/javascript/jquery.easing.1.3.js
extension/community_design/design/suncana/javascript/jquery.ui.tabs.js
extension/community_design/design/suncana/javascript/jquery.hoverIntent.min.js
extension/community_design/design/suncana/javascript/jquery.popmenu.js
extension/community_design/design/suncana/javascript/jScrollPane.js
extension/community_design/design/suncana/javascript/jquery.mousewheel.js
extension/community_design/design/suncana/javascript/jquery.cycle.all.js
extension/sevenx/design/simple/javascript/jquery.scrollTo.js
extension/community_design/design/suncana/javascript/jquery.cookie.js
extension/community_design/design/suncana/javascript/ezstarrating_jquery.js
extension/community_design/design/suncana/javascript/jquery.initboxes.js
extension/community_design/design/suncana/javascript/app.js
extension/community_design/design/suncana/javascript/twitterwidget.js
extension/community_design/design/suncana/javascript/community.js
extension/community_design/design/suncana/javascript/roadmap.js
extension/community_design/design/suncana/javascript/ez.js
extension/community_design/design/suncana/javascript/ezshareevents.js
extension/sevenx/design/simple/javascript/main.js

Templates used to render the page:

UsageRequested templateTemplateTemplate loadedEditOverride
1pagelayout.tpl<No override>extension/sevenx/design/simple/templates/pagelayout.tplEdit templateOverride template
 Number of times templates used: 1
 Number of unique templates used: 1

Time used to render debug report: 0.0001 secs