I have a page that has some template block included programmatically as follows:
public function indexAction() {
$this->loadLayout();
$block = $this->getLayout()
->createBlock('core/template')
->setTemplate('somefolder/sometemplate.phtml');
$this->getLayout()->getBlock('content')->append($block);
$this->renderLayout();
}
I would like to put inside the sometemplate.phtml, $this->getChildHtml('somechild') to insert another block.
I tried
$box = $this->getLayout()
->createBlock('page/html')
->setTemplate('somefolder/somechild.phtml');
$block->append($box);
But it didn't work. How can I do it?
I solved the problem by using setChild method as follows:
$block->setChild('somealias',$childBlock);
And so I can use
<?php echo $this->getChildHtml('somealias'); ?>
To add up to Ricardo Martins's answer
If you need it in a block directly you can do what product price does
Mage_Catalog_Block_Product:
public function getPriceHtml($product)
{
$this->setTemplate('catalog/product/price.phtml');
$this->setProduct($product);
return $this->toHtml();
}
Related
My master layout here's below and working fine. I just want little bit more passing a default variable with this master layout that I can get in every pages.
class MY_Controller extends CI_Controller {
public $layout;
function __construct() {
parent::__construct();
$this->layout='layout/master';
}
}
I need to pass variable like below:
function __construct() {
parent::__construct();
$data['msg'] = $this->session->flashdata('usermsg');
$this->layout=('layout/master',$data);
}
How do I get this.
If you are loading up the data dynamically from the controller with the help of $this->layout you can send the data like this.
Method 1:
If you are using the general method to load the data to the view you can use this method.
$this->load->view('profile_view', $data);
This will load the profile_view page along with the $data as you passs into it with the help of array()
Method 2:
If you have created a master Layout and you are passing the data from the controller to the Master Layout you need to do like this.
<?php
public function master_layout () {
$this->template['header'] = $this->load->view('include/header', $this->Front_End_data, true);
$this->template['navigation'] = $this->load->view('include/navigation', $this->Front_End_data, true);
$this->template['center'] = $this->load->view($this->middle, $this->Front_End_data, true);
$this->template['footer'] = $this->load->view('include/footer', $this->Front_End_data, true);
$this->load->view('include/index', $this->template);
?>
In this code the below line alone will be loaded dynamically based on the page which you call in the master Layout.
$this->template['center'] = $this->load->view($this->middle, $this->Front_End_data, true);
In order to pass the data to this center layout you can use the funciton like this.
$data['msg'] = 'Success';
$this->template['center'] = $this->load->view ($this->middle = 'pages/view_oage',$data, true);
$this->master_layout();
And in the page you can get the data to be printed using the foreach loop as follows.
foreach($msg as $value)
{
echo $value;
}
I'm adding a small function to the code for our view.phtml to check if the product in question has certain atributes and then build a list from them.
At the top of my file I have
<?php $_helper = $this->helper('catalog/output');?>
<?php $_product = $this->getProduct(); ?>
and elsewhere in my code I quite happily and without issue make use of such calls as:
<?php $_product->getColor();?>
All fine and dandy so far.
Later I declare a class AttributeList and within it's constructor I try to access values of $_product
class AttributeList{ // AttributeList CLASS DEFINITION
public $attributes = array();
public $count;
function __construct(){ //CONSTRUCTOR FOR AttributeList CLASS
$this->itemCount = 0;
if($_product->getColor()){
//DO SOME THINGS
}
}//CONSTRUCTOR ENDS
}// AttributeList CLASS ENDS
This causes my page not to load. If i change the conditions of the if statement to something arbitraraly true like "0 < 1" the code executes perfectly, so I presume the issue is that $_product is not visible from within my class definition.
Can someone explain why this is the case, and how i'm supposed to access the properties of my product from within my class definition?
Which stupidly obvious facet of magento or php am I overlooking here?
It's a very bad practice to put a class inside a view but to answer your question, use Mage::registry('current_product') inside your class:
<?php
class AttributeList {
public $attributes = array();
public $count;
public $product;
function __construct()
{
$this->itemCount = 0;
$this->product = Mage::registry('current_product');
if($this->_product->getColor()){
}
}
}
i have created one news module . it is working fine..
http://domain.com/magento/news
this page is displaying all the news items. with title, content and date.
i want to make view more link for content when user click on view more link it will redirect user to specific newsitem page
http://domain.com/magento/news/newsitem-1
i created another controller newsitemController.php with following code :
public function infoAction(){
$this->loadLayout();
$this->getLayout()->getBlock('content')
->append($this->getLayout()->createBlock('news/newsitem') );
$this->renderLayout();
}
also created block name info.php with below code:
public function _prepareLayout()
{
return parent::_prepareLayout();
}
public function getnewsitem()
{
if(!$this->hasData('news')) {
$this->setData('news', Mage::registry('news'));
}
return $this->getData('news');
}
not getting output..
need help to get output.
In your info.php add the following function to get the url of the news item.
public function getItemUrl($newsItem)
{
return $this->getUrl('*/*/view', array('id' => $newsItem->getId()));
}
In your controller add following function to view the news detail page.
public function viewAction()
{
$model = Mage::getModel('magentostudy_news/news');
$model->load($newsId);
$this->loadLayout();
$itemBlock = $this->getLayout()->getBlock('news.info');
$this->renderLayout();
}
By doing this you can simply access the info page attaching this link on read more like
foreach ($this->getCollection() as $newsItem)
{
//Other Code
Read More..
}
I'm just wondering how I can use/define my own function using the $this->set() method in CakePHP? I want to do something like this...
AppController.php
<?php
function checkSetup() {
if ($this->Auth->user('setup') == 'notcomplete') { return true; }
}
$this->set('isSetup', checkSetup());
?>
And then I will be able to access and call it in my view file:
<?php if ($isSetup): ?>
You haven't setup your profile yet!
<?php endif; ?>
I've tried that, but It clearly doesn't work as I get a massive fatal error. Any ideas/suggestions on how I can do this?
$this->set('isSetup', checkSetup());
That line needs to be inside some function in order to be called. Presumably you want it in the beforFilter of your app controller - something like this:
<?php
App::uses('Controller', 'Controller');
class AppController extends Controller {
function beforeFilter() {
$this->set('isSetup', checkSetup());
}
function checkSetup() {
if ($this->Auth->user('setup') == 'notcomplete') { return true; }
}
}
?>
I failed to load a model from my controller
This is the controller file, article.php:
<?php
class Article extends CI_Controller {
function show($id) { //id'ye gore getir
$this->load->model('articles_model');
$parameter = $this->articles_model>getarticle($id);
$this->my_template->build('article_view', $parameter);
}
}
?>
This is the model file, articles_model.php:
<?php
class Articles_model extends CI_Model {
function __construct()
{
// Call the Model constructor
parent::__construct();
}
function Getarticle($id) {
parent::Model();
$query = $this->db->get_where('articles', array('id' => $id));
$data['articles'] = $query->result();
return $data;
$query->free_result();
}
}
?>
just to add, i even tried to load it from autoloader, still no chance, i assume something is wrong with the model, or the whole system broke.
up: the models loads without problems, if i put echo in __construct function, it works, however, i cannot call the getarticle function. geez
UP: I did it! according to http://grasshopperpebbles.com/codeigniter/codeigniter-call-to-a-member-function-on-a-non-object/
i used
$CI =& get_instance();
and called the function
$CI->articles_model->getarticle($id) and it called the function
It should be,
$CI =&get_instance();
$CI->load->model('articles_model');
$parameter = $CI->articles_model>getarticle($id);
There's a parse error in the following line:
$parameter = $this->articles_model>getarticle($id);
It should be:
$parameter = $this->articles_model->getarticle($id);
Does that fix your problem? If not, what error message are you seeing?
Leif's answer is the right one. Just to add one thing: you don't have to use the long variable name like $this->articles_model over and over, by using the second parameter:
$this->load->model('articles_model','artm');
$parameter = $this->artm->getarticle($id);
Just a little faster to type, and can reduce typos like the one in your sample.