简体   繁体   English

如何将 PHP 代码用于 HTML 开始标记内的属性

[英]How to use PHP code for attributes inside HTML opening tag

I know this method enables us to use PHP variables/statements for values of HTML attributes:我知道这种方法使我们能够将 PHP 变量/语句用于 HTML 属性的值:

<button type="button" data-value="<?= $value; ?>">Remove</button>

I'm wondering what the best practice is to add them as attributes:我想知道将它们添加为属性的最佳做法是什么:

  1. This works, but Sublime's syntax highlighter marks it as an error.这有效,但 Sublime 的语法高亮将其标记为错误。 See https://www.shotroom.com/2137/NPu9x for a screenshot.有关屏幕截图,请参阅https://www.shotroom.com/2137/NPu9x

     <?php $attributes = array( 'title="Lorem ipsum"', 'style="background-color: red;"', ); if ($is_special_case) { array_push($attributes, 'data-case="special"'); } $attributes = implode(' ', $attributes); ?> <button type="button" <?= $attributes; ?>>Remove</button>
  2. In researching this, I learned of an alternative, but it seems to have the same problem: it works even though Sublime detects faulty syntax:在研究这个时,我了解到了一个替代方案,但它似乎有同样的问题:即使 Sublime 检测到错误的语法,它也能工作:

     <?php $tmp = ' disabled'; echo <<<HTML <button type="button" ${tmp}>Remove</button> HTML; ?>

    Adding ?> behind the first HTML is supposed to fix this, but I cannot confirm.在第一个 HTML 后面添加?>应该可以解决这个问题,但我无法确认。 See " How do I get HTML syntax highlighting inside PHP strings & heredoc syntax? ".请参阅“ 如何在 PHP 字符串和 heredoc 语法中突出显示 HTML 语法? ”。

  3. Instead, I am currently using this method:相反,我目前正在使用这种方法:

     <?php echo sprintf(` <button type="button" %s>Remove</button> `, ($is_disabled ? 'disabled' : '') ); ?>

    but it adds further layers of indentation, is it so much harder to read, and overall feels clunky.但它增加了更多的缩进层,它更难阅读,整体感觉很笨重。

Is there a better, or agreed upon alternative?是否有更好的或商定的替代方案? Or is the first method the best one, and I just need to modify Sublime somehow, or file a bug report?或者第一种方法是最好的方法,我只需要以某种方式修改 Sublime,或者提交错误报告?

I'm using Sublime 3.2.2 Build 3211.我正在使用 Sublime 3.2.2 Build 3211。

The best way in your examples is the first one, but I suggest you implement a library for your HTML tags (like popular MVC frameworks: Laravel Forms , Codeigniter Form Helper ), for example, you might have a function to create a button with a lot of optional properties, and you should just call this function in your HTML file.示例中最好的方法是第一种,我建议您为 HTML 标签实现一个库(例如流行的 MVC 框架: Laravel FormsCodeigniter Form Helper ),例如,您可能有一个函数来创建一个带有许多可选属性,您应该只在 HTML 文件中调用此函数。 It's really reusable and readable.它真的是可重用和可读的。

You're unnecessarily complicating example #2.您不必要地使示例#2 复杂化。 Try this:尝试这个:

<?php
    $tmp = 'disabled';
    echo <<<HTML
        <button type="button" $tmp>Remove</button>
HTML;
?>

or even better if it's really a single line of HTML:或者甚至更好,如果它真的是一行 HTML:

<?php
    $tmp = 'disabled';
    echo "<button type='button' $tmp>Remove</button>"
?>

(If you simply must use double-quotes in your HTML, use: (如果您必须在 HTML 中使用双引号,请使用:

echo "<button type=\"button\" $tmp>Remove</button>"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM