Smarty include with no include file - include

I have a basic page: base.tpl which contains the header and footer of every page on my site. Specific pages (Example: home.tpl) extends ({extends file='../templates/base.tpl'}) the basepage. In the base page I have an include for a statusbar, right underneath the header
({include file=$statusbar}). I include the right statusbar depending if the user is logged in or logged out... But in some occasions, I don't wish to include the statusbar. When the user chose to register, I just want to show the register form, or when a user has tried to loggin and fail I wish to show him a detailed page, with extra info on how to register/retrieve password.
But if I try to include an empty value ($smarty->assign("statusbar","")) I get an error and nothing on the page renders. I was considering adding a blank .tpl and link to it. But I was wondering if that is a "good" solution. Is there a better way to do it?

You can define blocks for you template sections. Then on your extending templates leave blank that blocks
ex: base.tpl
<head>{block name = "head"}<title>Welcome</title>{/block}</head>
{block name = "status_bar"}{include file = $statusbar}{/block}
{block name = "footer"}Some footer{/block}
ex :register.tpl
{extends file = 'base.tpl'}
{block name = 'head'}<title>Register</title>{/block}
{block name = 'status_bar'}{/block}
If you do not to want change the content of any block just dont type

Yes, you just have to add a conditional to see if $statusbar has any value:
{if $statusbar!=''}
{include file=$statusbar}
{/if}

Related

Magento LiteMage Cache - Excluding a block from caching

I have a Magento site with LiteMage cache on.
My website share a common header at every pages. In the header, I have the following code, which checks to see if a customer is logged in to show either "Account" if logged in or "Log in" otherwise.
My problem is that, when a customer is logged in, it still shows "Log in" in some pages but "Account" at some other pages.
I believe this problem is related to my cache, how do I fix this?
How do I set to exclude this particular block from caching in LiteMage, if this is how it should be fixed.
The following code is the relevant part of the codes I have at my header.phtml
<?php
//check the user is logged on or not
if (! Mage::getSingleton('customer/session')->isLoggedIn()){
//if user logged on show the logout link - add this code within anchor tag
echo ''.$this->__('Log In').'';
}
else{
//if user is not logged on yet show the login link - add this code within anchor tag
echo ''.$this->__('Account').'';
}
?>
You need to identify which block contains this code. Like in default magento sample data, the login link is inside "top.links", and that block is hold punched. Try not to use the full header for it, the smaller the scope, the faster it is. Once you know the block name, you can add that in the "customized block name for top links".

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):

Magento - replicate behaviour of email template with variable in admin

I want to have a functionality in Magento where i can add text field to the admin are and allow a user to add a code snippet which will have a placeholder for a variable in it.
At runtime I will grab the code snippet via getStoreConfig and replace the variable/placeholder with the value i need.
I have managed to do everything apart from how to add the variable and replace.
So i do not need help in creating the config in admin etc.
An example would be that the user entered in a config field the following:
<script>
alert('{{amazing_value}}');
</script>
then in my template i would do:
$codeSnippet = Mage::getStoreConfig('path_to_config');
$amazingValue = "This will replace the variable";
// Something here :) (this bit im not sure about where the variable is translated)
echo $codeSnippet
The output from then be:
<script>
alert('This will replace the variable');
</script>
In your "Something here" area, you could try:
$codeSnippet = str_replace("{{amazing_value}}",$amazingValue,$codeSnippet);

How to change load layout in Joomla view?

By default parent::display($tpl); loads components/com_my_component/views/my_component/tmpl/default.php, but in some cases i need to load other php file which is in the same folder near default.php (for example components/com_my_component/views/my_component/tmpl/lol.php). How to do this from view.html.php.
P.S.
Tried load loadTemplate and setLayout methods with no luck.
Solved the problem by myself. Need to use the method setLayout and pay attention to the input syntax
$this->setLayout('dafault:lol');
parent::display($tpl);
By default, joomla looks for the layout keyword in the URL to decide which layout to display. If this variable is empty or not present then the tmpl/default.php layout will be loaded.
By editting your view.html.php file you can set the default layout by using the JView API, e.g. $this->setLayout('lol') will make the URL example.com/yourview equivalent to example.com/yourview?layout=lol.
However, this change alone will result in Joomla overriding it's default behaviour so that the layout request will be ignored. This means that the request example.com/yourview?layout=lmao will also display example.com/yourview = example.com/yourview?layout=lol
You can solve this easily by adding a condition around the setLayout function so that only if the layout keyword is not present then you will set the default layout to lol, e.g.
<?php
# ...
function display($tpl = null) {
# ...
# Edit : Set the default layout to 'lol'
$layout = JRequest::getWord('layout', '');
if (empty($layout)) $this->setLayout("lol");
// Display the view
parent::display($tpl);
}
# ...
I keep coming back to this and I've yet to find a satisfying solution.
What does work, from J1.5 right up to J3.4, for me has always been to set the $tpl variable in view.html.php
If $tpl is empty or "" then tmpl/default.php is displayed by default.
If you change $tpl to a string, e.g. $tpl="stacker" then it will look for and display tmpl/default_stacker.php
I've seen various differing theories on changing it earlier in the MVC so that it doesn't need the default_ pretext. e.g. tmpl/stacker.php
None have worked for me.

Displaying a static block as a popup window in Magento

I'm trying to display a static block in Magento as a popup window, but can't seem to get the code to work.
I've found code in various places on the internet that seems to be fairly close to what I want but I can't get any results. I've used the basic code to return the "top links" to my site so I know that the basics work.
I've created a delpopup.php script in my Magento root folder and put in this code:
<?php
require_once('app/Mage.php');
umask(0);
$layout = Mage::app()
->getLayout();
$layout
->getUpdate()
->addHandle('default')
->load();
$layout
->generateXml()
->generateBlocks();
echo '<p>before</p>';
echo $layout
->createBlock('cms/block')
->setBlockId('delivery-info')
->toHtml();
echo '<p>after</p>';
?>
Unfortunately the code doesn't display the static block. That part of the output is blank.
The idea is that I can place a link in a regular page in Magento and have my delivery into pop up. Like this:
<a title="" onclick="popWin('http://www.mysite.com.au/delpopup.php', 'deliveryinfo', 'width=300,height=300,left=0,top=0,location=no,status=yes,scrollbars=yes,resizable=yes'); return false;" href="#">Delivery Info</a>
Basically I want to be able to display a static block in a popup window that uses my Magento site's theme. Any ideas would be greatly appreciated.
As I thought, the problems lies on incorrect Block Id.
Now that the cms is able to be shown.
So now the question is: how to get the theme work?
Not really sure what do you mean by theme, if what you mean is css that was included in <default> tag, you can use:
Mage::getDesign()->setTheme('your theme');
echo $layout->getBlock('head')->toHtml();
After the xml has been finished generated, it means put that code after:
$layout
->generateXml()
->generateBlocks();
I'd first setup a controller and block(s) to render the type of layout that you want to throw within the popup. So, instead of doing this within a standalone php file, do it within the regular mage framework within a specific controller action.
Then, once you have that working - the question is how to pull it into a popup. I'd go with maybe a nice jquery popup widget that allows you to specify a URL to hit - and then just pass in the one that you prepped for step 1 above.
You may want to look at adminhtml/default/default/template/popup.phtml for inspiration. That's actually a popup template for the admin, not the frontend, but you can see what they've done.
They're pulling in some standard magento blocks including the head block, which should pull in all your CSS and JS, in order to give you the general color scheme / look&feel of your frontend, but without all of the navigation, etc.

Resources