Adding static blocks to specific CMS pages with Magento layouts

本文介绍了一种在Magento中为特定CMS页面添加完全可定制静态块的方法。通过编辑CMS编辑器定义自定义布局,可以轻松创建、修改和删除这些元素,而无需深入编程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

While working on a project, I had to add a unique static block between header and main content of the CMS page on each CMS page. Element had to be fully customizable via Magento admin panel and it had to be easy to create on new CMS pages, easily modifiable and removable. Since that element needed to be placed outside of the main content, it couldn’t have been added using CMS in Magento admin. It had to be added using Magento layouts. Here’s how I achieved that.

As you may know, Magento only offers the following handles for CMS pages:

  • Homepage handle and 404-page handle
  • A handle for all CMS pages

If a CMS block had to be shown on the homepage, we would use the homepage handle in our local.xml file. In the same way, if the same static block had to be shown on all CMS pages, we would have used a handle for all CMS pages.

But what about individual user-created CMS pages?

Frontend developer’s solution

Luckily, Magento enables us to define a custom layout for every CMS page by editing it from CMS editor on the Magento admin panel. That way, layout for a CMS page is modified using defined values for that specific page which are stored in the database.

First, we will create a static block that we want to add to a specific CMS page which we’ll namestatic-block-1 and apply some simple styles to it.

<div class=”element-blue”>
        <h3>Static Block 1</h3>
        <p>This is blue static element 1 </p>
</div>

Next, we will create a new CMS page that will contain the created static block which will be added through Layout Update XML on Designs tab, along with a header and one paragraph. For this example, we’ll use 1 column layout for all CMS pages.

<reference name="root">
        <block type="cms/block" name="myelement">
            <action method="setBlockId"><block_id>static-block-1</block_id></action>
        </block>
</reference>

And finally, we have to update our template files and add a line of code which calls our element.Since we used column layout for all CMS pages and we will modify 1column.phtml template file. If you want to add the elements to CMS pages that use some other template (like 2columns-left), you need to modify those files. 

<div class="page">
        <?php echo $this->getChildHtml('header') ?>
        <?php echo $this->getChildHtml('myelement') ?>
…

For this example, I’ve made one additional static block that is different than the first one and two additional CMS pages – one for the new element I’ve created and another one that won’t have any layout updates and because of that, It won’t contain any of the elements that we’ve created.

Results

CMS page 1 with blue static block:

CMS page 1

HTML code of CMS page 1:

01-code-1

CMS page 2 with green static block:

02-cms-2

HTML code of CMS page 2:

02-code-2

CMS page 3 for which we didn’t set any layout updates in its XML and therefore it doesn’t show any created static blocks.

03-cms-3

HTML code of the CMS page 3:

03-code-3

The good and the bad and the alternative

Although this is a really quick and simple solution that works well, it becomes tedious when you have to go through these same steps for every element that you want to add, even on a project that had 6 or 7 CMS pages.

If you want a more backend-oriented solution for this, Tim Reynolds, Magento Certified Developer, wrote an article on a topic of adding widgets to specific CMS pages in which he used event observers to generate unique handles for each CMS page.

So, how do you like this approach? Feel free to share your solutions in the comment section.


source: http://inchoo.net/magento/adding-static-blocks-to-specific-cms-pages-with-magento-layouts/

To add custom functions to the NX menu, you can use the NXOpen.MenuBar class in NXOpen.NET API. Here is an example code that demonstrates how to add a custom function to the NX menu: ```vb Imports System Imports NXOpen Module Module1 Sub Main() ' Get the NX session Dim theSession As Session = Session.GetSession() ' Get the UI work part Dim theUI As UI = theSession.UI Dim lw As ListingWindow = theSession.ListingWindow ' Get the menu bar Dim menuBar As MenuBar = theUI.MenuBar ' Get the File menu Dim fileMenu As Menu = menuBar.GetMenu("File") ' Add a separator to the File menu fileMenu.AddSeparator() ' Add a custom function to the File menu Dim menuItem As MenuItem = fileMenu.AddMenuItem("Custom Function", AddressOf CustomFunction) ' Show a message box when the custom function is clicked Sub CustomFunction(ByVal item As MenuItem) lw.Open() lw.WriteLine("Custom function is clicked!") lw.Close() End Sub ' Start the NX message loop to display the menu theUI.NXMessageBox.Show("Menu Example", NXMessageBox.DialogType.Information, "Click OK to display the menu") theUI.NXMessageBox.GetMessage() ' Remove the custom function from the menu fileMenu.RemoveMenuItem(menuItem) End Sub End Module ``` In the above code, we first obtain the NX session and UI work part. Then, we get the MenuBar object using `theUI.MenuBar`. Next, we retrieve the desired menu (e.g., "File") using `GetMenu()` method. We can add a separator using `AddSeparator()` method and add a custom function using `AddMenuItem()` method, specifying the function to be called when the menu item is clicked. In the example above, the `CustomFunction` is a sub that will be executed when the custom function menu item is clicked. You can customize the behavior of this function to perform your desired actions. After adding the custom function, we start the NX message loop using `theUI.NXMessageBox.Show()` and `theUI.NXMessageBox.GetMessage()` to display the menu. Finally, we remove the custom function from the menu using `RemoveMenuItem()` method. Please note that above code is just an example, and you may need to adjust it based on your specific requirements and menu structure in NX.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值