2

I'm editing an HTML file that also contains CSS and Javascript. I would like to keep this in an Org file with each in separate blocks like

#+BEGIN_SRC css
CSS code ...
#+END_SRC

#+BEGIN_SRC js
Javascript code ...
#+END_SRC

#+BEGIN_SRC html
html ...
#+END_SRC

so that when I edit the blocks with "C-c '" I can be in the proper mode. However the HTML block needs to include (substitute) the other 2 blocks verbatim, but Org wants to "evaluate" them, which doesn't make sense in this case. I haven't found a way to make a variable contain a whole named block literally and then be substituted. Is this possible?

1 Answer 1

2

You seem to want to use the literate capabilities of org mode, with noweb reference:

#+name: mycss
#+begin_src css
  This is css
#+end_src

#+name: myjs
#+begin_src js
  this is js
#+end_src

#+name: myhtlm
#+begin_src html :noweb yes
  <html>
    <head>
      <style type="text/css" media="screen">
        <<mycss>>
      </style>
      <code>
        <<myjs>>
      </code>
    </head>
  </html>
#+end_src

<<mycss>> and <<myjs>> are noweb reference, that will be replaced by the content of the mycss and myjs source block

3
  • 1
    Thank you very much! I never quite understood how the noweb thing worked but this is probably what I want. But when I hit "C-c C-c" on the "#+begin_src html" line it says "org-babel-execute-src-block: No org-babel-execute function for html!". How do I make it create the output?
    – jtgd
    Commented Apr 16, 2018 at 22:47
  • OK, poking around I discovered org-babel-expand-src-block which seems to expand correctly. I puts it in a temp buffer it creates, but I'm wondering if there are other ways to do this. Ultimately I will want this in a file which I upload to S3. Is there a way I can script this with elisp or possibly in some Org-style?
    – jtgd
    Commented Apr 17, 2018 at 6:19
  • (org-babel-expand-noweb-references) seems to be what I was looking for. Once I get the point inside the block and call that it returns it as a string. Thanks again.
    – jtgd
    Commented Apr 17, 2018 at 7:01

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.