3

Is it possible to create files with pug?

lets take an example I want to create a folder "TestFolder".

This folder contains two files

Test.html - contains a button "alert"

Test.js- contain code for what happens when alert is clicked

now i want to create this folder with Pug and node

2 Answers 2

1

Sure you can write javascript with pug. Therefore you've got the script. tag. However the way you write javascript is not really different. Personally I create a javascript file and reference it from the pug file.

Javascript written in pug looks like this:

script.
  console.log('Foo bar');

A javascript file referenced in a pugfile:

script(src="/js/analytics.js")
var local_data =!{JSON.stringify(data)} // Variable from the backend
Sign up to request clarification or add additional context in comments.

6 Comments

i want to automate the generation of Typescript file basically a whole angular component, so i am planning to write templates and pass data to it and then write to a file. is it possible? any reference?
What? Nor have you written anything about Typescript or Angular in your original post? And yes it's also possible to use variables passed from the backend in your javascript (see edited answer). That's not really different, however I think that defeats the purpose of using a powerful frontend framework like Angular.
please have a look at this. stackoverflow.com/questions/47646587/…
@AnkitRaonka Is your question answered? Feel free to mark it as answer then, so that others now your question has been answered :-). I took a look at your question but I don't even know what nunjucks is. I usually use pug for frontend stuff
no its not answered, maybe this link will help you understand what i want to achieve.
|
1

In my opinion there should be abolutely no problem creating folders and .html as well as .js files, provided you are using some kind of Workflow CLI (like Gulp).

Creating HTMl is easy, as this is what PUG is supposed to do. If you want to place it in a new folder (that should be created and wasn't there before), use your workflow tool. Example:

gulp.task('pug', function () {
    gulp.src('./app/pug/**/_html_*.pug')
        .pipe(pug({
            pretty: true
        }))
        .pipe(gulp.dest('./somenewfolder/anotherone'));
});

Creating JS is a little bit more tricky, but can also be achieved. Just generate your PUG (containing JS only)...:

gulp.task('pugtoJS', function () {
    gulp.src('./app/pug/**/_js_*.pug')
        .pipe(pug({}))
        .pipe(gulp.dest('./somenewfolder/generatedscript'));
    });

...and then change the file type using a renaming plugin like gulp-ext-replace.

Now the only thing to do is removing the <script> tags, in which your JS code lives. This can be done by hand or using an additional package, e.g. gulp-string-replace.

You see, it's all about chaining and integrating NPM-Packages into your Workflow.

Let me know if this works for you. Feel free to ask any questions you might have in the comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.