Pug/Jade and inline javascript calculations












0















I'm lost, again, trying to do some simple calculations in jade template.



Given this data object:



{
"trade": {
"name": "Mogens",
"dst_currency": "EUR",
"dst_value": 115.7,
"src_price": null,
"src_value": 2,
"src_currency": "XMR",
"date": null
}
}


And this pug source:



table
thead
tr
th Currency
th Quantity
th Price
th Total
th Date
tbody
tr
script.
if (trade.dst_currency === "EUR")
trade.src_price = trade.dst_value / trade.src_value
else
trade.src_price = Number(trade.src_value) / Number(trade.dst_value)
th.align-middle #{trade.dst_currency}
th.align-middle #{trade.dst_value}
th.align-middle= #{trade.src_price}
th.align-middle #{trade.src_value} #{trade.src_currency}
th.align-middle #{trade.date}

if trade.name === "Bob"
h1 Hello Bob
else
h1 My name is #{trade.name}


How is this done, if at all possible? What am I missing?










share|improve this question





























    0















    I'm lost, again, trying to do some simple calculations in jade template.



    Given this data object:



    {
    "trade": {
    "name": "Mogens",
    "dst_currency": "EUR",
    "dst_value": 115.7,
    "src_price": null,
    "src_value": 2,
    "src_currency": "XMR",
    "date": null
    }
    }


    And this pug source:



    table
    thead
    tr
    th Currency
    th Quantity
    th Price
    th Total
    th Date
    tbody
    tr
    script.
    if (trade.dst_currency === "EUR")
    trade.src_price = trade.dst_value / trade.src_value
    else
    trade.src_price = Number(trade.src_value) / Number(trade.dst_value)
    th.align-middle #{trade.dst_currency}
    th.align-middle #{trade.dst_value}
    th.align-middle= #{trade.src_price}
    th.align-middle #{trade.src_value} #{trade.src_currency}
    th.align-middle #{trade.date}

    if trade.name === "Bob"
    h1 Hello Bob
    else
    h1 My name is #{trade.name}


    How is this done, if at all possible? What am I missing?










    share|improve this question



























      0












      0








      0








      I'm lost, again, trying to do some simple calculations in jade template.



      Given this data object:



      {
      "trade": {
      "name": "Mogens",
      "dst_currency": "EUR",
      "dst_value": 115.7,
      "src_price": null,
      "src_value": 2,
      "src_currency": "XMR",
      "date": null
      }
      }


      And this pug source:



      table
      thead
      tr
      th Currency
      th Quantity
      th Price
      th Total
      th Date
      tbody
      tr
      script.
      if (trade.dst_currency === "EUR")
      trade.src_price = trade.dst_value / trade.src_value
      else
      trade.src_price = Number(trade.src_value) / Number(trade.dst_value)
      th.align-middle #{trade.dst_currency}
      th.align-middle #{trade.dst_value}
      th.align-middle= #{trade.src_price}
      th.align-middle #{trade.src_value} #{trade.src_currency}
      th.align-middle #{trade.date}

      if trade.name === "Bob"
      h1 Hello Bob
      else
      h1 My name is #{trade.name}


      How is this done, if at all possible? What am I missing?










      share|improve this question
















      I'm lost, again, trying to do some simple calculations in jade template.



      Given this data object:



      {
      "trade": {
      "name": "Mogens",
      "dst_currency": "EUR",
      "dst_value": 115.7,
      "src_price": null,
      "src_value": 2,
      "src_currency": "XMR",
      "date": null
      }
      }


      And this pug source:



      table
      thead
      tr
      th Currency
      th Quantity
      th Price
      th Total
      th Date
      tbody
      tr
      script.
      if (trade.dst_currency === "EUR")
      trade.src_price = trade.dst_value / trade.src_value
      else
      trade.src_price = Number(trade.src_value) / Number(trade.dst_value)
      th.align-middle #{trade.dst_currency}
      th.align-middle #{trade.dst_value}
      th.align-middle= #{trade.src_price}
      th.align-middle #{trade.src_value} #{trade.src_currency}
      th.align-middle #{trade.date}

      if trade.name === "Bob"
      h1 Hello Bob
      else
      h1 My name is #{trade.name}


      How is this done, if at all possible? What am I missing?







      javascript pug






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 24 '18 at 7:26







      Mogens TrasherDK

















      asked Nov 24 '18 at 7:20









      Mogens TrasherDKMogens TrasherDK

      1691211




      1691211
























          2 Answers
          2






          active

          oldest

          votes


















          0














          Placing a script tag in your Pug code renders a script tag in your compiled HTML. It does not tell Pug to execute any javascript within the script tag when it compiles. If you want to run javascript in Pug as your code is compiled, use an unbuffered code block.



          -
          // this is an unbuffered code block
          // that will update the value of `trade.src_price`
          // before it is rendered by Pug
          if (trade.dst_currency === "EUR") {
          trade.src_price = trade.dst_value / trade.src_value
          } else {
          trade.src_price = Number(trade.src_value) / Number(trade.dst_value)
          }

          table
          thead
          tr
          th Currency
          th Quantity
          th Price
          th Total
          th Date
          tbody
          tr
          th.align-middle #{trade.dst_currency}
          th.align-middle #{trade.dst_value}
          th.align-middle= #{trade.src_price}
          th.align-middle #{trade.src_value} #{trade.src_currency}
          th.align-middle #{trade.date}

          if trade.name === "Bob"
          h1 Hello Bob
          else
          h1 My name is #{trade.name}





          share|improve this answer































            0














            Okay. Figured it out at last.



            I had to give up on the inline script, and go with something more simple.



            table
            thead
            tr
            th Currency
            th Quantity
            th Price
            th Total
            th Date
            tbody
            tr
            th.align-middle #{trade.dst_currency}
            th.align-middle #{trade.dst_value}
            if (trade.dst_currency === "EUR")
            th.align-middle #{trade.dst_value / trade.src_value}
            else
            th.align-middle #{trade.src_value / trade.dst_value}
            th.align-middle #{trade.src_value} #{trade.src_currency}
            th.align-middle #{trade.date}
            p.
            EUR #{trade.dst_value / trade.src_value}
            XMR #{trade.src_value / trade.dst_value}

            - var name = "Bobby"
            if name == "Bob"
            h1 Hello #{name}
            else
            h1 My name is #{trade.name}, born on #{trade.date}


            Compiles to



            <table>
            <thead>
            <tr>
            <th>Currency</th>
            <th>Quantity</th>
            <th>Price</th>
            <th>Total</th>
            <th>Date</th>
            </tr>
            </thead>
            <tbody>
            <tr>
            <th class="align-middle">EUR</th>
            <th class="align-middle">115.7</th>
            <th class="align-middle">57.85</th>
            <th class="align-middle">2 XMR</th>
            <th class="align-middle">29 May, 1958</th>
            </tr>
            </tbody>
            </table>
            <p>
            EUR 57.85
            XMR 0.017286084701815037

            </p>
            <h1>My name is Mogens, born on 29 May, 1958</h1>


            And that actually makes some sense.



            (I would still like to be able to inline javascript, if anybody has ideas)






            share|improve this answer























              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53456071%2fpug-jade-and-inline-javascript-calculations%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              0














              Placing a script tag in your Pug code renders a script tag in your compiled HTML. It does not tell Pug to execute any javascript within the script tag when it compiles. If you want to run javascript in Pug as your code is compiled, use an unbuffered code block.



              -
              // this is an unbuffered code block
              // that will update the value of `trade.src_price`
              // before it is rendered by Pug
              if (trade.dst_currency === "EUR") {
              trade.src_price = trade.dst_value / trade.src_value
              } else {
              trade.src_price = Number(trade.src_value) / Number(trade.dst_value)
              }

              table
              thead
              tr
              th Currency
              th Quantity
              th Price
              th Total
              th Date
              tbody
              tr
              th.align-middle #{trade.dst_currency}
              th.align-middle #{trade.dst_value}
              th.align-middle= #{trade.src_price}
              th.align-middle #{trade.src_value} #{trade.src_currency}
              th.align-middle #{trade.date}

              if trade.name === "Bob"
              h1 Hello Bob
              else
              h1 My name is #{trade.name}





              share|improve this answer




























                0














                Placing a script tag in your Pug code renders a script tag in your compiled HTML. It does not tell Pug to execute any javascript within the script tag when it compiles. If you want to run javascript in Pug as your code is compiled, use an unbuffered code block.



                -
                // this is an unbuffered code block
                // that will update the value of `trade.src_price`
                // before it is rendered by Pug
                if (trade.dst_currency === "EUR") {
                trade.src_price = trade.dst_value / trade.src_value
                } else {
                trade.src_price = Number(trade.src_value) / Number(trade.dst_value)
                }

                table
                thead
                tr
                th Currency
                th Quantity
                th Price
                th Total
                th Date
                tbody
                tr
                th.align-middle #{trade.dst_currency}
                th.align-middle #{trade.dst_value}
                th.align-middle= #{trade.src_price}
                th.align-middle #{trade.src_value} #{trade.src_currency}
                th.align-middle #{trade.date}

                if trade.name === "Bob"
                h1 Hello Bob
                else
                h1 My name is #{trade.name}





                share|improve this answer


























                  0












                  0








                  0







                  Placing a script tag in your Pug code renders a script tag in your compiled HTML. It does not tell Pug to execute any javascript within the script tag when it compiles. If you want to run javascript in Pug as your code is compiled, use an unbuffered code block.



                  -
                  // this is an unbuffered code block
                  // that will update the value of `trade.src_price`
                  // before it is rendered by Pug
                  if (trade.dst_currency === "EUR") {
                  trade.src_price = trade.dst_value / trade.src_value
                  } else {
                  trade.src_price = Number(trade.src_value) / Number(trade.dst_value)
                  }

                  table
                  thead
                  tr
                  th Currency
                  th Quantity
                  th Price
                  th Total
                  th Date
                  tbody
                  tr
                  th.align-middle #{trade.dst_currency}
                  th.align-middle #{trade.dst_value}
                  th.align-middle= #{trade.src_price}
                  th.align-middle #{trade.src_value} #{trade.src_currency}
                  th.align-middle #{trade.date}

                  if trade.name === "Bob"
                  h1 Hello Bob
                  else
                  h1 My name is #{trade.name}





                  share|improve this answer













                  Placing a script tag in your Pug code renders a script tag in your compiled HTML. It does not tell Pug to execute any javascript within the script tag when it compiles. If you want to run javascript in Pug as your code is compiled, use an unbuffered code block.



                  -
                  // this is an unbuffered code block
                  // that will update the value of `trade.src_price`
                  // before it is rendered by Pug
                  if (trade.dst_currency === "EUR") {
                  trade.src_price = trade.dst_value / trade.src_value
                  } else {
                  trade.src_price = Number(trade.src_value) / Number(trade.dst_value)
                  }

                  table
                  thead
                  tr
                  th Currency
                  th Quantity
                  th Price
                  th Total
                  th Date
                  tbody
                  tr
                  th.align-middle #{trade.dst_currency}
                  th.align-middle #{trade.dst_value}
                  th.align-middle= #{trade.src_price}
                  th.align-middle #{trade.src_value} #{trade.src_currency}
                  th.align-middle #{trade.date}

                  if trade.name === "Bob"
                  h1 Hello Bob
                  else
                  h1 My name is #{trade.name}






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 26 '18 at 15:05









                  seansean

                  976620




                  976620

























                      0














                      Okay. Figured it out at last.



                      I had to give up on the inline script, and go with something more simple.



                      table
                      thead
                      tr
                      th Currency
                      th Quantity
                      th Price
                      th Total
                      th Date
                      tbody
                      tr
                      th.align-middle #{trade.dst_currency}
                      th.align-middle #{trade.dst_value}
                      if (trade.dst_currency === "EUR")
                      th.align-middle #{trade.dst_value / trade.src_value}
                      else
                      th.align-middle #{trade.src_value / trade.dst_value}
                      th.align-middle #{trade.src_value} #{trade.src_currency}
                      th.align-middle #{trade.date}
                      p.
                      EUR #{trade.dst_value / trade.src_value}
                      XMR #{trade.src_value / trade.dst_value}

                      - var name = "Bobby"
                      if name == "Bob"
                      h1 Hello #{name}
                      else
                      h1 My name is #{trade.name}, born on #{trade.date}


                      Compiles to



                      <table>
                      <thead>
                      <tr>
                      <th>Currency</th>
                      <th>Quantity</th>
                      <th>Price</th>
                      <th>Total</th>
                      <th>Date</th>
                      </tr>
                      </thead>
                      <tbody>
                      <tr>
                      <th class="align-middle">EUR</th>
                      <th class="align-middle">115.7</th>
                      <th class="align-middle">57.85</th>
                      <th class="align-middle">2 XMR</th>
                      <th class="align-middle">29 May, 1958</th>
                      </tr>
                      </tbody>
                      </table>
                      <p>
                      EUR 57.85
                      XMR 0.017286084701815037

                      </p>
                      <h1>My name is Mogens, born on 29 May, 1958</h1>


                      And that actually makes some sense.



                      (I would still like to be able to inline javascript, if anybody has ideas)






                      share|improve this answer




























                        0














                        Okay. Figured it out at last.



                        I had to give up on the inline script, and go with something more simple.



                        table
                        thead
                        tr
                        th Currency
                        th Quantity
                        th Price
                        th Total
                        th Date
                        tbody
                        tr
                        th.align-middle #{trade.dst_currency}
                        th.align-middle #{trade.dst_value}
                        if (trade.dst_currency === "EUR")
                        th.align-middle #{trade.dst_value / trade.src_value}
                        else
                        th.align-middle #{trade.src_value / trade.dst_value}
                        th.align-middle #{trade.src_value} #{trade.src_currency}
                        th.align-middle #{trade.date}
                        p.
                        EUR #{trade.dst_value / trade.src_value}
                        XMR #{trade.src_value / trade.dst_value}

                        - var name = "Bobby"
                        if name == "Bob"
                        h1 Hello #{name}
                        else
                        h1 My name is #{trade.name}, born on #{trade.date}


                        Compiles to



                        <table>
                        <thead>
                        <tr>
                        <th>Currency</th>
                        <th>Quantity</th>
                        <th>Price</th>
                        <th>Total</th>
                        <th>Date</th>
                        </tr>
                        </thead>
                        <tbody>
                        <tr>
                        <th class="align-middle">EUR</th>
                        <th class="align-middle">115.7</th>
                        <th class="align-middle">57.85</th>
                        <th class="align-middle">2 XMR</th>
                        <th class="align-middle">29 May, 1958</th>
                        </tr>
                        </tbody>
                        </table>
                        <p>
                        EUR 57.85
                        XMR 0.017286084701815037

                        </p>
                        <h1>My name is Mogens, born on 29 May, 1958</h1>


                        And that actually makes some sense.



                        (I would still like to be able to inline javascript, if anybody has ideas)






                        share|improve this answer


























                          0












                          0








                          0







                          Okay. Figured it out at last.



                          I had to give up on the inline script, and go with something more simple.



                          table
                          thead
                          tr
                          th Currency
                          th Quantity
                          th Price
                          th Total
                          th Date
                          tbody
                          tr
                          th.align-middle #{trade.dst_currency}
                          th.align-middle #{trade.dst_value}
                          if (trade.dst_currency === "EUR")
                          th.align-middle #{trade.dst_value / trade.src_value}
                          else
                          th.align-middle #{trade.src_value / trade.dst_value}
                          th.align-middle #{trade.src_value} #{trade.src_currency}
                          th.align-middle #{trade.date}
                          p.
                          EUR #{trade.dst_value / trade.src_value}
                          XMR #{trade.src_value / trade.dst_value}

                          - var name = "Bobby"
                          if name == "Bob"
                          h1 Hello #{name}
                          else
                          h1 My name is #{trade.name}, born on #{trade.date}


                          Compiles to



                          <table>
                          <thead>
                          <tr>
                          <th>Currency</th>
                          <th>Quantity</th>
                          <th>Price</th>
                          <th>Total</th>
                          <th>Date</th>
                          </tr>
                          </thead>
                          <tbody>
                          <tr>
                          <th class="align-middle">EUR</th>
                          <th class="align-middle">115.7</th>
                          <th class="align-middle">57.85</th>
                          <th class="align-middle">2 XMR</th>
                          <th class="align-middle">29 May, 1958</th>
                          </tr>
                          </tbody>
                          </table>
                          <p>
                          EUR 57.85
                          XMR 0.017286084701815037

                          </p>
                          <h1>My name is Mogens, born on 29 May, 1958</h1>


                          And that actually makes some sense.



                          (I would still like to be able to inline javascript, if anybody has ideas)






                          share|improve this answer













                          Okay. Figured it out at last.



                          I had to give up on the inline script, and go with something more simple.



                          table
                          thead
                          tr
                          th Currency
                          th Quantity
                          th Price
                          th Total
                          th Date
                          tbody
                          tr
                          th.align-middle #{trade.dst_currency}
                          th.align-middle #{trade.dst_value}
                          if (trade.dst_currency === "EUR")
                          th.align-middle #{trade.dst_value / trade.src_value}
                          else
                          th.align-middle #{trade.src_value / trade.dst_value}
                          th.align-middle #{trade.src_value} #{trade.src_currency}
                          th.align-middle #{trade.date}
                          p.
                          EUR #{trade.dst_value / trade.src_value}
                          XMR #{trade.src_value / trade.dst_value}

                          - var name = "Bobby"
                          if name == "Bob"
                          h1 Hello #{name}
                          else
                          h1 My name is #{trade.name}, born on #{trade.date}


                          Compiles to



                          <table>
                          <thead>
                          <tr>
                          <th>Currency</th>
                          <th>Quantity</th>
                          <th>Price</th>
                          <th>Total</th>
                          <th>Date</th>
                          </tr>
                          </thead>
                          <tbody>
                          <tr>
                          <th class="align-middle">EUR</th>
                          <th class="align-middle">115.7</th>
                          <th class="align-middle">57.85</th>
                          <th class="align-middle">2 XMR</th>
                          <th class="align-middle">29 May, 1958</th>
                          </tr>
                          </tbody>
                          </table>
                          <p>
                          EUR 57.85
                          XMR 0.017286084701815037

                          </p>
                          <h1>My name is Mogens, born on 29 May, 1958</h1>


                          And that actually makes some sense.



                          (I would still like to be able to inline javascript, if anybody has ideas)







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 25 '18 at 3:28









                          Mogens TrasherDKMogens TrasherDK

                          1691211




                          1691211






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53456071%2fpug-jade-and-inline-javascript-calculations%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

                              Calculate evaluation metrics using cross_val_predict sklearn

                              Insert data from modal to MySQL (multiple modal on website)