javascript - Node.js + Mustache to Preprocess Static HTML -


I am working on a project for which static HTML pages must be converted into new static HTML pages. I scraped pages with chirio for content and stored the relationship between pages as JSON.

The challenge is to create a static HTML page that contains a table of contents that connects everything to each other.

Template:

  & lt; H1 & gt; Table of contents & lt; / H1> {{#toc}} & lt; H2 & gt; {{moduleName}} & lt; / H2 & gt; & Lt; ul class = 'module' & gt; {{#page}} & lt; Li & gt; & Lt; a href = '{{url}}' & gt; {{Title}} & lt; / A & gt; & Lt; / li & gt; {{/ page}} & lt; / Ul & gt; {{/ Toc}}   

Data:

  {"toc": [{"moduleName": "starting", "page": [ {"Title": "welcome", "url": "L0-01_Welcome.html"}, {"title": "who should read this?", "Url": "L0-02_WhoFor.html"}]}, {"ModuleName": "Module 1", "Page": [{"Title": "Definitions", "url": "L1-01_Definitions.html"}]}]}   

Node setup:

  var mustache = essential ("mustache"); Var fs = requirement ("fs"); var cheerio = requires ("charion"); // file path var pathostost = "./templates/toc.mustache"; Var Pathosaxen = "./menu/data.json"; // Generate HTML menu // var html menu = mustache Render (fs.readFileSync (PathtoMastak) .string (), fs.readFileSync (pathToJSON)); var html menu = mustache html_html (fs.readFileSync (pathToMustache) .toString (), fs.readFileSync (pathToJSON)); Console.log (htmlMenu); // Re loop through HEL files that adds new menus using Cheerio ...   

This successfully & lt; H1 & gt; Table abstract & lt; / H1> , but nothing else. I must remember something very obvious, because I can not understand the meaning of this

I am very new to programming, along with mustaches, so the advice would be greatly appreciated.

You read JSON as a text string from a file and before starting it mustache Must have to convert the object. Use

JSON.parse : 'Strict use'; Var Mustache = Essential ("Mustache"); Var FS = Requirement ("FS"); Var page = fs.readFileSync ("page.mustache"). ToString (); Var data = JSON (fs.readFileSync ("data.json") .toString ()); Var h = Mustache Render (page, data); console.log (h);

Output:

  & lt; H1 & gt; Table table & lt; / H1> & Lt; H2 & gt; Getting started & lt; / H2 & gt; & Lt; Ul class = 'module' & gt; & Lt; Li & gt; & Lt; A href = 'L0-01_Welcome.html' & gt; Welcome & lt; / A & gt; & Lt; / li & gt; & Lt; Li & gt; & Lt; A href = 'L0-02_WhoFor.html' & gt; Who should read it? & Lt; / A & gt; & Lt; / li & gt; & Lt; / Ul & gt; & Lt; H2 & gt; Module 1 & lt; / H2 & gt; & Lt; Ul class = 'module' & gt; & Lt; Li & gt; & Lt; A href = 'l1-01_Definitions.html' & gt; Definitions & lt; / A & gt; & Lt; / li & gt; & Lt; / Ul & gt;   

In Javascript, there are basically two functions: and.

JSON.parse - Returns an object for the given JSON-text

JSON.stringify - to a value JSON-Notation Converts

Comments

Popular posts from this blog

php - PDO bindParam() fatal error -

logging - How can I log both the Request.InputStream and Response.OutputStream traffic in my ASP.NET MVC3 Application for specific Actions? -

java - Why my included JSP file won't get processed correctly? -