2015-03-07 26 views
7

के साथ एक जेड टेम्पलेट से कई HTML फ़ाइलों का उत्पादन कैसे करें I Gulp के साथ एक स्थिर साइट जेनरेटर बना रहा हूं। मैं सोच रहा हूं कि एकाधिक HTML फ़ाइलों का उत्पादन करने के लिए डेटा के प्रत्येक टुकड़े (JSON) को एक जेड टेम्पलेट में कैसे पाइप करना है।गुलप और गल्प-डेटा

gulp.task('templates', function() { 
    'use strict'; 

    var myData = JSON.parse(fs.readFileSync('./_assets/data/content.json')); 

    var myPages = myData.pages; 
    var myPosts = myData.posts; 

    gulp.src('./_assets/templates/index.jade') 
    .pipe(data(function (file) { 
     return myData; 
    })) 
    .pipe(jade()) 
    .pipe(minifyhtml()) 
    .pipe(gulp.dest('./_site/')) 
    .pipe(connect.reload()); 

    // this is my dumb attempt at trying to 
    // output each piece of data and than 
    // have jade create an html file... 
    for (var i in myPages) { 
    gulp.src('./_assets/templates/page.jade') 
    .pipe(data(function (file) { 
     return myPages[i]; 
    })) 
    .pipe(jade()) 
    .pipe(minifyhtml()) 
    .pipe(gulp.dest('./_site/')) 
    .pipe(connect.reload());  
    } 

}); 

function applyTemplate(data, template) { 
    'use strict'; 
    // I was thinking maybe another function could 
    // take care of applying a template to each json 
    // data object in both 'posts' or 'pages' 
    return; 
} 

यहाँ और डेटा है कि मेरी सामग्री के सभी से उत्पादन किया है और एक JSON संरचित फ़ाइल content.json

{ 
    "pages": { 
    "projects": { 
     "title": "projects", 
     "body": "" 
    }, 
    "another": { 
     "title": "another", 
     "body": "" 
    }, 
    "about": { 
     "title": "about", 
     "body": "<p>This page is dedicated to the about me section of the site :). Lorem ipsum dolor sit amet, consectetur adipisicing elit. Non in totam suscipit laborum nam nobis. Mollitia, adipisci, iusto impedit molestiae, sapiente numquam laborum dignissimos quam animi eius ipsum eaque voluptate. Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nisi, possimus quia. Deleniti itaque facilis eius, possimus sint molestiae repellendus, repellat, rerum quis aliquam voluptate quidem voluptas dignissimos, perferendis molestias sequi. Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quisquam reiciendis corporis, molestiae tempora similique laudantium eaque fuga, fugit delectus molestias omnis incidunt nostrum expedita! Aliquid temporibus eum dolor, beatae saepe.</p>\n<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Dignissimos, commodi, quos. Ratione laborum assumenda aliquam soluta consequatur alias, recusandae corporis, quidem eveniet quo natus nulla voluptates praesentium aperiam ducimus ea!</p>\n<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quae numquam, doloremque dicta magni quaerat consequatur culpa quisquam dolore. Modi at rerum reiciendis esse, deserunt sit culpa repudiandae repellat, id earum. Lorem ipsum dolor sit amet, consectetur adipisicing elit. Asperiores sunt officiis fugit dolor quisquam aut eveniet odio delectus quam quasi debitis quas, eligendi consequatur iste aperiam minus nemo soluta a.</p>\n" 
    }, 
    "page": { 
     "title": "page", 
     "body": "" 
    }, 
    "test": { 
     "title": "test", 
     "body": "" 
    } 
    }, 
    "posts": { 
    "whatsnext": { 
     "title": "What's Next?!", 
     "date": "04/01/2015", 
     "published": true, 
     "body": "<p>So I need to come up with some kind of plan of attack towards this project. Ive mostly been just messing around with gulp im going to list out what needs to be done next. Bare with me...(talking to myself?).</p>\n<h3 id=\"things-to-do-\">Things to do:</h3>\n<ul>\n<li>Instead of converting to json just extract front matter and post .md--&gt;.html transfer in the templates</li>\n<li>organize folder structure to better reflect this project and change the templating system to something simple project:<ul>\n<li>_assets/<ul>\n<li>scripts/</li>\n<li>templates/</li>\n<li>styles/</li>\n<li>images/</li>\n<li>etc.../</li>\n</ul>\n</li>\n<li>_content/<ul>\n<li>posts/</li>\n<li>pages/</li>\n</ul>\n</li>\n<li>.gitignore</li>\n<li>gulpfile.js</li>\n<li>prose.yml</li>\n<li>travis.yml</li>\n<li>package.json</li>\n<li>readme.md</li>\n</ul>\n</li>\n<li>Create a simple, global cli to &quot;scaffold&quot; sites that utilize the scope of this project. Some helpful links:<ul>\n<li><a href=\"http://tutorialzine.com/2014/09/creating-your-first-node-js-command-line-application/\">http://tutorialzine.com/2014/09/creating-your-first-node-js-command-line-application/</a></li>\n<li><a href=\"http://cruft.io/posts/node-command-line-utilities\">http://cruft.io/posts/node-command-line-utilities</a></li>\n</ul>\n</li>\n<li>git r done</li>\n</ul>\n" 
    }, 
    "itsalive": { 
     "title": "Its Alive!!!", 
     "date": "2/24/2015", 
     "published": true, 
     "body": "<p>My site is finally coming together. I need to check out more on the gulp subject and get everything working correctly. Some helpful links will be <a href=\"http://blog.crushingpennies.com/a-static-site-generator-with-gulp-proseio-and-travis-ci.html\">gulp + prose</a> and another one: <a href=\"http://www.rioki.org/2014/06/09/jekyll-to-gulp.html\">from jekyll to gulp</a>. And now for a weird baby.</p>\n<p><img src=\"../../_assets/images/babycreep.gif\" alt=\"dat baby creep\"></p>\n<p>The goal of this project is to create something that resembles a cms without all of the bloat. I&#39;ve been working with Drupal and its a very powerful tool but it comes at a price for developers. I&#39;m really wanting to cut out php and myself and focus on what the web wants and that is html, css, and JavaScript... nothing more...except for all the other things written in node(js) and maybe some ruby</p>\n" 
    }, 
    "secondpost": { 
     "title": "Test", 
     "date": "2/24/2015", 
     "body": "<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ipsam obcaecati expedita dolorem unde nemo, veritatis quidem velit perspiciatis illum quasi ratione. Provident et reiciendis numquam maxime, alias, aliquid harum omnis. Lorem ipsum dolor sit amet, consectetur adipisicing elit. Modi optio nostrum, incidunt aut! Illo, architecto, numquam? Neque commodi ex tempora. Dolorem rem omnis nulla ab blanditiis saepe at id, dolore.</p>\n<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Molestias eius officia officiis quos, obcaecati tempora suscipit optio rem. Deserunt qui, quidem. Fuga hic dolor sit voluptatem qui, dignissimos. Consequuntur, sint!</p>\n<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. In dignissimos voluptatem nemo quae neque a dolorum provident culpa error corporis, fuga alias corrupti, quaerat illum facere, et repellat voluptatibus similique? Lorem ipsum dolor sit amet, consectetur adipisicing elit. Asperiores veniam impedit esse iure, sint repellat facilis. Rerum excepturi eos hic incidunt quam, quidem sit odio dignissimos tenetur neque beatae illo.</p>\n" 
    } 
    } 
} 

कहा जाता है में डाल आप कर सकते हैं के रूप में किया जाता है: यहाँ घूंट 'टेम्पलेट्स' काम नहीं है देखें, 'content.json' का डेटा आसानी से पार्स किया गया है और मेरे जेड टेम्पलेट में पाइप किया गया है। जहां मैं छोटा हो रहा हूं, डेटा के साथ कई एचटीएमएल फाइलों का उत्पादन करना और प्रत्येक प्रकार के डेटा के लिए केवल एक टेम्पलेट फ़ाइल का उपयोग करना

उदाहरण: 'pages.jade' पृष्ठों के प्रत्येक पृष्ठ पर "लागू" होगा content.json फ़ाइल, और 'about.html', 'anotherpage.html' जैसी अलग-अलग HTML फ़ाइलों के रूप में outputted।

मुझे बताएं कि आपको अधिक जानकारी चाहिए और मेरे प्रश्न को देखने के लिए समय निकालने के लिए बहुत बहुत धन्यवाद!

  • क्रिस

उत्तर

5

मैं दो संभव समाधान के साथ आया था। मैं स्विग का उपयोग कर रहा हूं, लेकिन यह किसी भी अन्य टेम्पलेट भाषा के लिए समान होना चाहिए।

पहला समाधान थोड़ा 'हैकिश' दिख सकता है, क्योंकि यह हमारे प्रत्येक JSON फ़ाइलों के लिए HTML टेम्पलेट को "अनुकरण" करने के लिए सिम्लिंक का उपयोग करता है। यही है, प्रत्येक HTML फ़ाइल के लिए हम बनाना चाहते हैं, हमारे पास एक सिम्लिंक है जो हमारी एकल स्विग टेम्पलेट फ़ाइल को इंगित करता है। तो, हम है:। product-A.html -> product.swig.tpl, product-B.html -> product.swig.tpl, product-C.html -> product.swig.tpl और इतने पर (सभी सिमलिंक और बड़ा घूँट टेम्पलेट _assets/html/ फ़ोल्डर में जमा हो जाती है
फिर, हम _assets/data/ फ़ोल्डर (product-A.json, product-B.json, product-C.json, आदि) में हमारे JSON फ़ाइलें

कोड:।

var gulp = require('gulp'); 
var swig = require('gulp-swig'); 
var data = require('gulp-data'); 
var path = require('path'); 

var config = { 
     html: { 
      src: '_assets/html/*.html', 
      dest: '_build/html' 
     }, 
     data: { 
      src: '_assets/data/*.json' 
     } 
     } 

var getJsonData = function(file) { 
    return require('./data/' + path.basename(file.path, path.extname(file.path)) + '.json'); 
}; 

gulp.task('templates', function() { 
    return gulp.src(config.html.src) 
    .pipe(data(getJsonData)) 
    .pipe(swig()) 
    .pipe(gulp.dest(config.html.dest)) 
}); 

दूसरा समाधान का उपयोग करता gulp-foreach
इस उदाहरण में, वहाँ एक ते है _assets/html/ में उदाहरण के लिए, उदाहरण के लिए, product.html), और फिर, हमारे पास _assets/data/ पर उदाहरण के लिए JSON फ़ाइलों का एक समूह है (उदाहरण के लिए, product-A.json, product-B.json, product-C.json और इसी तरह)।

var gulp = require('gulp'); 
var swig = require('gulp-swig'); 
var data = require('gulp-data'); 
var foreach = require('gulp-foreach'); 
var rename = require('gulp-rename'); 
var path = require('path'); 

var config = { 
     html: { 
      src: '_assets/html/*.html', 
      dest: '_build/html' 
     }, 
     data: { 
      src: '_assets/data/*.json' 
     } 
     } 

var getJsonData = function(file) { 
    return require(file.path); 
}; 

gulp.task('templates', function() { 
    return gulp.src(config.data.src) 
    .pipe(foreach(function(stream, file){ 
     var jsonFile = file; // We create this 'jsonFile' variable because the 'file' variable is overwritten on the next gulp.src. 
     var jsonBasename = path.basename(jsonFile.path, path.extname(jsonFile.path)); 
     return gulp.src(config.html.src) 
      .pipe(data(getJsonData(jsonFile))) 
      .pipe(swig()) 
      .pipe(rename(function(htmlFile) { 
      htmlFile.basename = jsonBasename; 
      })) 
      .pipe(gulp.dest(config.html.dest)) 
     }) 
    ) 
}); 

तो, मूल रूप से, हम JSON फाइल के हमारे सेट के साथ एक धारा बनाते हैं, और प्रत्येक फ़ाइल के लिए, हम एक टेम्पलेट लेने, हमारे JSON फ़ाइल से डेटा प्राप्त और टेम्पलेट से लागू होते हैं। फिर, हम उसी फ़ाइल पर लिखने (ओवरराइट) लिखने के लिए प्रत्येक लूप से बचने के लिए, इसे अपने गंतव्य को आउटपुट करने से पहले बफर का नाम बदलते हैं। फिर, हम _build/html पर HTML फ़ाइलों का एक समूह के साथ अंत: product-A.html, product-C.html, product-C.html

+0

यह एक एकल .json फ़ाइल से .html फ़ाइल उत्पन्न करने के लिए संभव है? उदाहरण के लिए मेरे पास एक .json है जिसमें ऑब्जेक्ट है और प्रत्येक .html फ़ाइल के लिए सरणी के अंदर है। – chalo

+0

@chalo मुझे लगता है कि आप ऑब्जेक्ट पर फिर से सक्रिय हो सकते हैं, प्रत्येक सरणी ला सकते हैं और प्रत्येक के लिए एक फाइल बना सकते हैं।यह सुनिश्चित नहीं है कि इसे कैसे किया जाए, जैसे कि फाइलों के साथ काम करते हैं, लेकिन मुझे लगता है कि कुछ डेटा को फ़ाइल (vynil?) ऑब्जेक्ट में कनवर्ट करने का एक तरीका है और इसके साथ गलप को वास्तविक फ़ाइल के रूप में काम करने दें। –

संबंधित मुद्दे