当前位置: 技术文章>> magento2中的HTML 风格指南以及代码示例

文章标题:magento2中的HTML 风格指南以及代码示例
  • 文章分类: Magento
  • 10774 阅读
系统学习magento二次开发,推荐小册:《Magento中文全栈二次开发 》

本小册面向Magento2以上版本,书代码及示例兼容magento2.0-2.4版本。涵盖了magento前端开发,后端开发,magento2主题,magento2重写,magento2 layout,magento2控制器,magento2 block等相关内容,带领您成为magento开发技术专家。


以下是Magento 2的HTML风格指南:


缩进:使用2个空格进行缩进,而不是制表符。

标签嵌套:避免使用过多的嵌套,以保持代码简洁和易于阅读。

标签属性:使用双引号而不是单引号来定义标签属性。

标签闭合:所有标签必须正确闭合。

ID和类名:使用小写字母和破折号来定义ID和类名,例如“my-class”和“my-id”。

行内样式:避免使用行内样式,尽可能使用外部CSS样式表。

以下是一些Magento 2 HTML代码示例:


布局文件示例

Magento 2的布局文件使用XML格式编写,例如下面是一个显示产品价格的布局文件示例:


<div class="product-info">
    <h1 class="product-name"><?php echo $block->escapeHtml($product->getName()); ?></h1>
    <div class="product-price">
        <?php echo $block->getLayout()->createBlock('Magento\Catalog\Pricing\Render', 'product.price.final')->setTemplate('Magento_Catalog::product/price/final_price.phtml')->setData('price', $finalPrice)->toHtml(); ?>
    </div>
</div>

模板文件示例

Magento 2的模板文件使用PHP语法编写,例如下面是一个显示产品名称和价格的模板文件示例:


<div class="product-info">
    <h1 class="product-name"><?php echo $block->escapeHtml($product->getName()); ?></h1>
    <div class="product-price">
        <?php echo $block->getLayout()->createBlock('Magento\Catalog\Pricing\Render', 'product.price.final')->setTemplate('Magento_Catalog::product/price/final_price.phtml')->setData('price', $finalPrice)->toHtml(); ?>
    </div>
</div

>

HTML元素示例

以下是一个示例HTML元素,用于创建一个简单的表格:


<table class="table">
    <thead>
        <tr>
            <th>Name</th>
            <th>Age</th>
            <th>City</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>John Doe</td>
            <td>25</td>
            <td>New York</td>
        </tr>
        <tr>
            <td>Jane Smith</td>
            <td>30</td>
            <td>Los Angeles</td>
        </tr>
    </tbody>
</table>






推荐文章