简体   繁体   中英

Magento conditional block in layout xml

is it impossible or in layout xml file write condition if isset get param x that x.phtml else y.phtml? I see solve this problem in action controller. But I overwrite action in other module. And I think xml is better.

Actualy I have one register form to affiliate program from done module. I want overwrite this action and show 1 from 2 forms. According what is in get param value.

there is a way how you can do this with nearly pure xml:

background:
you can call function from you xml, which will return the value instead of a static value.

So in your case you can do something like this to you xml to archive the desired behavior:

    <reference name="your-block-name">
        <action method="setTemplate">
            <template helper="your_module/getTemplate">
                <param>a_default_template_if_wanted</param>
            </template>
        </action>
    </reference>

and in the Data.php helper of you module the function getTemplate which could look like this:

public function getTemplate($defaultTemplate){
    //check for your get params

    if(Mage::app()->getRequest()->getParam('x')){
        return 'x.phtml';
    }
    return $defaultTemplate;
}

The best way to achieve this is by creating your own module and adding the block in the XML like this:

<block type="mymodule/someblock" name="my_block" as="myBlock" />

So without any template parameter in the XML. Than in your block set the template in the constructor like this:

public function __construct() {
    parent::__construct();
    if($some_value == 'something') {
        $this->setTemplate('mymodule/first-template.phtml');
    } else {
        $this->setTemplate('mymodule/second-template.phtml');
    }
}

And of course you will replace that 'if' statement with whatever 'if' you need.

The only way I know of to do if statements in configs is the use the ifconfig but that is not what you want.

In your case what I would suggest is having a master block with sub blocks then put your conditional statement in the master template and use $this->getChildHtml('child_name') . That way you can put your logic in a template and not pollute your configs (I think there might be a way).

Config:

    <block type="core/template" name="master_block" template="folder/path/master_block.phtml">
        <block type="core/template" name="sub_block_form1" template="folder/path/form1.phtml" />
        <block type="core/template" name="sub_block_form2" template="folder/path/form2.phtml" />
    </block>

In folder/path/master_block.phtml :

    <?php if ($this->someBlockMethod() === 'Something'): ?>
            <?php echo $this->getChildHtml('sub_block_form1'); ?>
    <?php else: ?>
            <?php echo $this->getChildHtml('sub_block_form2'); ?>
    <?php endif; ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM