Magento 模板文件调用 对于每一行

发布于 2024-11-08 12:11:30 字数 3219 浏览 0 评论 0原文

首先我要说的是,我对 Magento 很陌生,但对 PHP 并不陌生。我的问题是为什么 Magento 中模板文件中的每一行都有启动和停止 PHP 调用,这样做是否会影响性能。

这是一个例子:

<ul class="links<?php if($iecheckout):?> <?php echo $iecheckout ?><?php endif;?>"<?php if($this->getName()): ?> id="<?php echo $this->getName() ?>"<?php endif;?>>
<?php foreach($_links as $_link): ?>
    <?php if ($_link instanceof Mage_Core_Block_Abstract):?>
        <?php echo $_link->toHtml() ?>
    <?php else: ?>
        <li<?php if($_link->getIsFirst()||$_link->getIsLast()): ?> class="<?php if($_link->getIsFirst()): ?>first<?php endif; ?><?php if($_link->getIsLast()): ?> last<?php endif; ?>"<?php endif; ?> <?php echo $_link->getLiParams() ?>><?php echo $_link->getBeforeText() ?><a href="<?php echo $_link->getUrl() ?>" title="<?php echo $_link->getTitle() ?>" <?php echo $_link->getAParams() ?>><?php echo $_link->getLabel() ?></a><?php echo $_link->getAfterText() ?></li>
    <?php endif;?>
<?php endforeach; ?>
</ul>

所以在这个例子中,周围的部分有很多开始和结束的地方,不需要被分解。例如,对于最后两行,为什么要在每行调用 put the ?这与系统读取这些文件的方式不同吗?虽然我知道它必须增加文件空间(一切都是最小的,但加起来有 18k 个文件),但是当您启动 PHP 标签并一遍又一遍地停止它们时,PHP 是否需要更长的时间来解释这些文件。 Magento 是一个庞大的系统,我确信任何性能提升都会有所帮助。

不是更有意义吗

<?php foreach($_links as $_link): ?>
    <?php if ($_link instanceof Mage_Core_Block_Abstract):?>
        <?php echo $_link->toHtml() ?>
    <?php else: ?>
        <li<?php if($_link->getIsFirst()||$_link->getIsLast()): ?> class="<?php if($_link->getIsFirst()): ?>first<?php endif; ?><?php if($_link->getIsLast()): ?> last<?php endif; ?>"<?php endif; ?> <?php echo $_link->getLiParams() ?>><?php echo $_link->getBeforeText() ?><a href="<?php echo $_link->getUrl() ?>" title="<?php echo $_link->getTitle() ?>" <?php echo $_link->getAParams() ?>><?php echo $_link->getLabel() ?></a><?php echo $_link->getAfterText() ?></li>
    <?php endif;?>
<?php endforeach; ?>

让块看起来像这样

<?php 
foreach($_links as $_link): 
    if ($_link instanceof Mage_Core_Block_Abstract):
         echo $_link->toHtml() 
    else:
    ?> 
        <li<?php if($_link->getIsFirst()||$_link->getIsLast()): ?> class="<?php if($_link->getIsFirst()): ?>first<?php endif; ?><?php if($_link->getIsLast()): ?> last<?php endif; ?>"<?php endif; ?> <?php echo $_link->getLiParams() ?>><?php echo $_link->getBeforeText() ?><a href="<?php echo $_link->getUrl() ?>" title="<?php echo $_link->getTitle() ?>" <?php echo $_link->getAParams() ?>><?php echo $_link->getLabel() ?></a><?php echo $_link->getAfterText() ?></li>
    <?php 
    endif;
endforeach; ?>

?这有意义吗?这只是为了模仿一些 HTML 类型的布局吗?随着我越来越习惯这个系统,我是否应该在这个庄园中编写模板,即使它看起来更难阅读?

感谢任何人都可以提供的任何信息。

First off I will say that I am new to Magento but not new to PHP. The question I have is why are there start and stop PHP calls for every line in the template files within Magento and is there a performance hit for doing so.

Here is an example:

<ul class="links<?php if($iecheckout):?> <?php echo $iecheckout ?><?php endif;?>"<?php if($this->getName()): ?> id="<?php echo $this->getName() ?>"<?php endif;?>>
<?php foreach($_links as $_link): ?>
    <?php if ($_link instanceof Mage_Core_Block_Abstract):?>
        <?php echo $_link->toHtml() ?>
    <?php else: ?>
        <li<?php if($_link->getIsFirst()||$_link->getIsLast()): ?> class="<?php if($_link->getIsFirst()): ?>first<?php endif; ?><?php if($_link->getIsLast()): ?> last<?php endif; ?>"<?php endif; ?> <?php echo $_link->getLiParams() ?>><?php echo $_link->getBeforeText() ?><a href="<?php echo $_link->getUrl() ?>" title="<?php echo $_link->getTitle() ?>" <?php echo $_link->getAParams() ?>><?php echo $_link->getLabel() ?></a><?php echo $_link->getAfterText() ?></li>
    <?php endif;?>
<?php endforeach; ?>
</ul>

so in this example, there are a lot of start and stops of the around sections that do not need to be broken up. for example with the last two lines, why call put the on each line? Is this apart to how the system reads these files? While I know it has to increase file space (all be it minimally but with 18k files it adds up), does it take PHP longer to interpret these files when you are starting the PHP tags and stopping them over and over again. Magento is a huge system and I am sure any performance increase could be helpful.

would it not make more sense to have the block

<?php foreach($_links as $_link): ?>
    <?php if ($_link instanceof Mage_Core_Block_Abstract):?>
        <?php echo $_link->toHtml() ?>
    <?php else: ?>
        <li<?php if($_link->getIsFirst()||$_link->getIsLast()): ?> class="<?php if($_link->getIsFirst()): ?>first<?php endif; ?><?php if($_link->getIsLast()): ?> last<?php endif; ?>"<?php endif; ?> <?php echo $_link->getLiParams() ?>><?php echo $_link->getBeforeText() ?><a href="<?php echo $_link->getUrl() ?>" title="<?php echo $_link->getTitle() ?>" <?php echo $_link->getAParams() ?>><?php echo $_link->getLabel() ?></a><?php echo $_link->getAfterText() ?></li>
    <?php endif;?>
<?php endforeach; ?>

look like

<?php 
foreach($_links as $_link): 
    if ($_link instanceof Mage_Core_Block_Abstract):
         echo $_link->toHtml() 
    else:
    ?> 
        <li<?php if($_link->getIsFirst()||$_link->getIsLast()): ?> class="<?php if($_link->getIsFirst()): ?>first<?php endif; ?><?php if($_link->getIsLast()): ?> last<?php endif; ?>"<?php endif; ?> <?php echo $_link->getLiParams() ?>><?php echo $_link->getBeforeText() ?><a href="<?php echo $_link->getUrl() ?>" title="<?php echo $_link->getTitle() ?>" <?php echo $_link->getAParams() ?>><?php echo $_link->getLabel() ?></a><?php echo $_link->getAfterText() ?></li>
    <?php 
    endif;
endforeach; ?>

Does this make sense? Is this just to mimic some HTML type layout? As I get more used to this system, should I be writing templates in this manor even though it looks like it is harder to read?

Thanks for any information anyone can give.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

妞丶爷亲个 2024-11-15 12:11:30

我在模板(不是控制器、模型等)中使用以下内容的原因:

<?php if ($condition): ?>
    <?php foreach($items as $item): ?>

    <?php endforeach ?>
<?php endif ?>

而不是:

<?php if ($condition):
    foreach($items as $item): ?>

<?php endforeach;
endif; ?>

纯粹是为了可读性。

至于开始和结束标签对性能的影响,这似乎是您的问题归结为的问题,您可能需要查看 开始/结束标签&性能?

The reason I use the following in templates (not the controllers, models, etc):

<?php if ($condition): ?>
    <?php foreach($items as $item): ?>

    <?php endforeach ?>
<?php endif ?>

Instead of:

<?php if ($condition):
    foreach($items as $item): ?>

<?php endforeach;
endif; ?>

Is purely for readability.

As for performance impact from opening and close tags goes, which appears to be what your question boils down to, you might want to checkout Opening/closing tags & performance?.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文