How to print html code so that I write it only once?

I am working on a web application and I am refactoring.

In any case, I was caught in a dilemma: I have some similar or identical parts on my pages that I want to compress only one in order to make some changes only once, as the changes become pain.

But how to do that? I work with php, js and jquery.

Doing this with php echos is a big no; it is really difficult to manage all the brackets, and in any case it doesn’t seem like the most elegant solution, plus, I need to do some control over the things that I print.

I thought about the functions, but since I do not have much experience, I do not know where I can hit my head. Thank you in advance.

EDIT: I was given a few examples.

<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.

+4
4

HTML PHP include().

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

+3

PHP include() ( )

, : <?php include 'my_file.php'; ?>

html/js .php , .

0

, :

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

0

MVC- (), ( ).

You should also consider using a template engine such as Twig.

-1
source

Source: https://habr.com/ru/post/1621630/


All Articles