4

I want to call a static block into another static block if the right conditions are met. I believe that my main problem is how to register the template in XML, as I never worked with it before.

First off I have a static block with the code: footer02-links. Here I'm trying to call my template file. I realise that the code below is missing the XML tags.

{{block template="custom/employee-links-template.phtml"}}

Then in the themes template folder in /custom/employee-links-template.phtml I have my conditional statement.

<?php if (Mage::getSingleton('customer/session')->isLoggedIn() && Mage::getSingleton('customer/session')->getCustomerGroupId() == "4") :?>
 <?php echo $this->getChildHtml('employee-links') ?>
<?php endif;?> 

So if the customer is logged in and part of customer group with ID 4, they will be shown the static block with the code: employee-links. In this static block I simply have a link to a otherwise hidden category.

<li><a title="Employee Offers" href="/employee-offers.phtml">Employee Monthly Offers</a></li>

So I guess my problem is how to register the block in the local.xml file, which I really have no idea of how to do. Furthermore there might be a better way to do what I'm trying to achieve?

asked Aug 27, 2014 at 8:24

4 Answers 4

6

First off, the code should be

{{block type="core/template" template="custom/employee-links-template.phtml"}}

No need to edit your local.xml you can call your static block in your custom/employee-links-template.phtml template, see below.

 <?php if (Mage::getSingleton('customer/session')->isLoggedIn() && Mage::getSingleton('customer/session')->getCustomerGroupId() == "4") :?>
 <?php echo $this->getLayout()->createBlock("cms/block")->setBlockId("your-static-block-id")->toHtml();?>
 <?php endif;?> 

Explanation: You cannot add children to blocks that are dynamically created with {{block}}, or access them from the layout XML, so you have to create the inner block directly from the template.

Fabian Schmengler
66.2k25 gold badges191 silver badges422 bronze badges
answered Aug 27, 2014 at 9:16
0

Here is the work around for your problem. You can use this code inside local.xml file.

File :app/design/frontend/<package>/<theme>/layout/local.xml

<layout>
 <default> <!-- Layout handle may be different depends upon in which page you need to show the template -->
 <reference name="content"> <!-- structural block may be different depend upon in which portion of the page you need to show the template -->
 <block type="core/template" name="employee.links.template" template="custom/employee-links-template.phtml">
 <block type="cms/block" name="footer.custom.link" as="footer_custom_link">
 <action method="setBlockId"><block_id>footer02-links</block_id></action>
 </block>
 <block type="cms/block" name="employee.custom.link" as="employee_custom_link">
 <action method="setBlockId"><block_id>employee-links</block_id></action>
 </block>
 </block>
 </reference>
 </default>
</layout>

Here first we defined a block with name employee.links.template in the content part of the page (The block will get included in almost every page, since we have used default layout handle here). Now employee.links.template block holds two static blocks as its child. That's it.

Now we need to define the template file.

File : app/design/frontend/<package>/<theme>/custom/employee-links-template.phtml

<div>
 <?php
 //condition for first static block
 if (//put some condition here if any) :
 ?>
 <div><?php echo $this->getChildHtml('footer_custom_link'); ?></div>
 <?php
 endif;
 //condition for second block
 if (Mage::getSingleton('customer/session')->isLoggedIn() && Mage::getSingleton('customer/session')->getCustomerGroupId() == "4")) :
 ?>
 <div><?php echo $this->getChildHtml('employee_custom_link'); ?></div>
 <?php endif; ?>
</div>

Here we simply put two if conditions for our static blocks. If those condition satisfied, it will show the output in frontend. Otherwise it won't. If you need to just show the static blocks in your template, you can omit if condtions.

Hope that helps

answered Aug 27, 2014 at 9:18
0

Into *.phtml template file:

<?php
$block = Mage::app()->getLayout()->createBlock(
 'Customext_Extname_Block_Contacts_Fields',
 'some_index_name',
 array('template' => 'extname/contacts/fields.phtml')
);
echo $block->toHtml();
?>
Lalit Kaushik
8234 gold badges16 silver badges32 bronze badges
answered Sep 11, 2015 at 22:17
0
 <?php if (Mage::getSingleton('customer/session')->isLoggedIn() && Mage::getSingleton('customer/session')->getCustomerGroupId() == "4") :?>
 <?php echo $this->getLayout()->createBlock("cms/block")->setBlockId("your-static-block-id")->toHtml();?>
 <?php endif;?> 
{{block type="core/template" template="custom/employee-links-template.phtml"}}
Teja Bhagavan Kollepara
3,8275 gold badges33 silver badges69 bronze badges
answered Aug 14, 2017 at 10:36
1
  • 1
    HEY don't post a nonsense answer then edit it within the grace period!! Not cool Commented Aug 14, 2017 at 10:45

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.