I am integrate custom template to widget but its doesn't show the template data. below is my code
community/Sample/Productslider/etc/widget.xml
<widgets>
<productslider_bestseller type="productslider/bestseller" translate="product slider option" module="productslider">
<name>Best Seller Product</name>
<description type="desc">Enable the Best Seller Product Widget</description>
<parameters>
<template>
<required>1</required>
<visible>1</visible>
<label>Template</label>
<type>select</type>
<values>
<best-seller translate="label">
<value>productslider/best-seller.phtml</value>
<label>Best Seller</label>
</best-seller>
</values>
</template>
</parameters>
</productslider_bestseller>
</widgets>
community/Sample/Productslider/Block/Bestseller.php
class Sample_Productslider_Block_Bestseller extends Mage_Core_Block_Abstract implements Mage_Widget_Block_Interface
{
protected function _construct()
{
parent::_construct();
}
protected function _toHtml()
{
$pageTitle = '';
$headBlock = $this->getLayout()->getBlock('head');
if ($headBlock) {
$pageTitle = $headBlock->getTitle();
}
$html = "test";
$this->assign('best-seller', $html);
return parent::_toHtml();
}
}
fronted/base/default/template/productslider/best-seller.phtml
echo $html;
It show blank page in front when i include widget in cms page.
Any one can help me to find out the issue in my code.
Thanks
To me your issue consists in extending Mage_Core_Block_Abstract in which _toHtml method return a void string.
Just set your template in your constructor and extends Mage_Core_Block_Template instead of Mage_Core_Block_Abstract
You can assign a template to a block via the constructor (example taken from core file : /app/code/core/Mage/Catalog/Block/Layer/State.php:43)
public function __construct()
{
parent::__construct();
$this->setTemplate('catalog/layer/state.phtml');
}
So you would have to do something like this in your __contruct
$this->setTemplate('productslider/best-seller.phtml');
But what you seems to have trouble with is passing data to this template ?
Then, make is as you would in any other template :
In your block :
public function getSomeExtraHtml(){
return 'test';
}
In the template :
<?php echo $this->getSomeExtraHtml() ?>
You'll have to set the template in your Block Class
protected function _toHtml()
{
$this->setTemplate('my_module/widget.phtml');
return parent::_toHtml();
}
Create a folder under
app/design/frontend/your_theme/default/my_module and add your html file
I have a variable, contaning data that should be present in the entire site. Instead of passing this data to each view of each controller, I was wondering if there is a way to make this data available for every view in the site.
Pd. Storing this data as a session variable / ci session cookie is not an option.
Thanks so much.
Create a MY_Controller.php file and save it inside the application/core folder. In it, something like:
class MY_Controller extends CI_Controller {
public $site_data;
function __construct() {
parent::__construct();
$this->site_data = array('key' => 'value');
}
}
Throughout your controllers, views, $this->site_datais now available.
Note that for this to work, all your other controllers need to extend MY_Controllerinstead of CI_Controller.
You need to extend CI_Controller to create a Base Controller:
https://www.codeigniter.com/user_guide/general/core_classes.html
core/MY_Controller.php
<?php
class MY_Controller extend CI_Controller {
public function __construct() {
parent::__construct();
//get your data
$global_data = array('some_var'=>'some_data');
//Send the data into the current view
//http://ellislab.com/codeigniter/user-guide/libraries/loader.html
$this->load->vars($global_data);
}
}
controllers/welcome.php
class Welcome extend MY_Controller {
public function index() {
$this->load->view('welcome');
}
}
views/welcome.php
var_dump($some_var);
Note: to get this vars in your functions or controllers, you can use $this->load->get_var('some_var')
Set in application/config/autoload.php
$autoload['libraries'] = array('config_loader');
Create application/libraries/Config_loader.php
defined('BASEPATH') OR exit('No direct script access allowed.');
class Config_loader
{
protected $CI;
public function __construct()
{
$this->CI =& get_instance(); //read manual: create libraries
$dataX = array(); // set here all your vars to views
$dataX['titlePage'] = 'my app title';
$dataX['urlAssets'] = base_url().'assets/';
$dataX['urlBootstrap'] = $dataX['urlAssets'].'bootstrap-3.3.5-dist/';
$this->CI->load->vars($dataX);
}
}
on your views
<title><?php echo $titlePage; ?></title>
<!-- Bootstrap core CSS -->
<link href="<?php echo $urlBootstrap; ?>css/bootstrap.min.css" rel="stylesheet">
<!-- Bootstrap theme -->
<link href="<?php echo $urlBootstrap; ?>css/bootstrap-theme.min.css" rel="stylesheet">
If this is not an Variable(value keep changing) then I would suggest to create a constant in the constant.php file under the config directory in the apps directory, if it's an variable keep changing then I would suggest to create a custom controller in the core folder (if not exist, go ahead an create folder "core") under apps folder. Need to do some changes in other controller as mentioned here :
extend your new controller with the "CI_Controller" class. Example
open-php-tag if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class LD_Controller extends CI_Controller {
}
close-php-tag
Here LD_ is my custom keyword, if you want to change you can change it in config.php file under line# 112 as shown here : $config['subclass_prefix'] = 'LD_';
and extend this class in all your controllers as "class Mynewclass extends LD_Controller..
And in LD_controller you've to write the method in which you want to define the variable/array of values & call that array in all over the application as shown here :
defining variable :
var $data = array();
Method to get values from db through the Model class:
function getbooks()
{
$books = $this->mybooks_model->getbooks(); //array of records
$this->data = array('books'=>$books);
}
to call this variable in the views : print_r($this->data['books']);); you will get all the array values... here we've to make sure atleast one "$data" parameter needs to be passed if not no problem you can define this $data param into the view as shown here :
$this->load->view('mybookstore',$data);
then it works absolutely fine,,, love to share... have a fun working friends
you can use $this->load->vars('varname', $data);[ or load data at 1st view only] onse and use in any loaded views after this
Use sessions in your controllers
$this->session->set_userdata('data');
then display them in your view
$this->session->userdata('data');
Or include a page in base view file e.g index.php
include "page.php";
then in page.php,
add $this->session->userdata('data'); to any element or div
then this will show on all your views
I read all answers, but imho the best approch is via hook:
Create hook, let's get new messages for example:
class NewMessages {
public function contact()
{
// Get CI instance CI_Base::get_instance();
$CI = &get_instance(); // <-- this is contoller in the matter of fact
$CI->load->database();
// Is there new messages?
$CI->db->where(array('new' => 1));
$r = $CI->db->count_all_results('utf_contact_requests');
$CI->load->vars(array('new_message' => $r));
}
}
Attach it to some of the flow point, for example on 'post_controller_constructor'. This way, it will be loaded every time any of your controller is instantiated.
$hook['post_controller_constructor'][] = array(
'class' => 'NewMessages',
'function' => 'contact',
'filename' => 'NewMessages.php',
'filepath' => 'hooks',
'params' => array(),
);
Now, we can access to our variable $new_message in every view or template.
As easy as that :)
You could override the view loader with a MY_loader. I use it on a legacy system to add csrf tokens to the page where some of the forms in views don't use the builtin form generator. This way you don't have to retrospectively change all your controllers to call MY_Controller from CI_Controller.
Save the below as application/core/MY_Loader.php
<?php
class MY_Loader extends CI_Loader {
/**
* View Loader
*
* Overides the core view function to add csrf token hash into every page.
*
* #author Tony Dunlop
*
* #param string $view View name
* #param array $vars An associative array of data
* to be extracted for use in the view
* #param bool $return Whether to return the view output
* or leave it to the Output class
* #return object|string
*/
public function view($view, $vars = array(), $return = FALSE)
{
$CI =& get_instance();
$vars['csrf_token'] = $CI->security->get_csrf_hash();
return $this->_ci_load(array('_ci_view' => $view, '_ci_vars' => $this->_ci_prepare_view_vars($vars), '_ci_return' => $return));
}
}
I am calling my block from controller but it doesn't display header buttons.I call my controller function with button click and create block using (
$this->loadLayout();
$this->_addContent($this->getLayout()->createBlock('mymodule/adminhtml_mymodule_edit_tab_addanswer'));
$this->renderLayout();.
While on addanswer page i simply use the class
Mymodule_Block_Adminhtml_module_Edit_Tab_Addanswer extends Mage_Adminhtml_Block_Widget_Form
{
protected function _prepareForm()
{
// code
}
}
It display data like the attach image
I want to add save or add like buttons but don't know from where i can do that.I added buttons from block or from edit.php but i don't know how i can add buttons from controller or in form direct.Is it possible ? Thanks in advance
You maybe missing
class CompanyName_Mymodule_Block_Adminhtml_Addanswer_Edit extends Mage_Adminhtml_Block_Widget_Form_Container
{
public function __construct()
{
parent::__construct();
$this->_objectId = 'id';
$this->_blockGroup = 'addanswer';
$this->_controller = 'adminhtml_addanswer';
$this->_updateButton('save', 'label', Mage::helper('addanswer')->__('Save'));
$this->_updateButton('delete', 'label', Mage::helper('addanswer')->__('Delete'));
}
.....
Take a look # Custom Module with Custom Database Table
how to set payment info.phtml for adminhtml ...
like set for the frontend...something like this
class Ks_Gippayment_Block_Info extends Mage_Payment_Block_Info
{
protected function _construct()
{
parent::_construct();
$this->setTemplate('ks/info.phtml');
}
thanks :D
This code will also do the job for adminhtml. Just make sure the following template exists:
app/design/adminhtml/base/default/template/ks/info.phtml
Is it possible to use the catalog collection pager for the wishlist, and if so how can I implement this into the wishlist?
danny (OP) already self-answered the question.
Quote:
Ok, i've found the solution here but i'll post it here too for a better code highlighting:
Create a new modul and overwrite the wishlist block located in: code/core/Mage/Wishlist/Block/Customer/Wishlist.php
and add the following to your Wishlist.php
class Company_Wishlist_Block_Customer_Wishlist extends Mage_Wishlist_Block_Customer_Wishlist
{
protected function _prepareLayout()
{
parent::_prepareLayout();
$pager = $this->getLayout()
->createBlock('page/html_pager', 'wishlist.customer.pager')
->setCollection($this->getWishlist());
$this->setChild('pager', $pager);
$this->getWishlist()->load();
return $this;
}
public function getPagerHtml()
{
return $this->getChildHtml('pager');
}
}
now add <?php echo $this->getPagerHtml(); ?> to the start and/or end of the view.phtml located in: app/design/frontend/default/your_theme/template/wishlist/view.phtml. that should do the trick.
Note: It's absolutely OK to self-answer your own question. Please just post it as an real answer, but not in a question or comment. Posting as real answer helps to keep the "Unanswered" list more clear (avoids making other people wasting their time).
you do not need to create a new module.just create (with folder) in your local : app\code\local\Mage\Wishlist\Block\Customer\Wishlist.php.and enter the following code on Wishlist.php
<?php class Mage_Wishlist_Block_Customer_Wishlist extends Mage_Wishlist_Block_Abstract {
/**
* Preparing global layout
*
* #return Mage_Wishlist_Block_Customer_Wishlist
*/
protected function _prepareLayout()
{
parent::_prepareLayout();
$pager = $this->getLayout()->createBlock('page/html_pager', 'wishlist.customer.pager');
$pager->setAvailableLimit(array(5=>5,10=>10,20=>20,'all'=>'all'));
$pager->setCollection($this->getWishlist());
$this->setChild('pager', $pager);
$this->getWishlist()->load();
return $this;
}
/**
* Pager HTML
*
* #return HTML
*/
public function getPagerHtml()
{
return $this->getChildHtml('pager');
}
}
After that add following code in /app/design/frontend/base/default/template/wishlist/view.phtml
<?php echo $this->getPagerHtml(); ?>
after title div and after formkey in end of view.phtml
: image example
tested on Magento ver. 1.9.0.1