How do I give my controller function its own template? - magento

I've built a widget module that has this basic controller:
class MyModule_OrderForm_HandlersController extends Mage_Core_Controller_Front_Action{
public function handleroneAction(){
// do some stuff
}
}
So this is giving me a page at mydomain.com/orderform/handlers/handlerone which is great, but how do I give that function its own template file.
I've searched Google for hours and not found a straight forward answer, I hope someone here can help me.
Thanks.

If you are looking for how to create a widget i'd check out http://www.magentocommerce.com/knowledge-base/entry/tutorial-creating-a-magento-widget-part-1/.
But for giving your controller action a template I would check out this article from inchoo:
http://inchoo.net/ecommerce/magento/programatically-create-magento-blocks-and-inject-them-into-layout/
As the article says the proper 'magento way' is to create a block file in your module that extends Mage_Core_Block_template and then insert that block with its template file into the page using layout updates.
The method outlined in the inchoo article lets you skip creating a custom block and layout updates and lets you insert your template directly into the content area of that action.
When you call $this->loadLayout() you apply your site's theme to that action. The template you insert would have everything you want to insert into that page's main content area.
The first param is the type of block you're inserting.In this example we are using Mage_Core_Block_Template, the basic block for asigning templates. The second param of the createBlock() function ('my_block_name_here') can be any arbitrary name. The third param is an array of attributes given to the block. In this example the only attribute we're assigning is 'template'. this is how we tell the block which template to use.

Related

How to create a New Page on Prestashop 1.7 Child Theme

I want to create a new page on my prestashop. I dont want to use the CMS to create the page, I need essentially a totally new page.
I have tried duplicating current .tpl's and renaming them - but I can never navigate to them - what is the url to access the new template?
E.g. say my site is www.xyz.com the "my account" template, sits under template/customer/my-account.tpl this my account page is normally accessed at xyz.com/my-account
I want a new but similar page - so I duplicate this template, rename it to my-account-new and change something in it, why can you not access the new template by change the URL to end with my-account-new - I just get a 404.
What am I missing?
Thanks
:)
you can add a new front controller in a custom module :
ModuleName/controllers/front/ControllerName.php
Then your new controller is a class that should be defined like :
Module::getInstanceByName('<ModuleName>');
class <ModuleName><ControllerName>ModuleFrontController extends ModuleFrontController
Then you add the method
public function initContent(){
parent::initContent();
$this->setTemplate('<templateFolder>/<templateName>');
}
You can now navigate to the template by going to index.php?fc=module&module=ModuleName&controller=ControllerName
So in this example replace every ModuleName with the name of your custom module and ControllerName with the name of your controller (for example MyCustomModule and MyCustomController).
The template will be in the your theme folder for example you can add customAddress in themes/ThemeName/templates/customer/customAddress.tpl
in which case the call to setTemplate would become :
$this->setTemplate('customer/customAddress');
I hope this helps.

Load Magento module template

For a Magento module I need to load template file and let it replace the complete page. It is a XML file (but could also be any content whatsoever).
Generally speaking when
MYNS_MYMODULE_controllernameController is triggered and calls fooAction() I need to be able to display a clean site with the content from my template file.
Please let me know where to place the template file and how to tell Magento to load this file as a root template without anything else around.
Edit, to clarify it more:
For http://domain.tld/modulename/controller/action/
Where do I have to place template files and how should I reference them?
You could do it like this
$this->loadLayout()->getLayout()->getBlock('root')->setTemplate('page/1column.phtml');
$this->renderLayout();
or to set a custom response
//$file = myfile or html
$this->getResponse()->setBody($file);
$this->renderLayout();
Ideally your template should sit in /app/design/frontend/mypackage/mytheme/template/mytemplate.phtml
You should read Magento doc and Alan Storm Blog. Alan also wrote a book about Magento Layout: No Frills Magento Layout.
Your url: http://domain.tld/modulename/controller/action/
The modulename_controller_action Handle is created by combining the route name (modulename), Action Controller name (controller), and Action Controller Action Method (action) into a single string. This means each possible method on an Action Controller has a Handle associated with it.
In your layout xml handles this request:
<modulename_controller_action>
......
</modulename_controller_action>
Hope my suggestion is useful for you.

How to make a laravel 5 view composer

I'm still learning Laravel and I'm working on a small project to help me understand better. In the project, I am in need of a global array, so that I may display it or its attributes on every view rendered. sort of on a notification bar, so that each page the user visits, he/she can see the number of notifications (which have been fetched in the background and are stored in the array).
I have done some research, and realized that I have to fetch and compile the array in a view composer I think. But everywhere I go, I cant seem to understand how to make a view composer.
I need to fetch the relevant rows from the database table, and make the resulting array available to each view rendered (I'm thinking attaching it somehow to my layouts/default.blade.php file.). Please help, any and all advice is greatly appreciated:)
You can now inject services on your view
More info here: https://laracasts.com/series/whats-new-in-laravel-5-1/episodes/2
You have to use Sub-Views of laravel blade. I guess your functionality is like a sidebar or like a top bar which will be rendered at every page.
//Your Controller pass data
class YOUR_CONTROLLER extends Controller {
public function index()
{
$data = YOUR_DATA;
return view('YOUR_VIEW_FILE', get_defined_vars());
}
}
//In Your View File
#extends('LAYOUTS_FILE')
#section('YOUR_SECTION')
#include('YOUR_SUB_VIEW_FOR_NOTIFICATION')//You need not pass any data passed all data will be available to this sub view.
#endsection
In your sub view
//Do what ever you want looping logic rendering HTML etc.
//In your layout file just yield or render the section that's it
#yield('YOUR_SECTION')
More explanation can be found Including Sub-Views

Magento layout basics

Im going through a version of No-frills Magento Layout (perhaps 4-5 months old) and am basically stuck right in the beginning.
In Indexcontroller, in the index action, I create a new block object.
public function indexAction()
{
//$this->loadLayout();
$block = new Mage_Core_Block_Template();
$block->setTemplate('helloworld.phtml');
//print_r($block->getTemplateFile());
echo $block->toHtml();
//$this->renderLayout();
I should then create a template file, namely helloworld.phtml and place it in the appropriate directory. I'm used to using a layout file to do this, but I am going through the book and am simply not able to render the file.
I have placed the phtml file in the following locations :
.../app/design/frontend/default/default/template
.../app/design/frontend/base/default/template
which is also the output of
print_r($block->getTemplateFile());
Im obviously missing something here. any chance someone can point it out?
cheers
Based on the comments above, I'd jump directly to to PHP file for the Mage_Core_Block_Template class
app/code/core/Mage/Core/Block/Template.php
Look for the include line and add some debugging that var_dumps whatever file Magento is trying to include OR add some debugging around the conditionals to determine why this isn't getting called.

What is the best practice for displaying multiple views in Codeigniter?

I'm trying to determine the best practice for calling multiple views from the same method in a controller.
Is it preferable in the controller to make one view call, then have that view call all the views it needs, or call all the views you need in sequence in the controller?
Example:
function index(){
//set all data variables
//then send them to the view
$this->load->view($index_view, $data);
}
or
function index(){
//set variables
//then call each view
$this->load->view($header_view, $header_data);
$this->load->view($body_view, $body_data);
$this->load->view($footer_view, $footer_data);
The Codeigniter guide shows both ways, but does not seem to advise to the best practice...is there one?
I didn't like the way of including the header/footer within the view, and I didn't like loading the footer and header each time in every single Controller function.
To fix this, I extended the Controller class with my own view display function.
<?php
// in application/libraries/MY_Controller.php
class MY_Controller extends Controller {
function _displayPage($page, $data = array()) {
$this->view->load('header', $data);
$this->view->load($page, $data);
$this->view->load('footer', $data);
}
}
?>
// in application/controllers/home.php
<?php
class Home extends MY_Controller {
function index() {
$this->_displayPage('home/index', array('title' => 'Home'));
}
}
?>
Not sure if this is CodeIgniter "best practice" but it makes sense to me.
I don't think there is a definitive answer for that. Choose one and stick with it, it's important to be consistent.
Anyway, I'd prefer the second one.
I would say that the controller should only display one view. Then it's up to the view if it wants to show a header, footer, sidebar or whatever. The controller shouldn't have to care, its job is to get data from a model and hand it to a view. Not decide if the view should have a header and a footer.
Agree with Christian Davén: its view / display logic not data or business / logic. essentially its the same as using php includes for snippets like navigation, footer etc. you're just embedding markup.
This is expected behavior. Once variables are set they become available within the controller class and its view files. Sending an array in $this->load->view() is the same as sending an array directly to $this->load->vars() before calling the view file. This simplifies things for most people using multiple views in a controller. If you are using multiple view files in a single controller and want them to each have their own set of variables exclusively, you’ll need to manually clear out the $this->load->_ci_cached_vars array between view calls.
A code comment in the Loader class describes another situation showing why this is the desired default behavior:
You can either set variables using the dedicated $this->load_vars()
function or via the second parameter of this function. We'll merge
the two types and cache them so that views that are embedded within
other views can have access to these variables.

Resources