Magento layout basics - magento

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.

Related

how to customise Drupal's ImageZoom module

I'm using version 7.x-2.0-beta2
Included in the module is imagezoom.api.php
From what I understand; this is the file you apply any customisations to the image zoom functionality.
I assume you have to copy this file into your own site specific templates directory to protect it from being overwritten during any module update.
So the original is in sites/all/modules/imagezoom/imagezoom.api.php
I have taken a copy and moved it to sites/all/themes/[my_site_name]/templates/system/imagezoom.api.php
But the changes I make to this file are not reflected on the site.
(I have tried the changes in both path locations)
The function within this file is simply:
function hook_imagezoom_settings_alter(&$settings, $context) {
// Change the border color to red
$settings['borderColour'] = '#f00';
$settings['responsive'] = TRUE;
}
Can anyone tell me where this file has to be placed in order for the module to pay attention to it ?
You need to create a custom module and then simply create a function in the mymodule.module file which replaces the hook function, like this:
function mymodule_imagezoom_settings_alter(&$settings, $context) {
// Change the border color to red
$settings['borderColour'] = '#f00';
$settings['responsive'] = TRUE;
}
Replace whatever you want from the function above, clear your caches and you should be good to go.
In the link I provided above, look deeper at the "Implementing your own hook" section if you are still having trouble.
Once you have the hook working, you can use the dpm function to view the current values that the page can see. The devel module needs to be on for this to work. In your case, you probably want to do this:
dpm($settings);
and modify the values from there.

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.

Including Joomla! 3 module in custom position on custom component

I've seen alot of examples of how to include modules on a custom joomla! component using JModuleHelper::getModules, however, when trying to include modules in a custom position, the array come back empty. Here's my code in my component's template:
<?php
jimport('joomla.application.module.helper');
$modules = JModuleHelper::getModules('comwhcustomer');
foreach ($modules as $module){
echo JModuleHelper::renderModule($module);
}
?>
You'll notice the position I'm passing the getModules function is comwhcustomer. This code is returning a blank array. If I pass "footer", I do successfully get the module in the footer position.
I'm using an adapted protostar tempalte and I have added the comwhcustomer position to the xml definition file for the template. I've created the module in module manager and in the position filed, I've selected the "comwhcustomer" position that shows up in the dropdown. On the modules manager page, I see the module listed in the position selected (comwhcustomer). I've confirmed in the database table #__modules that the position is saved correctly.
However, the array is still coming back empty on the getModules call. What am I missing?
Joomla version 3.4.1
Local environment
Windows IIS
PHP 5.4.24
MySQL db
PS - As an alternative, I have sucessfully loaded the module with JModuleHelper::getModule() however, none of the attributes I define in the module manager come over, because they're not supposed to. I'd like to use the method mentioned above so I can control the module behavior from the manager rather than attributes in the code, because this position will be used in multiple views of this component.
This is my first time posting to SO. I do appreciate the support :-)
itoctopus, you were exactly right! The module itself was not assigned to any pages, that's the piece I was not taking into consideration! Thank you so much for your help! I set it to display on all pages and it worked!
Of course, it doesn't show up on all the other pages because they do not contain a position called "comwhcustomer" so it is exactly what I was looking for!

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.

Symfony 1.4 and pjax (ajax pushstate)?

symfony: http://www.symfony-project.org
pjax: https://github.com/defunkt/jquery-pjax
Hi all,
I'm trying to use pjax in symfony in order to speed up our website (we will be able to keep header and footer static most of the time, and also avoid reloading lots of css/js and other files).
I have no problem with ajax or symfony, but I want to know if there is a better way:
Is it a good idea to use postExecute to return the html code back right away without sf going to the template at all
If so, can I somehow write this only once for all modules? I imagine that I can do:
mySfActions extends sfActions
moduleActions extends mySfActions
I wonder if there is a better way?
3. Is there a way to get the current layout name (defined in the module's view.yml) within the controller/action?
Question 1: Don't use post-execute like that. If you need to return html from an ajax call in your action then your action should return like this:
return $this->renderText("<p>Your html result.</p>");
This will skip the template call.
Question 2: That is correct. You have written the best way to write a function once and have it available to all module actions.
There is nothing to do.
When calling an action via XmlHttpRequest, symfony automaticaly skip the Layout render, and only return the module render.
You need to put all your "static" assets and html in your layout and that's all.
Thank you all for helping me, all your answers were helpful and pointed me to the right direction. I wanted to vote for both answers but since I can only accept one, I accepted the very first answer.
Anyhow, here is what I did:
First, I extended the sfActions class so I don't have to do add preExecute on every module:
<?php
class mySfActions extends sfActions{
public function preExecute(){
$request = $this->getRequest();
if ($request->getParameter('_pjax')) {
$this->setLayout(false);
}
}
}
Then of course each of my module action class must extend this new class.
Inside my individual template I have something like this:
<?php if($sf_request->getParameter('_pjax')):?>
<script type="text/javascript" src="/js/question_list.js"></script>
<?php endif;?>
This currently seems to work quite well for me, I'm enjoy the incredible loading speed when pushstate is supported, and still able to fallback when it is not (on the dumb IE for example)

Resources