HTML Templates — php?

前端 未结 4 1511
时光说笑
时光说笑 2021-02-05 23:20

So, I\'m making a site about WWI as a school assignment, and I want this to appear in every document:





    <         


        
相关标签:
4条回答
  • 2021-02-05 23:45

    Put the content in a file abc.php

    and then add this to each page you want the desired content in :

    <?php
    include("abc.php");
    ?>
    

    So if your code is :

    <nav>
            <ul>
            <a href="index.htm"><li><span>Home</span></li></a>
            <a href="1914.htm"><li><span>1914</span></li></a>
            <a href="1915.htm"><li><span>1915</span></li></a>
            <a href="1916.htm"><li><span>1916</span></li></a>
            <a href="1917.htm"><li><span>1917</span></li></a>
            <a href="1918.htm"><li><span>1918</span></li></a>
            </ul>
        </nav>
        </header>
        <section>
        <article>
        <br style="clear: both" />
        </article>
        <aside>
        </aside>
        </section>
    

    And you want the part inside <nav> to be repeated in each page, you can put the content between <nav> and </nav> (including the tags) inside abc.php and include abc.php in your file like this :

    <?php
        include("abc.php");
    ?>
        </header>
        <section>
        <article>
        <br style="clear: both" />
        </article>
        <aside>
        </aside>
        </section>
    
    0 讨论(0)
  • 2021-02-05 23:58

    You could consider one of these popular template engines :

    • Smarty (becoming outdated)

    • Latte (used mostly by the Nette community)

    • Twig (used mostly by the Symfony community)

    • Mustache (official implementations of this templating engine exist in more than two dozen proramming/scripting languages)

    I tend to favor Mustache, mostly because it has an official JS version, an official Ruby version, an official Java version, etc. It allows you to use the same templates frontend and backend, which is very useful for widgets that are first rendered in background and rerendered in foreground at updates.

    0 讨论(0)
  • 2021-02-06 00:00

    Using php includes:

    Save this as top.php

    <html>
    <head>
    <title><?php echo $title; ?></title>
    <script src="modernizr-1.5.js"></script>
    <link href="styles.css" type="text/css" rel="stylesheet" />
    <meta charset="utf-8" />
    
    </head>
    <body>
    <div id="container">
    <header>
    <img src="images/banner.png" alt="World War I" style="border: none"/>
    <nav>
        <ul>
        <a href="index.htm"><li><span>Home</span></li></a>
        <a href="1914.htm"><li><span>1914</span></li></a>
        <a href="1915.htm"><li><span>1915</span></li></a>
        <a href="1916.htm"><li><span>1916</span></li></a>
        <a href="1917.htm"><li><span>1917</span></li></a>
        <a href="1918.htm"><li><span>1918</span></li></a>
        </ul>
    </nav>
    </header>
    <section>
    

    Save this as bottom.php

    <aside>
    </aside>
    </section>
    <footer style="font-weight: bold; letter-spacing: .1em">
    <a href="citations.htm">Citations</a> &bull;
    <a href="about.htm">About</a>
    </footer>
    </div>
    </body>
    </html>
    

    Then your individual pages would be like this:

    <?php $title = '1914'; include("top.php");?>
    //This would be where you would make the changes that need to be made on each page.
    
    <article>
    <br style="clear: both" />
    </article>
    
    <?php include("bottom.php");?>
    
    0 讨论(0)
  • 2021-02-06 00:01

    Create the template file as a single file, exactly you have it already, but printing PHP variables in the places where you want content.

    eg:

    ....
    <article>
    <?php print $mainContent; ?>
    </article>
    ....
    

    Then write a PHP function as follows:

    <?php
    function insertContentIntoTemplate($mainContent) {
        require_once('/path/to/template.php');
    }
    ?>
    

    Now you can load your page content into the template simply by calling the function and passing the content into it for each page.

    So, for example, 1914.php could look like this:

    <?php
    require_once('/path/to/insertFunction.php');
    
    //the text could be loaded from a DB, or from another file, or just as a plain string like this:
    $text = "The year was 1914, and the war was just starting.";
    
    insertContentIntoTemplate($text);
    
    ?>
    

    Congratulations. You now have a working (albeit very simple) template system. Add more variables / placeholders to your template as required.

    0 讨论(0)
提交回复
热议问题