0

I have three different view files,

// view1.ejs file

<span>view1</span>

// view2.ejs file

<span>view2</span>

// view3.ejs file

<span>view3</span>

and I want each of them to include common.ejs file respectively.

// common.ejs

<div class="beginning">
    <div>
        <span>something</span>
    </div>
</div>

<div class="end">
    <div>
        <span>something</span>
    </div>
</div>

But the problem is that, I want to include common.ejs file like the way below,

// view1.ejs file

<div class="beginning">
    <div>
        <span>something</span>
    </div>
</div>

<span>view1</span> // if the case of view2.ejs, <span>view2</span>

<div class="end">
    <div>
        <span>something</span>
    </div>
</div>

In other words, I want to firstly divide a common.ejs file into pieces, then put(include) them in the way I like.

If I have ten view files, it would be a lot waste of code, copy and paste.

Is there any way to do that?

Any advice would be appreciated. Thanks!

jwkoo
  • 2,393
  • 5
  • 22
  • 35
  • Look into ejs partials – sharkdawg Mar 06 '18 at 12:35
  • @Enda Molloy thanks for your comment, but I have already read the article, https://stackoverflow.com/questions/5404830/node-js-ejs-including-a-partial. so do I have to seperate common.ejs file into two pieces? Is that the only way I can do? I posted this question wondering if there would be better approach. – jwkoo Mar 06 '18 at 12:39
  • 1
    It's not the only way but best practice would be to have seperate partials for say header and footer – sharkdawg Mar 06 '18 at 12:45
  • Well, maybe I have expected too much of it, ejs. Thanks! – jwkoo Mar 06 '18 at 13:19
  • @jwkoo It looks like community has cleared your doubt. Please try to answer your question so that it may be helpful for others in finding answers. Cheers – Bharathvaj Ganesan Mar 06 '18 at 14:10

0 Answers0