在Magento';的设计文件


What are the technical advantages of using the alternative php syntax in Magento's design files

对于Magento设计文件,似乎非常倾向于使用替代php语法(下面的示例)。技术优势是什么?或者,如果只是一种风格偏好,那么技术吸引力是什么?

<div class="page-title title-buttons">
    <h1><?php echo $this->__('Shopping Cart') ?></h1>
    <?php if(!$this->hasError()): ?>
    <ul class="checkout-types">
    <?php foreach ($this->getMethods('top_methods') as $method): ?>
        <?php if ($methodHtml = $this->getMethodHtml($method)): ?>
        <li><?php echo $methodHtml; ?></li>
        <?php endif; ?>
    <?php endforeach; ?>
    </ul>
    <?php endif; ?>
</div>

没有"优势"或"劣势"。这归结为偏好和可维护性。

记住=>你真的应该把PHP和HTML语法分开。将你的PHP和HTML保存在不同的文件中——这是出于可维护性和许多其他原因(如果你搜索的话,会有很多信息)。你不可能把这两件事完全分开。。。您总是需要将PHP变量插入HTML之类的内容中,但这应该保持在最低限度。

因此,我更喜欢在Wordpress、Codeigner和其他东西的模板文件中使用这种替代语法,因为在我看来,你是在将PHP插入模板文件中的HTML中,所以让我们保持这些PHP块的孤立性。

即对我来说:

<?php
if(something){
    // this is a block of PHP
    // if you want to output html in here 
    // it seems right to do
    echo "<div>Here is some HTML</div>";
}
?>

反之亦然:

<div>
    <!-- this is a template file and is written in html -->
    Hello <?=$username?> <!-- inserting PHP into HTML -->
    <?php if(something): ?> <!-- keep it insular - in its own separate blocks, don't mix the two -->
        <button>A button</button>
    <?php endif; ?>
</div>

同样,没有"技术优势"——它只是更清洁、更好(IMO)。这是混合PHP和HTML太多:

<div><!-- straight html -->
    <?php
        if(something){
            echo "<button>A Button</button>"; <!-- html output via php -->
        }
    ?>
</div>

简直太可怕了:

<div>
    <?php
        if(something){
            ?>
                <button>A Button</button>
                <!-- make your mind up! -->
            <?php
        }
    ?>
</div>

简而言之,它看起来更干净,您可以看到每个右大括号对应的内容(ish)。您仍然可以使用<?php if(foo) { ?><?php } ?>,但看起来不那么干净。

真正的优势只是代码(特别是HTML)变得清晰,语法在代码编辑器中突出显示,这可以帮助您发现拼写错误/bug等。它也更干净,因为您不会到处都有大括号,而且代码都可以很好地缩进。

否则,它将全部显示为PHP字符串,并且编辑器不会对其应用任何样式。

马根托团队可以这样做有很多原因。

如果您喜欢自动格式化,那么IDE将更容易缩进内容,从而使其更具可读性。你也可以用任何你想要的引号来写普通的html,而不是试图把它放在".."中

我不确定你以另一种方式表演时会受到什么样的打击。我会尽力帮你弄清楚的,但不会太多。