2

How is it possible to test whether a custom block (extends Mage_Core_Block_Template) is rendered in a HTML page or rendered in an email (transactional email template)? I want to do this in the PHP code of the block (in my case in recentproducts.phtml). It should look like

// recentproducts.phtml
if ($this->rendered_in() == 'page')
    echo('Foo.');
elseif ($this->rendered_in() == 'email')
    echo('Bar.');

The block {{block someparam="some data" type="recentproducts/recentproducts" name="recentproducts_recentproducts" template="recentproducts/recentproducts.phtml"}} is used in at CMS -> Static Blocks. I followed the tutorial Custom Block Development in Magento.

Magento Version 1.9.1.1

  • Probably need to be more specific. Do you see the block? Then it's been rendered. – pspahn Jun 29 '15 at 17:27
  • I have updated the question. – Ronny Jun 29 '15 at 17:43
1

Test Which Template is called in HTML

Steps to get Template Path Hints:

Login to Magento Admin panel

  1. Go to System->Configuration. The Template Path and Block name will only appear for current website. So change the Current Configuration Scope to Main Website.
  2. Go to the Advanced section and click on Developers. Expand the Debug tab. Set the Template Path Hints to Yes. Click on Save Configuration button.
  3. Open Magento user panel and see the template path hints. It shows each file where the page element is located in.

This will tell you which block is called. More info.

  • Thanks, good to know. But this was not the question. I’m updating the question to be more specific. – Ronny Jun 30 '15 at 12:34
1

AFAIK there is no out of the box way of checking if you are in a page environment or in an email environment.
But you can specify this from your layout files or from your e-mail configuration.
For example if you have this code in the layout file that displays your block

<block type="type/here" template="template/here.phtml" name="some.name" />

you can transform it to this:

<block type="type/here" template="template/here.phtml" name="some.name">
    <action method="setRenderedIn"><rendered>page</rendered></action>
</block>

then you will be able to access this in the block or template:

$this->getRenderedIn();

This should return page if you used the xml above.

And if you render it in an email template like

{{block type="type/here" template="template/here.phtml"}}

you can transform it into

{{block type="type/here" template="template/here.phtml" rendered_in="email"}}

Again you will be able to access this value in the block or template using

$this->getRenderedIn(); 

In this case it should return email.

0

The tutorial that you pointed out is excellent and I see codes and concepts are clear except for the template declaration part. Envato_Recentproducts extension uses a template recentproducts.phtml which is instructed to create at app/design/frontend/default/default/template/recentproducts/recentproducts.phtml.

But actually, the best practices says, we should place our template in the current active package theme directory. That means, the path will look like this (in general).

app/design/frontend/{package}/{theme}/template/recentproducts/recentproducts.phtml

So in order to make the above specified template should work, it is required that, you active package-theme of your current store should be default/default and most probably this would not be the case.

So put your template in your active package-theme directory or put it in base/default directory. Magento is smart enough to pick your template from base/default package-theme directory, if it couldn't find your template file inside your current package-theme directly. ie the file path that you should will look like this.

app/design/frontend/base/default/template/recentproducts/recentproducts.phtml

Note: Dont forget to clear the cache after all changes made.

  • You are right. I already put recentproducts.phtml in app/design/frontend/base/default/template/recentproducts/. But this was not the question. I’m updating the question to be more specific. – Ronny Jun 30 '15 at 12:25

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.