如何打印html代码,这样我只写过一次


How to print html code so that i wrote it only once?

我在一个web应用程序中工作,正在进行一些重构。

无论如何,在这样做的时候,我陷入了一个困境:我的页面中有一些相似或相同的部分,我想只压缩一个,以便只进行一次编辑,因为编辑正在成为一种痛苦。

但是如何做到这一点呢?我正在使用php、js和jquery。

使用php-echos做这件事是一个很大的禁忌;管理所有的括号真的很困难,无论如何,在我看来这不是最优雅的解决方案,而且,我需要对我打印的东西进行一些控制。

我在想函数,但由于我没有太多的经验,我真的不知道我的脑袋在哪里。提前谢谢。

编辑:有人问我一些例子。

<form action="insert.php" method="POST" class="big" style="display:none;">
<h3> Insert Contact </h3>
    <fieldset class="col2">
    <!-- Some inputs here -->
    </fieldset>
    <div>
        <input type="submit" value="Send">
        <input type="reset" value="Clean Form">
    </div>
    <input type="text" name="insert" value="1" style="display:none;">
    <input type="text" name="modify" value="1" style="display:none;">
    <input type="text" name="secret" value="<? echo($perm); ?>" class="hidden">
</form>

例如,这是我想在某些页面中添加的一个表单。在其他方面我不想要。但是,例如,根据进行调用的页面,我需要切换一些参数或更改一些值,我认为这是php includes无法做到的。

您可以使用PHP的include()函数在文件中包含HTML。

使用您提供的HTML的示例

form.php

<form action="insert.php" method="POST" class="big" style="display:none;">
<h3> Insert Contact </h3>
    <fieldset class="col2">
    <!-- Some inputs here -->
    </fieldset>
    <div>
        <input type="submit" value="Send">
        <input type="reset" value="Clean Form">
    </div>
    <input type="text" name="insert" value="1" style="display:none;">
    <input type="text" name="modify" value="1" style="display:none;">
    <input type="text" name="secret" value="<? echo($perm); ?>" class="hidden">
</form>

header.php

<div>Hello World!</div>
<?php $value = 10; ?>

index.php

<html>
    <head>
    </head>
    <body>
        <?php include(header.php); ?>
        <div>This is some content.  The number is <?php echo($value); ?></div>
        <?php include(form.php); ?>
    </body>
</html>

输出HTML

<html>
    <head>
    </head>
    <body>
        <div>Hello World!</div>
        <div>This is some content.  The number is 10</div>
        <form action="insert.php" method="POST" class="big" style="display:none;">
        <h3> Insert Contact </h3>
        <fieldset class="col2">
        <!-- Some inputs here -->
        </fieldset>
        <div>
            <input type="submit" value="Send">
            <input type="reset" value="Clean Form">
        </div>
        <input type="text" name="insert" value="1" style="display:none;">
        <input type="text" name="modify" value="1" style="display:none;">
        <input type="text" name="secret" value="<? echo($perm); ?>" class="hidden">
        </form>
    </body>
</html>

 
 
 

如果你想在两种不同的形式之间进行选择,你可以使用以下任何一种方法:

方法1:在不同页面上包含相关表格

insert.php

<?php include("insert-form.php"); ?>

edit.php

<?php include("edit-form.php"); ?>

方法2:包含基于条件的相关表格

index.php

<?php
$formchoose = "insert";
if($formchoose == "insert") {
    include("form-insert.php");
} else {
    include("form-default.php");
}
?>

方法3:在include中做有条件的(这是你在评论中问的)

index.php

<?php
$formchoose = "insert";
include(forms.php);
?>

forms.php

switch ($formchoose) {
    case "insert":
        ?>
            <form id="insert-form">
            <!-- form here-->
            </form>
        <?
    break;
    case "edit":
        ?>
            <form id="edit-form">
            <!-- form here-->
            </form>
        <?
    break;
    default:
    break;
}

文件

http://php.net/manual/en/function.include.php

您可以查看PHP include()(包含示例)

它很容易使用,你只需要写:<?php include 'my_file.php'; ?>

由于您可以在.php文件中编写html/js,因此可以使用它来包含页面的部分内容。

另一个有助于制作动态表单的想法:

some_dynamic_form.php

<?php
function getDynamic($form)
{
    switch ($form) {
        case 'form1':
            return array('hello', 'world', '1');
        case 'form2':
            return array('hello', 'from', '2');
    }
}
?>

index.html

<?php
require_once 'some_dynamic_form.php';
$dynamicForm = getDynamic('form1');
?>
<html>
<body>
<form action="#">
    <?php foreach ($dynamicForm as $param): ?>
        <input type="text" name="<?php echo $param ?>" />
    <?php endforeach; ?>
</form>
</body>
</html>

你可以用它做任何你想做的事情,使它变得更好:)创建一个表单类,使其成为OPP

我建议使用MVC方法来分离模型(数据)、视图和控制器(应用程序的大脑)。

您还应该考虑使用模板引擎,例如Twig。