Fix Uncaught syntax error unexpected token < in

2019-08-23 11:02发布

问题:

I'm testing an electron app that will use php for some business logics. If I run the app from my MAMP stack all will work correctly, but if I run the app using electron, I will recieve an uncaught syntax error unexpected token <that is referenced to the <!DOCTYPE html> of my templates file. I read about this problem here on SO that is related to the path of my js and css files, but I can't figure out how to fix it, this because the paths are correct. I have an header.php file that include all the <head> code and it's required by all the templates files, is the problem caused by this?

header.php file:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet" href="assets/css/bootstrap.min.css" type="text/css">
<link rel="stylesheet" href="assets/css/fontawesome-all.min.css" type="text/css">

<script type="text/javascript" src="assets/js/jquery.min.js"></script>
<script type="text/javascript" src="assets/js/bootstrap.bundle.min.js"></script>
</head>
<body>

dashboard.php template file:

<?php
require_once 'header.php';
?>
<div class="container">
  <div class="row justify-content-center">

    <div class="col-lg-4">
      <div class="card">
        <div class="card-img-top text-center">
          <i class="fas fa-plus fa-2x"></i>
        </div>
        <a class="btn btn-link" href="add"><h4 class="text-uppercase">add info</h4></a>
      </div>
    </div>

    <div class="col-lg-4">
      <div class="card">
        <div class="card-img-top text-center">
          <i class="fas fa-search fa-2x"></i>
        </div>
      <a class="btn btn-link" href="search"><h4 class="text-uppercase">search info</h4></a>
      </div>
    </div>

  </div>
</div>

</body>
</html>

回答1:

Your header.php file is a HTML document. Since you require it, it will be interpreted as PHP which causes the syntax error.

Either create a valid PHP header file or print the contents from the header.php file. I would not recommend the later.

A simple solution would be:

  1. Rename your header.php to header.tpl (.tpl as in template)
  2. Change you dashboard.php
<?php
print file_get_contents('header.tpl'); 
?>
...