You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP文件中复用HTML标记的更优实现方案咨询

Optimized Ways to Reuse Your Pardot Form HTML

Great question! Since your core form content is identical across both the container block and modal scenarios, we can clean this up to follow DRY (Don't Repeat Yourself) principles—here are a few practical, concise approaches tailored to your needs:

1. Wrap the Form in a PHP Function (Most Maintainable)

This is my go-to for reusable HTML/PHP snippets. You can define the form once as a function, then call it wherever you need it. You can even keep this function directly in your index.php file if you want to avoid separate includes entirely.

// Define the form function at the top of index.php
function render_pardot_form() {
    ob_start(); // Use output buffering to capture clean HTML
    ?>
    <form class="form">
        <div class="form-group">
            <label for="formGroupExampleInput">First Name:</label>
            <input type="text" class="form-control" id="formGroupExampleInput" placeholder="Example input placeholder">
        </div>
        <div class="form-group">
            <label for="formGroupExampleInput2">Last Name:</label>
            <input type="text" class="form-control" id="formGroupExampleInput2" placeholder="Another input placeholder">
        </div>
        <div class="my-3">
            <button type="submit" class="btn btn-primary" disabled>Submit</button>
        </div>
    </form>
    <?php
    return ob_get_clean(); // Return the captured HTML as a string
}

// Your existing mode logic
$mode = get_field('mode');
$id = // Your existing $id variable here
$class_name = // Your existing $class_name variable here
?>

<?php if ($mode == 'page'): ?>
<div class="container my-4 <?= $class_name?>">
    <div class="row justify-content-md-center pardot-form">
        <div class="col-md-6 col-lg-6">
            <h3>Pardot Form</h3>
            <?= render_pardot_form() ?>
        </div>
    </div>
</div>
<?php else: ?>
<div class="modal_element" data-toggle="modal" data-target="#modal-<?= $id ?>" data-cookie="<?= get_field('cookie'); ?>"></div>
<div class="modal fade" id="modal-<?= $id ?>" tabindex="-1" role="dialog" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="exampleModalLabel">Pardot Form</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&#10005;</span>
                </button>
            </div>
            <div class="modal-body">
                <?= render_pardot_form() ?>
            </div>
        </div>
    </div>
</div>
<?php endif; ?>

Why this works: The function encapsulates your form logic, so you only ever edit the form in one place. Output buffering lets you write clean, indentable HTML without messy string concatenation, making the code easier to read and maintain. It also gives you flexibility later if you need to add dynamic logic (like pre-filling fields) to the form.

2. Use a String Variable with HEREDOC (Simple for Static HTML)

If your form doesn’t need dynamic PHP logic inside it, you can store the entire form as a string using PHP’s HEREDOC syntax. This keeps everything in one file without functions, and reads almost like plain HTML.

// Define the form as a string at the top of index.php
$pardot_form = <<<HTML
<form class="form">
    <div class="form-group">
        <label for="formGroupExampleInput">First Name:</label>
        <input type="text" class="form-control" id="formGroupExampleInput" placeholder="Example input placeholder">
    </div>
    <div class="form-group">
        <label for="formGroupExampleInput2">Last Name:</label>
        <input type="text" class="form-control" id="formGroupExampleInput2" placeholder="Another input placeholder">
    </div>
    <div class="my-3">
        <button type="submit" class="btn btn-primary" disabled>Submit</button>
    </div>
</form>
HTML;

// Your existing mode logic
$mode = get_field('mode');
$id = // Your existing $id variable here
$class_name = // Your existing $class_name variable here
?>

<?php if ($mode == 'page'): ?>
<div class="container my-4 <?= $class_name?>">
    <div class="row justify-content-md-center pardot-form">
        <div class="col-md-6 col-lg-6">
            <h3>Pardot Form</h3>
            <?= $pardot_form ?>
        </div>
    </div>
</div>
<?php else: ?>
<div class="modal_element" data-toggle="modal" data-target="#modal-<?= $id ?>" data-cookie="<?= get_field('cookie'); ?>"></div>
<div class="modal fade" id="modal-<?= $id ?>" tabindex="-1" role="dialog" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="exampleModalLabel">Pardot Form</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&#10005;</span>
                </button>
            </div>
            <div class="modal-body">
                <?= $pardot_form ?>
            </div>
        </div>
    </div>
</div>
<?php endif; ?>

Why this works: HEREDOC lets you write multi-line strings without escaping quotes, so the form HTML stays readable and easy to edit. It’s a great choice if your form is static and you want a minimal, straightforward solution.

Both approaches eliminate duplicate code and make your index.php much cleaner. The function method is the most flexible for future changes, while HEREDOC is perfect for simple, static forms.

内容的提问来源于stack exchange,提问作者user16645285

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 10:42:42