2

I'm looking if its possible when using grunt to build a task that would use my folder structure to build data.

I wanted to build a website from folder structure.

For example I have:

- data
-- folder_n
--- options.json
--- webpagetext.txt
--- additional.html

etc.

Then generate my webpage also in folder structure (folder_n/{variable_as_friendly_url}.html)

Is it possible to do with grunt? I read most of docs, but did not find if that would be possible.

Edit: How would I see it in PHP

<?php
/*
    Basic project structure

    templates/
    - template_1.php
    - template_2.php
    - template_3.php
    datafeed/
    - subpage_1/
    -- header_image.png
    -- webpagetext.txt
    -- options.json
    - subpage_2/
    -- header_image.png
    -- webpagetext.txt
    -- options.json
    - subpage_3/
    -- header_image.png
    -- webpagetext.txt
    -- options.json
    - subpage_n/
    -- header_image.png
    -- webpagetext.txt
    -- options.json

*/

// IN PHP it would look like.

foreach (glob('datafeed/*') as $dir){
    foreach(glob('templates/*') as $tpl){
        constructpage($dir, $tpl);
    }
}

function constructpage($dir, $tpl) {
    $options = json_decode($dir.'options.json');
    $output_html_file_name = 'output/'.basename($dir) . '/' . basename($dir) . '.html';
    /*
        Logic that would copy, assign vars etc for template in $tpl.
    */

}

But grunt is my choice because of: I want to learn it, it got some pretty cool tools.

Grzegorz
  • 3,538
  • 4
  • 29
  • 47
  • trying to understand: you would like to create a website boilerplate, to create new websites quickly and automatically, using Gruntjs? – Manube May 24 '15 at 20:11
  • More like create ready articles (subpages) for sites from data in files. For example: one folder is article. It can use one of 3 templates and those are parsed with options. PHP without php. Want to have already generated "cache" files to be served. Not sure if you get my point – Grzegorz May 25 '15 at 00:03
  • I think what you need are Yeoman sub-generators – Manube May 25 '15 at 03:33

1 Answers1

0

What you need is a Yeoman generator/sub-generator


WHAT IT IS:

According to the Yeoman website, generator is basically a plugin that can be run with the yo command to scaffold complete projects or useful parts


WHAT IT CAN DO

You can run Yeoman commands from the command line; Yeoman is able to acommodate the code it creates and adapt it to the parameters you provide, or which are present in your files.


DOCUMENTATION

For more information about Yeoman scaffolding:

Yeoman official tutorial

Call sub-generators with user-supplied arguments


FOR (STILL) MORE AUTOMATION

Also, although I still only have a partial understanding of what you really mean to achieve, you might want to trigger the creation of files by Yeoman, when some file directory exists, by using a Grunt watch like so:

watch: {
  scripts: {
    files: ['**/*.file-directory-to-detect'],
    tasks: ['myCustomYeomanScript'],
  },
}

and grunt-shell like so:

grunt.initConfig({
shell: {
    myCustomYeomanScript: {
        command: 'yo createMyFiles'
    }
}

});

Community
  • 1
  • 1
Manube
  • 5,110
  • 3
  • 35
  • 59
  • Hello, thank you for your answer. I updated question making a prototype workflow of script in PHP. Maybe it would help to understand me. Not sure if Yeoman is for this. Yeoman from what I understood is designed to create ready code for big sites. Not small subpages. Also it totally is not going to use javascript, and css would be inline (it's strange but I know what im doing :) ). PS: Im still digging into yeoman docs meanwhile. – Grzegorz May 25 '15 at 06:19
  • Yes, Yeoman can scaffold anything, big or small, and any file type too, whatever the language. 100% configurable – Manube May 25 '15 at 06:52
  • You are welcome. I know Yeoman can seem complex, or not enough documented; but just follow the tutorial, observe what files are created, how to structure the model templates, and you will soon be able to customize your scaffolding to your liking: you could easily have Yeoman create PHP files; afterword: not affiliated to Yeoman, but I like the tool – Manube May 25 '15 at 07:24
  • Hey, I did. Ended up writing my own scripts. Found ``Yo`` a bit too advanced for things I want to do. But answer is good... but not sure how I should chose best answer... the one that is better (your), or the one that solves my problem best (mine). – Grzegorz Jun 01 '15 at 07:43
  • life can be hard, sometimes :) – Manube Jun 01 '15 at 08:16