Jump to content

Action Hooks Create Smarty Variables


stef_dotcom

Recommended Posts

Hi, I'm trying to replace any php code in my templates to turn off the 'Allow PHP tags' setting to future proof our system but I am really struggling with moving everything to action hooks. Despite having read and re-read every bit of documentation I can find and adapting any examples I still can't get it to create a couple of simple variables which can then be used in the template header.tpl. I need to grab the url and work out which version of the site it is (dev, test or live) and add this to the beginning of the page title just to differentiate between the 3.

 

I grabbed the following from one of the examples i found but the return just print 'Array' at the top of the page and nothing seems to be loaded into any variables:

 

function prepend_header($vars) {

 

$extraTemplateVariables = array();

$extraTemplateVariables['mode'] = 'live';

$extraTemplateVariables['titlemode'] = '';

 

$h = "<GETTING URL HERE>";

if ($h) {

if (strlen($h)>5) {

if (substr($h,0,4) == "dev.") {

$extraTemplateVariables['titlemode'] = "DEVELOPMENT - ";

$extraTemplateVariables['mode'] = 'dev';

}

if (substr($h,0,5) == "test.") {

$extraTemplateVariables['titlemode'] = "TEST - ";

$extraTemplateVariables['mode'] = 'test';

}

}

}

return $extraTemplateVariables;

 

 

add_hook("ClientAreaHeaderOutput",1,"prepend_header");

 

Have also tried ClientAreaHeadOutput although I'm not sure what the difference is and it was no better.

 

Any help gratefully received or better ways of doing this, thank you. I have a couple of other places where I need to do something similar so even just a better simple example of how to create a variable in an action hook that can be used in a template would be great, thanks again.

Link to comment
Share on other sites

try using the ClientAreaPage hook instead...

 

<?php

function prepend_header($vars) {

   $mode = 'live';
   $titlemode = '';

   $h = "<GETTING URL HERE>";
       if ($h) {
           if (strlen($h)>5) {
               if (substr($h,0,4) == "dev.") {
                   $titlemode = "DEVELOPMENT - ";
                   $mode = 'dev';
               }
               if (substr($h,0,5) == "test.") {
                   $titlemode = "TEST - ";
                   $mode = 'test';
               }
           }
       }

$extraTemplateVariables = array("mode" => $mode, "titlemode" => $titlemode);
return $extraTemplateVariables;
}

add_hook("ClientAreaPage",1,"prepend_header");

that will create two new Smarty variables, {$mode} and {$titlemode} (names taken from the text in the quotes), and make them available to the client area pages - you could then modify header.tpl to use them.

Link to comment
Share on other sites

also I think that Output hooks are used to add new sections of code, not to modify header code already defined in the template...

 

btw - the link below shows how to use an action hook to modify $pagetitle - that should allow you to do what you want to do, without the need to edit header.tpl

 

http://forum.whmcs.com/showthread.php?101427-Set-title-of-page-using-an-action-hook&p=420418#post420418

 

you'd just need to do your calculation and then concatenate dev/test etc with the pagetitle and output it back to $pagetitle.

 

I think if you had to, you could do this without a hook and just do it in Smarty, but using the hook would be simpler.. if for no other reason that you won't have to modify the templates after every WHMCS update. :)

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated