如何在 html 中包含 php if 条件

how to include php if condition in html

我已经编写了 php 代码以使用 mpdf API 将 html 内容转换为 pdf,现在我想在其中包含一个 php if 条件 html 内容 我该怎么做? 这是我的代码:

$cart_body='<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>New Order Placed</title>
</head>
<body>
<table width="550" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="400" align="left" valign="top">
<a style="font-size:16px">' if(somecondition){ echo "somecontent"; }
else{
echo "somecontent";
}'</a><br />
</body>
</html>';

您可以使用 . 运算符进行连接,请参见下面的示例:

$cart_body='<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>New Order Placed</title>
</head>
<body>
<table width="550" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="400" align="left" valign="top">
<a style="font-size:16px">';
if(somecondition){ $cart_body .= "somecontent"; }
else{
$cart_body .= "somecontent";
}

$cart_body .= '</a><br />
</body>
</html>';

如果您有充分的理由将所有 HTML 存储在一个变量中,一种有条件地根据需要更改它的方法是:

$cart_body='<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>New Order Placed</title>
</head>
<body>
<table width="550" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="400" align="left" valign="top">
<a style="font-size:16px">'.
(somecondition?"somecontent":"othercontent")
.'</a><br />
</body>
</html>';

虽然不漂亮。

你可以这样使用

<a style="font-size:16px">';
if(somecondition){ $cart_body .= "somecontent"; }
else{
$cart_body .= "somecontent";
}

$cart_body .= '</a><br />
</body>
</html>';

我建议使用 sprintf() 方法来 format/render 您的模板。像这样,您可以在模板中放置尽可能多的占位符,然后以更易读的方式锻炼您的逻辑,然后在最后为占位符注入值:)

<?php
// preparing the template
$cart_body = <<<EOF
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        <title>New Order Placed</title>
    </head>
    <body>
        <table width="550" border="0" cellspacing="0" cellpadding="0">
            <tr>
                <td width="400" align="left" valign="top">
                    <a style="font-size:16px">%s</a><br/>
                </td>
            </tr>
        </table>
    </body>
</html>
EOF;

// working out the logic
$basedOnThisCondition = true;
$whatYouWantToInject = ($basedOnThisCondition) ? 'Was true' : 'Was false' ;

// Injecting value to the placeholder (%s for string)
$rendered_cart_body = sprintf($cart_body, $whatYouWantToInject);

// And here is to test the result 
print_r($rendered_cart_body);

p.s。

您的 html 模板中缺少一些关闭器,我已在上面的示例中进行了修复:)