7

I have used the {{depend}} template functionality before using email templates and I am wondering if this is possible within CMS Static Blocks, for example:

<a href="{{store url='customer/account'}}">Login/Account</a>

Could be updated to:

<a href="{{store url='customer/account'}}">
  {{depend loggedIn}}Account{{/depend}}
  {{depend loggedOut}}Login{{/depend}}
</a>

Which could be useful to allow customers the option to update these within the CMS Block whilst keeping the dependency functionality.

Obviously this dependency depends on the variables being available to the block itself which the email template achieves using the following (which could be applied to the cms block/model with a rewrite):

public function setTemplateParams(array $templateParams)
{
    return $this->setData('template_params', $templateParams);
}

What I am unsure on is how the templating language used within the block accesses this information, adding {{depend}} tags to a block simply outputs as plain text. I assume there is a compilation step applied before the html is rendered, but a nudge in the right direction as to how to proceed would be of great help.

4

This is not an answer to the question, but it might be a solution to the problem.
I don't think it's a good idea to do all kinds of rewrites. You can achieve the same thing by creating a block and a template and using that in your static block.

Create the block [Namespace]/[Module]/Block/Link.php with this content:

<?php 
class [Namespace]_[Module]_Block_Link extends Mage_Core_Block_Template
{

    public function getAccountUrl()
    {
        if (Mage::getSingleton('customer/session')->isLoggedIn()) {
            return $this->getUrl('customer/account');
        } 
        return $this->getUrl('customer/account/login');
    }
    public function getLabel()
    {
        if (Mage::getSingleton('customer/session')->isLoggedIn()) {
            return Mage::helper('customer')->__('Account');
        } 
        return Mage::helper('customer')->__('Login');
    }
}

then create the template app/design/frontend/base/default/template/[namespace]_[module]/link.phtml

<a href="<?php echo $this->getAccountUrl()?>"><?php echo $this->getLabel()?></a>

Now add this in your cms block

{{block type="[block_alias]/link" template="[namespace]_[module]/link.phtml"}}
  • I have to agree, this is The Right Way™ – Fabian Schmengler Sep 3 '15 at 11:32
  • absolutely right !! – Rajeev K Tomy Sep 3 '15 at 13:25
  • The only issue with The Right Way™ is that this only caters for this specific instance (of the login link), and would require me to write other block/template combinations for any other instances which might require the {{depends}} functionality. – Invoke Sep 3 '15 at 15:43
  • OK so after thinking it over I think this is the most sensible solution (see my comment on the answer from @fschmengler below for some reasons) - I think I will update the getLabel() function so I can pass label info in via the cms using {{ [...] logged_out_label="Login to your account"}} and access them with $this->getBlockParams() allowing an editor to override the labelling from within the cms static block - I feel that is a good compromise. – Invoke Sep 3 '15 at 15:58
  • Yep. This sounds like a good approach. – Marius Sep 3 '15 at 16:09
4

The filter directives are processed in Mage_Cms_Model_Template_Filter which inherits from Mage_Core_Model_Email_Template_Filter, so you have all the functionality from email templates in CMS blocks and pages as well.

But you are right that it all depends on which variables are available. And unfortunately the CMS pages do not assign any variables to the filter, as you can see in Mage_Cms_Block_Block::_toHtml():

$processor = $helper->getBlockTemplateProcessor();
$html = $processor->filter($block->getContent());

Between those lines, $processor->setVariables($block->getData()) would be useful.

How to enable template variables in CMS

Rewrite Mage_Cms_Block_Block and Mage_Cms_Block_Page as described above.

You could then set any additional variables with $block->setData() with another rewrite, but I would suggest to use an observer instead to be more flexible. Observe the event cms_page_render for CMS pages or core_block_abstract_prepare_layout_after for static blocks to add dynamic variables like "logged_in" to all blocks/pages.

Why are you not seeing anything?

The reason that you see {{depend}} as plain text is probably that no template variables have been set. If you look at the dependDirective implementation, you will see that if there are no template variables, the directive is returned unchanged ($construction is a match returned by preg_match_all and $construction[0] the whole subpattern):

public function dependDirective($construction)
{
    if (count($this->_templateVars)==0) {
        // If template preprocessing
        return $construction[0];
    }

    if($this->_getVariable($construction[1], '')=='') {
        return '';
    } else {
        return $construction[2];
    }
}

Should you really do that?

Probably not. Please refer to Marius' answer for a solution to your problem as it is intended by Magento.

In both cases, pay attention to effects of the block cache. You might need to add additional cache tags to the block to distinguish the dynamic content.

  • Thanks for the details @fschmengler - thinking about it some more the issue with implementing this solution means the problem then shifts to the setting of the variables themselves and if it is such a good idea (globally or otherwise) to start appending variables to the block system. I think overall the downsides outweigh the benefits, kudos for writing me a detailed explanation and then telling me not to use it though! >_< – Invoke Sep 3 '15 at 15:56

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.