我有一个用django制作的网站,它基本上是一个教程网站,我想在我的教程中动态突出显示代码我该怎么办?
答案 0 :(得分:1)
我建议使用一些JavaScript框架。例如,highlight.js或prism.js。将要突出显示的代码通过<!DOCTYPE html>
<html>
<head>
<title>Table with database</title>
<style>
table {
border-collapse: collapse;
width: 100%;
color: #588c7e;
font-family: monospace;
font-size: 25px;
text-align: left;
}
th {
background-color: #588c7e;
color: white;
}
tr:nth-child(even) {background-color: #f2f2f2}
</style>
</head>
<body>
<table>
<tr>
<th>Id</th>
<th>First Name</th>
<th>Last Name</th>
<th>Address</th>
<th>City</th>
<th>State</th>
<th>Zipcode</th>
<th>Telephone</th>
<th>Email</th>
</tr>
<?php
$con = mysqli_connect('localhost','username','password');
if(!con) {
echo 'Error: Not connected to the server.';
}
if(!mysqli_select_db($con,'MPITdBase')) {
echo 'Error: Database is not selected';
}
$sql = "SELECT `custID` , `custLastName` , `custFirstName` ,
`custStreet` ,
`custCity` , `custState` , `custZipcode` , `custTX` , `custEmail`
FROM `Customer`";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<tr><td>"$row["custID"]"</td><td>"$row["custFirstName"]"</td>
<td>"$row["custLastName"]"</td><td>"$row["custStreet"]"</td>
<td>"$row["custCity"]"</td><td>"$row["custState"]"</td>
<td>"$row["custZipcode"]"
</td><td>"$row["custTX"]"</td><td>"$row["custEmail"]"</td></tr>";
}
echo "</table>";
} else { echo "0 results"; }
$conn->close();
?>
</table>
</body>
</html>
- 函数传递给django模板,并使用JS对其进行着色。