上QQ阅读APP看书,第一时间看更新
How to do it...
- In workspace/app, add a new folder and call it partial:
cd && cd worspace/app && mkdir partial
- Add _nav.ejs to the partial folder--touch partial/_nav.ejs.
- Open the app/index.html and cut all the code starting with <nav> and ending with </nav> (the whole navbar). Paste the code into the _nav.ejs file.
- In index.html, right where you cut out the <nav> element, paste this include:
<%- partial("partial/_nav") %>
- Make a new file, _data.json, in workspace/app:
touch _data.json
- Paste the following code into it:
{
"index": {
"title": "Welcome to Our Homepage"
},
"about": {
"title": "About Us"
}
}
Note that "index" is the name of the file ( index.html), and "title" is the text for the <title> tag of the index page. We can add more pages as needed.
- Now, let's copy and rename index.html to _layout.ejs:
cp index.html _layout.ejs
- C9 into index.html and delete everything except the div with the class of container, that is, keep only the following code in index.html:
<div class="container">
<div class="starter-template">
<h1>Bootstrap 4 Grid Recipe</h1>
<p class="lead">Use this document to quickly start any new
project.
<br> All you get is this text and a barebones HTML document.
</p>
</div>
</div><!-- /.container -->
- Change the file extension of index.html to index.ejs:
mv index.html index.ejs
- In _layout.ejs, add the title of the current page:
<title><%- title %></title>
- Also, delete everything under the _nav partial include, all the way up to the comment <!-- Bootstrap core JavaScript ... and place just this one line there:
<%- yield %>
- Run the harp compile and harp server commands to verify that everything still works.
In this recipe, we have split our code into includes, making it a lot easier to work with. In the following recipes, we will deal with layouts.
Do not forget to change the variable overrides in the main.scss file if you want your recipe code to have a nice background color and your navbar to have regular-sized text.