构造函数addPage在foreach的第一个字母后剪切所有内容

时间:2013-10-25 18:32:58

标签: php arrays function foreach construct

我正在开发一个新的UI类,我偶然发现了一个奇怪的问题,即脚本会在第一个字母后切断所有内容。让我告诉你我的代码。

class UI {


    // legger CSS-fil til <head>
    public function addCSS($css_file) {
        $this->css[] = array($css_file);
    }

    // legger javaScript-fil til <head>
    public function addJS($js_file) {
        $this->js[] = $js_file;
    }

    // legger sider til <body>
    public function addPage($php_file) {
        $this->php[] = $php_file;
    }

    // Funksjon for add
    public function __construct() {
        // CSS
        $this->addCSS('bootstrap.css');
        $this->addCSS('style.css');

        // JS
        $this->addJS('jquery.js');
        $this->addJS('bootstrap.js');
        $this->addJS('functions.js');

        // Sider
        $this->addPage('home.php');
        $this->addPage('about.php');
    }


    // Struktur for header
    public function getHeader() {
        $html = '<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="">
    <meta keywords="">
    <meta name="author" content="">';

        // Legger inn CSS
        foreach ($this->css as $css) {
            $html .= '
    <link rel="stylesheet" type="text/css" href="css/' . $css[0] . '" />';
        }
    $html .= '
    <title>www.Dan-Levi.no</title>
    <!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!--[if lt IE 9]>
      <script src="js/html5shiv.js"></script>
      <script src="js/respond.min.js"></script>
    <![endif]-->
  </head>
  <body>';
  return $html;
    }


    // Struktur for navigasjonen festet til bunn av siden
    public function getNav() {
        $html = '

    <div class="navbar navbar-inverse navbar-fixed-bottom">
        <div class="container">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
            </div>
            <div class="navbar-collapse collapse">
                <ul class="nav navbar-nav">
                    <li class="active"><a href="#">Home</a></li>
                    <li><a href="#about">About</a></li>
                    <li><a href="#contact">Contact</a></li>
                    <li class="dropdown">
                    <a href="#" class="dropdown-toggle" data-toggle="dropdown">Other stuff <b class="caret"></b></a>
                    <ul class="dropdown-menu">
                        <li><a href="#">Lorem</a></li>
                        <li><a href="#">Ipsum</a></li>
                        <li><a href="#">Dolor</a></li>
                        <li class="divider"></li>
                        <li class="dropdown-header">Social</li>
                        <li><a href="#">Lorem</a></li>
                        <li><a href="#">Ipsum</a></li>
                        <li><a href="#">Dolor</a></li>
                    </ul>
                    </li>
                </ul>
                <span class="navbar-brand pull-right"><small>&copy; '; $html .= date('Y') ; $html .= ' '. htmlentities('Tømta') .' Data Service</small></span>
            </div><!--/.navbar-collapse -->
        </div>
    </div><!--/.navbar -->';
        return $html;
    }


    // Struktur for siden
    public function getContent() {

        // Legger inn Sider
        foreach ($this->php as $php) {
            include 'pages/'.$php;
        }

    }


    // Struktur for footer
    public function getFooter() {

        // Legger inn javaScript
        foreach ($this->js as $js) {
            $html = '
    <script type="text/javascript" src="js/' . $js[0] . '" ></script>';
        }

        $html .= '

  </body>
</html>';
    return $html;
    }


    // Printer ut sideheader
    public function printHeader() {
        print $this->getHeader();
    }

    // printer ut navigasjonen
    public function printNav() {
        print $this->getNav();
    }

    // printer ut sideinnhold
    public function printContent() {
        print $this->getContent();
    }

    // printer ut sidefooter
    public function printFooter() {
        print $this->getFooter();
    }

}

addCSS和addPage函数按预期工作,但是addJS会切断所有内容,只返回第一个字母:$this->addJS('functions.js');

页面源的结尾如下所示:

    <div class="jumbotron">
        <div class="container">
            <h1>Test!</h1>
            <p>Tootsie roll chocolate liquorice jelly. Chocolate cake liquorice cake pie dragée caramels liquorice wafer topping. Lemon drops cotton candy bear claw pudding icing. Chupa chups oat cake candy.</p>
            <p><a class="btn btn-primary btn-lg">Sweet muffin cookie &raquo;</a></p>
        </div>
    </div>



    <script type="text/javascript" src="js/f" ></script>



  </body>
</html>

为什么会发生这种情况?我尝试了一些试验和错误并阅读了一些,无法理解这一个。

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

我重写了代码,现在按预期工作:

// Adds javaScript to <head>
    public function addJavaScript($javascript_file) {
        $this->javascript[] = $javascript_file;
    }

    // Construct
    public function __construct() {
        // JS
        $this->addJavaScript('jquery.js');
        $this->addJavaScript('bootstrap.js');
        $this->addJavaScript('functions.js');

    }

// Adds javaScript (Put this in head)
        foreach ($this->javascript as $js_file) {
            $html .= '
    <script src="js/' . $js_file . '"></script>';
        }