Magento - How to call a phtml template file from the URL - magento

The question pretty much says it all. I have a store locator application. Which dynamically produces a URL like:
http://www.XXXX.com/storelocator/index/viewstore/id/XXX
And I have also the template file in right directory:
package/template/storelocator/view_store.phtml
Also the block in:
app/code/local/COMPANY/Storelocator
When I click on the ID that is generated by the Javascript, it returns a 404 error. So obviously the template file is not loading. But I am not sure what is causing the issue. Can someone give me some idea.

have you assign your template file in layout file if not try to assign your block in layout file.
<storelocator_index_viewstore>
<block type="Storelocator/Storelocator" template="storelocator/view_store.phtml"></block>
</storelocator_index_viewstore>
This way when your action is call magento render this block and call your phtml file

Related

How do I place Magento theme blocks?

I am using two plugins, easycatalogimg and bannerslider.
For sanity purpose, I am using the default Magento theme, I am just trying to learn the template engine.
When I enable easycatalogimg, it appears above the bannerslider on the homepage. I am using the following code to call bannerslider.phtml, within CMS -> Homepage.
{{block type='bannerslider/bannerslider' template='bannerslider/bannerslider.phtml'}}
I would like to call the bannerslider, then easycatalogimg. Problem is, I cannot determine what block type easycatalogimg is. If I could, I would just turn off the homepage display, and then place the block beneath that. When I turn on the setting to make easycatalogimg appear on the homepage, it inserts itself above the rest of the page content.
Are there files I can open to determine what block type easycatalogimg is? Then I could do this code, right under the bannerslider.
{{block type='foo/bar' template='default/default/easycatalogimg.home.phtml'}}
Right now, the easycatalogimg appears on the homepage.
IN AN IDEAL WORLD, the solution here would be that I would edit a page like...
frontend/base/default/template/cms/content_heading.phtml
...within that page, call out the bannerslider.phtml and easycatalogimg/homepage.phtml.
Open the template file of the block you want to know the type. If it's a default Magento template, it will generally have the Block_Class name at the top. If it doesn't, run:
<?php echo get_class($this) ?>
That will give you the Block_Class name which then gets translated into Magento's calling convention, such as:
<?php $block = $this->getLayout()->getBlock('core/template') ?>
Where 'core/template' gets translated into Mage_Core_Block_Template

How do I display "News" on the Home page? (Magento)

I need news articles to be displayed on the front page but the standard way, to put this code {{block type="profile/profile" name="profile" template="profile/news-front.phtml"}} in the editor on backend doesn't work, even though I have created file news-front.phtml in the template/profile. I really-really need help with this. The output in system.log is Not valid template file:frontend/base/default/template/profile/news-front.phtml
Is the block type really going to be profile/profile? If you are just hoping to output the file as PHP/HTML you may want to consider using core/template rather than profile/profile.

Magento get which layout being used on phtml files

Is there a way I could get which layout being used on a certain phtml files?
Here in my case, I want to check what layout being used on catalog/list.phtml, I used that information to make conditional "if" on the product image grid size.
I've tried to google it out. But all the result is just explaining about xml layout things. The closest clue I got is this thread
Magento get layout for given page
which stated the use of this snippet
$left_block = $this->loadLayout()->getLayout()->getBlock('left');
but when I tried it on the phtml files, I got an exception error
UPDATE
joe's answer below has give me some more clue, the exception gone. But the behavior doesn't really what I need. That snippet of code seems to be just check whether the specified block is defined on the XML. What I really need is whether that block exist on a certain page.
In my case, I need to check what layout being used on catalog/product/list.phtml. if it's 3 columns, I'm gonna make the image resized smaller. If it 1 column, I'll make it bigger.
Is there any way I could do that??
If I read the question correctly, then try:
$this->getLayout()->getBlock('root')->getTemplate();
Remove loadLayout():
$left_block = $this->getLayout()->getBlock('left');
By the time you are in the PHTML file, the layout is already loaded.
In PHTML files, $this refers to the Mage_Core_Block_Template class (or a class that extends it). This class doesn't have a loadLayout() method defined, which is why you get an exception; instead, loadLayout() is part of Mage_Core_Controller_Varien_Action.

How can I display an XML feed without any template code in Joomla?

I'm trying to display an XML feed in a custom Joomla 2.5 component's view/layout, but the XML is rendered as a regular layout inside the site's HTML template. How can I display the XML without any template HTML code?
(The trick to include tmpl=component in the URL from this related question doesn't help, there's still some HTML output from the template that ruins the XML.)
I would prefer a solution that only involves code changes in my custom component, like in Symfony when you call the method setLayout(false).
The only solution I have found is to create a file in the current template folder, e.g. "xml.php", and put only this in the file:
<?php
$document = JFactory::getDocument();
$document->setMimeEncoding('text/xml');
echo '<?xml version="1.0" encoding="UTF-8" ?>';
?>
<jdoc:include type="component" />
Then I can append tmpl=xml to the URL.
[edit]
My bad, I made an assumption and you know what that gets you.
Joomla! 1.6->2.5 you can create an alternate output format for an existing view by:
calling the view with a format parameter attached e.g. &format=json
creating a matching view class file e.g. view.json.php that can sit alongside the standard view.html.php file for you view.
The view.yourformat.php file can use your existing controllers and template files in the normal fashion.
Don't forget to add either &tmpl=component or &tmpl=raw to your query string so modules etc don't load as well.
tmpl=raw won't load the html body surrounds or template, only the main component.
[/edit]
From Joomla! 1.6 onward (including 2.5) there is built in support for controller formats ie. you create a controller for the output format you want.
Normally a controller would be named for each view:
/components/mycomp/controllers/myview.php
A XML version of the controller would be name:
/components/mycomp/controllers/myview.xml.php
A JSON version would be:
/components/mycomp/controllers/myview.json.php
To call a particular format version of a controller you simply add &format=theformatyouwant to the URL parameters, so in your case &format=xml
This is discussed in this document from 1.6 days - I used it as a basis for several of our components that have JSON and ics requirements.
This issue drove me crazy a couple of times.
After much frustration, the simplest solution is the one suggested by cppl. In your query
string put the following variables:
format=yourcustomformat
view=viewname
Let say you want json output from a view called json.
Create a veiw folder with the name of your view
json
And a file inside that folder called
view.json.php
Then in your url string you include the following url parameters seperated by the & symbol:
index.php?option=com_mycomponent&format=json&view=json
cppl is correct that this loads a non-html view. However you don't have to put the tmpl parameter in at least in 2.5. If the view name is not view.html.php then 2.5 seems to not include the assigned site template in the response. I think because the view is not veiw.html.php it assumes raw output and does not include the template. I tested this with both an ajax call and a direct url call to the view and in both cases all I got back was the component output. Yeah!
If someone knows where this issue is well documented by the Joomla folks please post!

Magento - conditionals in layout files

Is it possible in Magento to conditionally add blocks into a layout xml file?
Im thinking along the lines of having an admin config option checkbox - if checked then a block needs to be added to the page and vice versa if not checked.
I could think of a way of doing this via code but not the actualy layout file system itself.
The ifconfig paramater can be used to conditionally call an action method
<action method="someBlockMethod" ifconfig="path/to/config"><param1>value</param></action>
The path/to/config path is passed to Mage::getStoreConfigFlag() to return a boolean.
I'd try using this in combination with the insert method
<action method="insert" ifconfig="path/to/config"><param>block_name</param></action>
The block with the name or alias of block_name will need to be already inserted into the layout object by other PHP or XML, so you may need to take additional steps to unset it from its original blocks after inserting it into your new block.
you can try this (i haven't tried it myself):
<action ifconfig='your/extension/active'

Resources