1

I want to execute some custom PHP code in a static CMS block. I want to do this by having a block of type core/template loading a template containing the PHP code. I have created the block and can do a "Hello, world" echo, however I can't access $this.

The code I use in the static CMS block to include the template with the PHP code is: {{block type="core/template" template="module/template.phtml"}}. core/template has been whitelisted.

Anyone has a clue why I can't use $this? Thanks in advance.

EDIT: This is the template content.

<?php
  $_product = Mage::registry('current_product')->getName();
  $this->helper('yotpo')->showWidget($this, $_product);
?>
<div id="yotpo-placeholder"></div>

EDIT: I ended up fixing this by declaring the block in my themes' local.xml, instead of using a static CMS block.

  • you added code in template.phtml ? can you show your code here ? – Dharmesh Jul 7 '17 at 10:47
  • theoretically you can use $this in templates. It is actually used everywhere in the core. $this references the block instance, in your case Mage_Core_Block_Template. Please add the template content so we can see what's wrong. – Marius Jul 7 '17 at 10:52
  • I've added the template content. – mlippie Jul 7 '17 at 10:54
0

Please replace your code with below

{{block type="core/template" template="module/template.php"}}

to

{{block type="core/template" template="module/template.phtml"}}
  • Thanks, but it was a typo in my question. I was using phtml. Updated my question. – mlippie Jul 7 '17 at 10:40
0

You will need to declare block class inside your block directives such as

{{block type="my/module" template="module/template.phtml"}}

instead of

{{block type="core/template" template="module/template.phtml"}}

In your case, $this is instance of Mage_Core_Block_Template and unable to call any methods from your block class.

Hope it helps.

  • I'm pretty sure it should work with core/template as well. Maurius explains this as well in his comment on my post. – mlippie Jul 7 '17 at 11:17
0

you need to show your static block in Product page, please make below changes

call static block in product page

<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('your-static-block-id')->toHtml(); ?>

Modify your module/template.phtml

<?php
  $_product = Mage::registry('current_product');
  $this->helper('yotpo')->showWidget($this, $_product);
?>
<div id="yotpo-placeholder"></div>
0

I hope this will work for you. Replace template.php with template.phtml

EDIT:

you need change block type core/template to module/model.

  • Thanks, but it was a typo in my question. I was using phtml. Updated my question. – mlippie Jul 7 '17 at 10:49
  • I have edited my answer. – BornCoder Jul 7 '17 at 12:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.