Load Magento module template - magento

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.

Related

Joomla Terminology: view, layout, task and component development

I'm a developer and I am very confident with MVC pattern and have already developed a lot of webapp from scratch using php framework like symfony or yii.
I'm a little bit confused about joomla mvc and terminology and after googling a lot, read joomla book extensions guide, read on joomla website my doubt are still there.
What is confusing for me is the component mvc structure and how I have to set up "my way of think" about joomla mvc, for doing the things in the joomla way.
In particular I am used to reasoning in terms of controller/action (like in symfony and yii framework)
So the final list of all my webapp url will be
controller1/action1
controller1/action2
controller1/action3
controller2/action1
controller2/action2
Each controller's action will decide what view to render and what layout to use for showing the view.
In particular in these frameworks, the definition of a layout is exactly the mean of a layout. And the view is the "core part" of the page.
So I can have a view with a list of users and I can put this view inside a mobile layout or a desktop layout, or to build a view for mobile and put it only in the mobile layout and so on.
The final result about directory structure in my webapp is something similar to the following:
controllers/
controller1
controller2
controller3
models/
modelForTableA
modelForTableB
views/
controller1/
viewForAction1
viewForAction2
layouts/
mobileLayout.php
desktopLayout.php
and for me is very clear to understand.
So finally my questions are:
how would be the directory structure in joomla?
what are in joomla the definition of view, layout and task?
I need to clarify that I do not need an explaination about MVC works in general, but if I would achieve the same result as before, how I have to organize my code and my work?
Suppose that I want to build a component with the following "url"
userController/addUser
userController/editUser
userController/listUsers
userController/viewUserDetail
anotherController/addOperation
anotherController/editOperation
anotherController/myNonCrudOperation
Thank you very much
Routing in Joomla is slightly different. The SEF URLs are built from menu items, which in turn point to a View/Layout combination.
This turns things around: a controller is not bound to a specific View/Layout.
Let's make an example of the flow with the addUser functionality you mentioned as an example; I'll be referring to these files (but you'll have plenty more):
/controllers/user.php
/models/user.php
/views/useradd/view.html.php
/views/useradd/tmpl/default.php
/views/useradd/tmpl/default.xml
/controller.php
/router.php
As you can see the layouts are inside each view's tmpl folder.
router.php
Let's start from this last file: router.php defines our custom SEF rules so, after Joomla passes the call to our component (usually with the params
?option=com_componentname) we can takeover and interpret the URL as we wish. It is a bit hard to get started with but does provide the most flexibility and power. We don't really need to implement it at all for this simple example: so back to our registration now.
First step: show the "new user" form.
You would typically bind this to a menu item, pointing to the /views/useradd/tmpl/default.php; the /views/useradd/tmpl/default.xml contains the definition of the layout so it's available in the menu manager. Very often there is only one layout per view.
Control is passed to the view /views/useradd/view.html.php , and the view will then load an instance of its own model (automatically chosen based on the view name, you can load other models of course) to gather any initialization data.
The view then renders the layout, and presents it to the user.
The layout's responsibility includes generating a form with an appropriate action (endpoint) and security tokens if appropriate:
<form action="index.php?option=com_mycomponent">
<input type="hidden" task="user.save">
<?php echo JHtml::_('form.token');?>
as you see it doesn't really matter if you want to use <input or params on the url, and you can most often mix them.
Form interaction
For autocompletion the form may need to invoke some backend controller methods, i.e. the method emailAvailable() in the /controllers/user.php
It does not make sense to have such functionality indexed, so we'll invoke the method directly with a non-SEF url:
index.php?option=com_ourcomponent&task=user.emailAvailable
followed by any other parameter. This will work in both get and post.
The controller /controllers/user.php's emailAvailable() method will return a json structure and then invoke exit() as we don't want the CMS to kick in at all. An alternative solution is to add the param &format=json in the call.
{"email":"johndoe#example.com", "available":true}
Saving the data
When the user submits the form, processing is first handled by the controller since a task is specified. (see above task=user.save). Joomla will invoke the method save() in the controller /controllers/user.php.
This time, however, our controller is responsible for returning information to the user. After processing the data, it may choose to re-render the registration form showing an error, or a thank you page. In either case the controller simply sets the redirect, letting Joomla handle the rendering when appropriate.
$this->setRedirect(JRoute::_('index.php?option=com_yourcomponent&view=useradd', false));
More control
Each time a controller task is not specified, the display() method of the main controller is invoked. You can add custom logic there.
Joomla fires several events during a view rendering; these can be intercepted by a system plugin or - if you add in the calls - other kinds of plugins as well. You may even create your own types of plugins. Do not try to instantiate a view manually from a controller, as this may inhibit plugin firing.
Small insight,
1) Directory Structure
controllers/
controller1
controller2
controller3
models/
modelForTableA
modelForTableB
views/
layout1
2) View and layout and task
check this answer
3) More routing techniques with SEF.
Hope it helps.
solved with this. I cannot delete this question because there already exists other answer.
Could any moderator close or delete this? Thank you
https://joomla.stackexchange.com/questions/18774/joomla-terminology-view-layout-task-and-component-development/18799#18799

Magento : how to load product details on homepage

I've been trying to show product details on homepage. After long hours searching on google and stackoverflow, i've come with this solution:
Mage::register('product',Mage::getModel('catalog/product')->load('17'));
$block = Mage::app()->getLayout()
->createBlock('catalog/product_view')
->setData('area','frontend')
->setTemplate('catalog/product/view.phtml');
echo $block->toHtml();
It does render the catalog/product_view block but no its children, i'd like to understand how to render the children too ?
Note: my homepage as the layout "myviewer.phtml" and the code above is in "myviewer.phtml".
You are currently manually creating the product view block and not using the standard layout xml, so you are not getting any of the default child blocks that would be loaded on the product page via the product page layout handle;
catalog_product_view
Viewing this layout handle in the catalog.xml file will show you which blocks are loaded, and importantly which child blocks are added to the block named 'product.info' which usually uses the template section you are trying to render.
If you view the product page template 'catalog/product/view.phtml' that you are rendering, you will see it calls blocks that will not be available such as;
<?php echo $this->getChildHtml('addtocart') ?>
So to fix this you have two options,
Manually initialize and add each of the child blocks to the product block you are creating using the append() method.
Or
Update the controller action that was used to load and render the page to include the product page layout handle. Call the following in the controller,
$update = $this->getLayout()->getUpdate();
$update->addHandle('catalog_product_view');
This will cause the layout to include all of the correct product page child blocks, then in the template just call;
$_product = Mage::getModel('catalog/product')->load('17');
Mage::register('product',$_product);
Mage::register('current_product',$_product);
$block = Mage::app()->getLayout()->getBlock('product.info');
echo $block->toHtml();
Adding the handle could be done in the controller which would need to be overridden using a rewrite in a custom module (there are already lots of articles on this).
Use an event observer for any event fired before the controller action.
Magento creates its block hierarchy based on its layout XML configuration. For example, the catalog/product_view block is used in the catalog.xml layout file (usually found in app/design/frontend/base/default/layout/catalog.xml). This file also defines all of the children blocks of the catalog/product_view block.
Magento decides which layout instructions to carry out depending on the active layout handles. For example, the catalog.xml file references the catalog_product_view layout handle, which corresponds to the catalog module, product controller, view action. Whenever that specific controller action is invoked, that layout is applied.
In your case, the home page is probably a CMS page, which doesn't apply the catalog_product_view layout handle (it typically applies the cms_page handle). As such, the block hierarchy is not defined correctly for you to utilize the catalog/product_view block.
There are many ways of resolving this issue, but first you have to think about exactly what you're trying to do. If you try to use the catalog/product_view block, your home page will look almost exactly like a regular product page, which may or may not be what you want. One way of achieving that is to use the <update/> tag, which takes another layout handle and merges it with the current layout handle.
I recommend reading some more into Magento's layout XML system. Alan Storm has some great resources on the subject, such as this blog post (warning, it's a bit outdated), and his book on Magento's layout system.
you can do it by inserting in CMS > Pages > Home
{{block type="catalog/product_list" name="home.catalog.product.list" alias="products_homepage" LATEST="0" template="catalog/product/list.phtml"}}
In this case you have to change in template/catalog/product/list.phtml
Find this code around in lines 74 , 133 and 180
<?php
$_nameAfterChildren = $this->getChild('name.after')->getSortedChildren();
foreach($_nameAfterChildren as $_nameAfterChildName):
Replace adding a if statement , should be something like this :
<?php
$_nameAfter = $this->getChild('name.after');
// New if here
if($_nameAfter):
$_afterChildren = $this->getChild('name.after')->getSortedChildren();
foreach($_afterChildren as $_afterChildName):

How can integrate html template in codeigniter

I'm new to codeigniter. please tell me how can I integrate or install a html theme/template in codeigniter ? (my css folder path=news/css and application folder path=news/application where news is my main folder)
-thanks.
This is a very simple, very powerful way to do templates in codeigniter that is also very flexible.
http://news.dice.com/2013/02/18/how-to-build-a-to-do-app-with-codeigniter/
ignore the title, most of the lesson is about setting up templates in CI.
Note that i was first exposed to this method from a jeffrey way CI tutorial on net.tutsplus.com
All of them are worth checking out: http://net.tutsplus.com/sessions/codeigniter-from-scratch/
edit -- ok this is good enough addition to post. So in the tutorial, on the template.php page, you will see
$this->load->view($maincontent);
which is cool. but this is much better:
// load your header views
$templatefolder = 'beta/';
if(isset($content01))
$this->load->view($templatefolder.$content01);
if(isset($content02))
$this->load->view($templatefolder.$content02);
if(isset($content03))
$this->load->view($templatefolder.$content03);
// load your footer views
so instead of calling the view "maincontent", i've put in references to $content1, $content2, etc. Because we are doing if isset none of them are required. that way you can easily send more then one view file to the template. Or none at all if you are just showing a message, etc. Also notice that we have $templatefolder - that way you can easily reuse the template file for other site templates, even with the same content.
in your controller (similar to tutorial) it would be
$data['content01'] = 'codeigniterrawks';
$data['content02'] = 'mypetlion';
// beta template
$this->load->view( 'template_beta', $data );
note how easy it is if i want to bring in those same view files into a different template
$data['content01'] = 'codeigniterrawks';
$data['content02'] = 'mypetlion';
// alpha template
$this->load->view( 'template_alpha', $data );
I ran into this exact question about a week ago, this guide really helped me:
http://net.tutsplus.com/tutorials/php/an-introduction-to-views-templating-in-codeigniter/
To do the CSS url's, I added "uri" to my libraries in config/autoload.php (so it looks like this:
$autoload['libraries'] = array('uri', 'database');)
" type="text/css" media="screen" />
the base_url function automatically return whatever the base url of your site is, ie
http://localhost/news/
with the argument appended to the end.
The reason behind this is that if/when you ever need to migrate servers, you just change the base_url in the config file and it automatically updates across all of your templates and sources.
Try this,
I'm using this and it's very powerful.
https://github.com/philsturgeon/codeigniter-template

How do I give my controller function its own template?

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.

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.

Resources