Jump to content

module to show two different template file?


Recommended Posts

Hi,

I am doing a small module. In that module I have module name lets say storeinfo.php. Now in that storeinfo.php I have assigned the template(smarty) like this

 

 


public function hookLeftColumn($params) {
    global $cookie, $smarty;
    $value=array();
    $store_query = "SELECT * from "._DB_PREFIX_."storeinfo where status='1' ORDER BY storeinfo_id DESC limit 1";
    $stores=Db::getInstance()->ExecuteS($store_query);
        $smarty->assign('array',$stores);
    return $this->display(__FILE__, 'storeinfo.tpl');
  }

  

  Now this one assigns the template file and I cann see my desired view in left column. Now here I want a link url like show more results

  


<a href="">show more results</a>

  and when someone will click on the link then it should show another template where there will prestashop default header content, footer content, left content, right content and in the middle part the more results should show. So can someone kindly tell me how to do this? Any help and suggestions will be really apprecaible. Thanks

Edited by prestashop_newuser (see edit history)
Link to comment
Share on other sites

you can use in href="" get param like "?example=2"

 

then in php file, in hook function you can use:

 

if (isset($_GET['example'])){

return $this->display(__FILE__, 'storeinfo-example.tpl');

} else {

return $this->display(__FILE__, 'storeinfo.tpl');

}

Link to comment
Share on other sites

you can use in href="" get param like "?example=2"

 

then in php file, in hook function you can use:

 

if (isset($_GET['example'])){

return $this->display(__FILE__, 'storeinfo-example.tpl');

} else {

return $this->display(__FILE__, 'storeinfo.tpl');

}

Then how to make the page from module controller. I mean a href will be linked like ?fc=module&module=storeinfo&controller=Storesinfo?store_id=2 and the new page should be open? Just like in prestashop default store block. When you click on the our stores block it opens the page ouir stores and the link there is like that you can see is /index.php?controller=stores

So how to do this from a module where the details page should be in controller

Link to comment
Share on other sites

i added if condition with this param: $_GET['example']

 

so you have to add &example=1 variable to url

yes I did like that  but it did not worked for me. I have done like this in the php file

public function hookLeftColumn($params) {
    global $cookie, $smarty;
    $value=array();
    $store_query = "SELECT * from "._DB_PREFIX_."storeinfo where status='1' ORDER BY storeinfo_id DESC limit 1";
    $stores=Db::getInstance()->ExecuteS($store_query);
        $smarty->assign('array',$stores);
    return $this->display(__FILE__, 'storeinfo.tpl');
    if(isset($_GET['example'])) {
      return $this->display(__FILE__, 'storeinfo-details.tpl');
    }
  }
  
  and in storeinfo.tpl I have my code like this
 
 <a href="?example=2">Click this!</a>
  but when I am doing click on the link it is redirecting a page and the url is 
  
http://localhost/Prestashop/index.php?example=2
  but I can't see any content of storeinfo-details.tpl
  I want that when someone clicks in link then it should redirect to the page where preastashop default header, footer, left column, right column and in the middle part I want to show my custom content.
Link to comment
Share on other sites

you missed basics in php coding:

return $this->display(__FILE__, 'storeinfo.tpl');
    if(isset($_GET['example'])) {
      return $this->display(__FILE__, 'storeinfo-details.tpl');
    }

you've got return before if statement. all code after return will not be executed.

Link to comment
Share on other sites

you missed basics in php coding:

return $this->display(__FILE__, 'storeinfo.tpl');
    if(isset($_GET['example'])) {
      return $this->display(__FILE__, 'storeinfo-details.tpl');
    }

you've got return before if statement. all code after return will not be executed.

 

 
ohh. Sorry for that mistake. By changing my code like that it worked. So now my final code is like this
 
 if(isset($_GET['example'])) {
      return $this->display(__FILE__, 'storeinfo-details.tpl');
    }
    else {
    return $this->display(__FILE__, 'storeinfo.tpl');
    }
    But here is another issue. As storeinfo.tpl is assigned in hookLeftColumn and the storeinfo-details.tpl should show content in the middle part of the content(not in left or right column). So how to check that? As beause I am checking the condition in leftColumn hook?
    Also storeinfo-details.tpl content should only show my custom content in the middle content part not any other content in the middle part.
Link to comment
Share on other sites

you can use home hook.

But then, your storeinfo-details.tpl will appear only on homepage.

 

you need custom controller,

read this: http://doc.prestashop.com/display/PS15/Creating+a+PrestaShop+module#CreatingaPrestaShopmodule-Embeddingatemplateinthetheme

 

Hi,

Thanks @vekia for the reply. I have gone through the documentation. But still I am not getting that how can I access the page from controller. 

Below I am writing my code which I have followed from the example.

 

Module main file ( mymodule.php ) file

 

<?php


if (!defined('_PS_VERSION_'))
  exit;


class MyModule extends Module {
  public function __construct() {
    $this->name = 'mymodule';
    $this->tab = 'front_office_features';
    $this->version = '1.0';
    $this->author = 'firstname lastname';
    $this->need_instance = 0;


    parent::__construct();


    $this->displayName = $this->l('My Module');
    $this->description = $this->l('My Module For Prestashop.');
    $this->confirmUninstall = $this->l('Are you sure to uninstall My Module?');
    $path = dirname(__FILE__);
    if (strpos(__FILE__, 'Module.php') !== false)
      $path .= '/../modules/'.$this->name;
}
  
  public function install() {
    if (!parent::install() || !$this->registerHook('leftColumn') || !$this->registerHook('rightColumn')
      )
      return false;
      return true;
  }
  
  public function uninstall() {
    if (!parent::uninstall())
      return false;
      return true;
  }
  
  public function getContent() {
    $output = null;
    if (Tools::isSubmit('submit'.$this->name)) {
        $my_module_name = strval(Tools::getValue('MYMODULE_NAME'));
        if (!$my_module_name  || empty($my_module_name) || !Validate::isGenericName($my_module_name))
            $output .= $this->displayError( $this->l('Invalid Configuration value') );
        else {
          Configuration::updateValue('MYMODULE_NAME', $my_module_name);
          $output .= $this->displayConfirmation($this->l('Settings updated'));
        }
    }
    return $output.$this->displayForm();
  }
  
  public function displayForm() {
    // Get default Language
    $default_lang = (int)Configuration::get('PS_LANG_DEFAULT');
     
    // Init Fields form array
    $fields_form[0]['form'] = array(
        'legend' => array(
            'title' => $this->l('Settings'),
        ),
        'input' => array(
            array(
                'type' => 'text',
                'label' => $this->l('Configuration value'),
                'name' => 'MYMODULE_NAME',
                'size' => 20,
                'required' => true
            )
        ),
        'submit' => array(
            'title' => $this->l('Save'),
            'class' => 'button'
        )
    );
     
    $helper = new HelperForm();
     
    // Module, t    oken and currentIndex
    $helper->module = $this;
    $helper->name_controller = $this->name;
    $helper->token = Tools::getAdminTokenLite('AdminModules');
    $helper->currentIndex = AdminController::$currentIndex.'&configure='.$this->name;
     
    // Language
    $helper->default_form_language = $default_lang;
    $helper->allow_employee_form_lang = $default_lang;
     
    // Title and toolbar
    $helper->title = $this->displayName;
    $helper->show_toolbar = true;        // false -> remove toolbar
    $helper->toolbar_scroll = true;      // yes - > Toolbar is always visible on the top of the screen.
    $helper->submit_action = 'submit'.$this->name;
    $helper->toolbar_btn = array(
        'save' =>
        array(
            'desc' => $this->l('Save'),
            'href' => AdminController::$currentIndex.'&configure='.$this->name.'&save'.$this->name.
            '&token='.Tools::getAdminTokenLite('AdminModules'),
        ),
        'back' => array(
            'href' => AdminController::$currentIndex.'&token='.Tools::getAdminTokenLite('AdminModules'),
            'desc' => $this->l('Back to list')
        )
    );
     
    // Load current value
    $helper->fields_value['MYMODULE_NAME'] = Configuration::get('MYMODULE_NAME');
     
    return $helper->generateForm($fields_form);
}
  
  public function hookLeftColumn($params) {
    $this->context->smarty->assign(
        array(
          'my_module_name' => Configuration::get('mymodule'),
          'my_module_link' => $this->context->link->getModuleLink('mymodule', 'display'),
          'my_module_message' => $this->l('This is a simple text message') 
          // Do not forget to enclose your strings in the l() translation method
        )
    );
     
    return $this->display(__FILE__, 'mymodule.tpl');
  }
  
  public function hookDisplayRightColumn($params) {
    return $this->hookLeftColumn($params);
  }
  
  public function hookDisplayHeader() {
    $this->context->controller->addCSS($this->_path.'css/mymodule.css', 'all');
  }  


}




?>

config.xml file

<?xml version="1.0" encoding="UTF-8" ?>
        <module>
            <name>mymodule</name>
            <displayName><![CDATA[My Module]]></displayName>
            <version><![CDATA[1.0]]></version>
            <description><![CDATA[My Module For Prestashop.]]></description>
            <author><![CDATA[firstname lastname]]></author>
            <tab><![CDATA[front_office_features]]></tab>
<confirmUninstall>Are you sure to uninstall My Module?</confirmUninstall>
            <is_configurable>1</is_configurable>
            <need_instance>0</need_instance>
<limited_countries></limited_countries>
        </module>

Module's view file

<li>
  <a href="{$base_dir}modules/mymodule/mymodule_page.php" title="Click this link">Click me!</a>
</li>
<!-- Block mymodule -->
<div id="mymodule_block_left" class="block">
  <h4>{l s='Welcome!' mod='mymodule'}</h4>
  <div class="block_content">
    <p>Hello,
       {if isset($my_module_name) && $my_module_name}
           {$my_module_name}
       {else}
           World
       {/if}
       !       
    </p>   
    <ul>
      <li><a href="{$my_module_link}" title="Click this link">Click me!</a></li>
    </ul>
  </div>
</div>
<!-- /Block mymodule -->   

Now the main part is controller and I have named that controller as display.php as per the docs. I have stored the file inside 

mymodule/controllers/front folder

<?php
class mymoduledisplayModuleFrontController extends ModuleFrontController
{
  public function initContent()
  {
    parent::initContent();
    $this->setTemplate('display.tpl');
  }
}

and in template display.tpl which is inside folder path (mymodule/views/templates/front) I have my simple string like this

Hello welcome to controller file.

But after all when i am doing click on Click me! link it is redirecting to the path

http://localhost/Prestashop/index.php?fc=module&module=mymodule&controller=display

and showing the error in that page like 

 

Oops, something went wrong.

 

Try to refresh this page or feel free to contact us if the problem persists.     

 

 

So can you tell me where is the wrong part I have done and how to fix this error?

Edited by prestashop_newuser (see edit history)
Link to comment
Share on other sites

  • 2 years later...
×
×
  • Create New...