Jump to content

Hook to Header on Font Module Controller


jonsecu

Recommended Posts

Hi... I am developing a module with a front tpl page. The module has its main php file with its constructor, install, uninstall and a hook to the header that assigns some smarty vars. If I write {$testVar} on the pme-header.tpl "value" appears... so this is good. I wrote this smarty assign just to illustrate what I want to do afterwards.

<?php
if (!defined('_PS_VERSION_'))
  exit;
 
class presentforme extends Module
 {
public function __construct() {...}
public function install() {...}
 public function uninstall() {...}
public function hookDisplayHeader($params)
  {
    global $smarty;
    $smarty->assign('testVar', "value"); 
    $this->context->controller->addCSS($this->_path.'pme.css', 'all');
    $this->context->controller->addJS($this->_path.'pme.js', 'all');
    return $this->display(__FILE__, 'pme-header.tpl');
  }
}

Then I have a front controller php that assigns some of its internal values to a smarty variable like this:

<?php
 
if (!defined('_PS_VERSION_'))
        exit;
include_once(dirname(__FILE__).'/../../tools.php');    
include_once(dirname(__FILE__).'/../../config.php'); 
include_once(dirname(__FILE__).'/../../sdk-php-master/lib/mercadopago.php'); 


class PresentformeDefaultModuleFrontController extends
ModuleFrontController   {
 
    public function initContent() {
 
        global $smarty;
        parent::initContent();
        code...
       // Asigna valores a metaetiquetas
       $this->context->smarty->assign('testValue', "test"); 
     }
}

Then on pmeheader.tpl if I write {$testVar} i get an error:

Notice: Undefined index: testVar...
Notice: Trying to get property of non-object...

 

I understand this is occurs because I am calling the hook to pme-header.tpl from the hookDisplayHeader($params) function on the module's main php.

 

So my question is: is there a way to call a hook to the header from the front controller php so the smarty assign to testVar properly shows the test string (or whatever I choose to assign) "value" to pme-header.tpl? I dont care if I have to create another file different from pme-header.tpl... I just want to pass a php var from the front module controller to the front module view`s header using a smarty var using a hook. Can i do this without a hook?

 

It has to be the header because I want to pass some info for og metatags.

 

Any idea will be really appreciated!

 

Thank you very much.

 

Link to comment
Share on other sites

Hi... I am developing a module with a front tpl page. The module has its main php file with its constructor, install, uninstall and a hook to the header that assigns some smarty vars. If I write {$testVar} on the pme-header.tpl "value" appears... so this is good. I wrote this smarty assign just to illustrate what I want to do afterwards.

<?php
if (!defined('_PS_VERSION_'))
  exit;
 
class presentforme extends Module
 {
public function __construct() {...}
public function install() {...}
 public function uninstall() {...}
public function hookDisplayHeader($params)
  {
    global $smarty;
    $smarty->assign('testVar', "value"); 
    $this->context->controller->addCSS($this->_path.'pme.css', 'all');
    $this->context->controller->addJS($this->_path.'pme.js', 'all');
    return $this->display(__FILE__, 'pme-header.tpl');
  }
}

Then I have a front controller php that assigns some of its internal values to a smarty variable like this:

<?php
 
if (!defined('_PS_VERSION_'))
        exit;
include_once(dirname(__FILE__).'/../../tools.php');    
include_once(dirname(__FILE__).'/../../config.php'); 
include_once(dirname(__FILE__).'/../../sdk-php-master/lib/mercadopago.php'); 


class PresentformeDefaultModuleFrontController extends
ModuleFrontController   {
 
    public function initContent() {
 
        global $smarty;
        parent::initContent();
        code...
       // Asigna valores a metaetiquetas
       $this->context->smarty->assign('testValue', "test"); 
     }
}

Then on pmeheader.tpl if I write {$testVar} i get an error:

Notice: Undefined index: testVar...

Notice: Trying to get property of non-object...

 

I understand this is occurs because I am calling the hook to pme-header.tpl from the hookDisplayHeader($params) function on the module's main php.

 

So my question is: is there a way to call a hook to the header from the front controller php so the smarty assign to testVar properly shows the test string (or whatever I choose to assign) "value" to pme-header.tpl? I dont care if I have to create another file different from pme-header.tpl... I just want to pass a php var from the front module controller to the front module view`s header using a smarty var using a hook. Can i do this without a hook?

 

It has to be the header because I want to pass some info for og metatags.

 

Any idea will be really appreciated!

 

Thank you very much.

 

Any idea please?

Link to comment
Share on other sites

Don't forget about Scope

Your smarty variable {$testValue} will only work on 1 page :

http://domain.com/index.php?fc=module&module=YOUR_MODULE&controller=default&id_lang=ACTIVE_ID_LANG

So if you use {$testValue} in pme-header.tpl, this smarty variable will only work on that page.

To prevent error notice while this smarty variable is undefined, you should write like this

{if isset $testValue}{$testValue}{/if}

If you want {$testValue} working globally on all pages, then you should override FrontController.php classes

And if you want to use this smarty variable on header then you should determine this variable inside public function initHeader()

instead public function initContent()

Link to comment
Share on other sites

  • 2 weeks later...

Don't forget about Scope

Your smarty variable {$testValue} will only work on 1 page :

http://domain.com/index.php?fc=module&module=YOUR_MODULE&controller=default&id_lang=ACTIVE_ID_LANG

So if you use {$testValue} in pme-header.tpl, this smarty variable will only work on that page.

To prevent error notice while this smarty variable is undefined, you should write like this

{if isset $testValue}{$testValue}{/if}

If you want {$testValue} working globally on all pages, then you should override FrontController.php classes

And if you want to use this smarty variable on header then you should determine this variable inside public function initHeader()

instead public function initContent()

 

Hi.. thanks for answering! The $testValue assignment will not be constant, so I cannot define it globally overriding the front controller class, it depends on some functions present at the front module controller:

class PresentformeDefaultModuleFrontController extends ModuleFrontController {}

All I want to do is to assign a php var to a smarty var, and then retrieve that smarty var on a hook to the header, just as I would do on the public function:

hookDisplayHeader($params) {} 

present on the main php of the module.

 

I want to do this because I `d like to generate some metatags for the front controller page located at:

http://domain.com/index.php?fc=module&module=YOUR_MODULE&controller=default&id_lang=ACTIVE_ID_LANG

I generate the contents of the metatags on my module`s front controller with php and assign it to smarty vars, and then use a hook?? or something to be able to retrieve those on the header <head></head> of the html rendered page.

 

Do you have any idea of how to do this? I believe overriding the front controller will not work for this case... am I correct?

 

Thanks again!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...