Magento: obtener un bloque estático como html en un archivo phtml


Tengo un bloque estático llamado newest_product (con contenido) y me gustaría mostrarlo en un .phtml archivo como html.

He probado este código:

echo $this->getLayout()->createBlock('cms/block')->setBlockId('newest_product')->toHtml(); 

Pero esta nada se está mostrando.

¿Estoy usando el código equivocado?

 40
Author: Tim Penner, 2011-01-12

8 answers

En el layout (app/design/frontend/your_theme/layout/default.xml):

<default>
    <cms_page> <!-- need to be redefined for your needs -->
        <reference name="content">
            <block type="cms/block" name="cms_newest_product" as="cms_newest_product">
                <action method="setBlockId"><block_id>newest_product</block_id></action>
            </block>
        </reference>
    </cms_page>
</default>

En su plantilla phtml:

<?php echo $this->getChildHtml('newest_product'); ?>

No se olvide de la limpieza de caché.

Creo que ayuda.

 50
Author: Max Pronko,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-01-12 17:05:42

Si ha creado un bloque CMS llamado 'block_identifier' desde el panel de administración. A continuación, el siguiente será el código para llamarlos .phtml

<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('block_identifier')->toHtml(); 
?> 
 75
Author: Suman-PHP4U,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-11-27 06:15:46
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('my_static_block_name')->toHtml() ?>

Y utilice este enlace para más información http://www.justwebdevelopment.com/blog/how-to-call-static-block-in-magento /

 21
Author: Ayush Sugandhi,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-04-27 07:03:28

Si desea cargar un cmsblock en su plantilla/blockfile/model, etc. Puedes hacer esto de la siguiente manera. Esto representará cualquier lugar de las variables en el cmsblock

$block  = Mage::getModel('cms/block')
            ->setStoreId(Mage::app()->getStore()->getId())
            ->load('identifier');

$var = array('variable' => 'value', 'other_variable' => 'other value');
/* This will be {{var variable}} and {{var other_variable}} in your CMS block */

$filterModel = Mage::getModel('cms/template_filter');
$filterModel->setVariables($var);

echo $filterModel->filter($block->getContent());
 12
Author: Jeroen,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-10-11 09:09:42

Creo que esto funcionará para usted

$block = Mage::getModel('cms/block')->setStoreId(Mage::app()->getStore()->getId())->load('newest_product');
echo $block->getTitle();
echo $block->getContent();

Funciona, pero ahora las variables en el bloque CMS ya no están analizando: (

 8
Author: Kanak Vaghela,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-11-07 13:02:21

El siguiente código funcionará cuando llame a CMS-Static Block en Magento.

<?php echo 

$this->getLayout()->createBlock('cms/block')->setBlockId('block_identifier')->toHtml();

?>
 5
Author: user3057379,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-12-04 10:12:06

Esto debe funcionar como probado.

<?php
$filter = new Mage_Widget_Model_Template_Filter();
$_widget = $filter->filter('{{widget type="cms/widget_page_link" template="cms/widget/link/link_block.phtml" page_id="2"}}');
echo $_widget;
?>
 2
Author: Farhan Islam,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-11-17 06:41:36

Cuando crea un nuevo bloque CMS llamado block_identifier desde el panel de administración, puede usar el siguiente código para llamarlo desde su .archivo phtml:

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

Luego borra la caché y vuelve a cargar tu navegador.

 2
Author: Joshua Kleveter,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-03-23 02:33:33