Re: [ClojureScript] How to read a file into a string Clojurescript

4,512 views
Skip to first unread message

Sean Grove

unread,
Mar 1, 2013, 10:19:53 AM3/1/13
to clojur...@googlegroups.com
This is obviously only going to be available on the Clojure runtime (i.e. you can't read a text file from the Clojurescript runtime), but I user a macro in my Clojure code:

(ns zenbox.macros
  (:require [clojure.pprint :as pprint])
  (:import java.io.File))

(defmacro load-template [relative-uri]
  "Reads and returns a template as a string."
  (slurp relative-uri))

And then in my Clojurescript code:

(ns zenbox.client.templates
  (:require-macros [zenbox.macros :as tm]))

(def user-profile-template 
  (tm/load-template "resources/public/js/handlebars/templates/user_profile.hbs"))

I have a bit more sugar on top, and (tm/load-template ...) will happen at compile time, so this works just fine for loading resources from a disk (great for templating, etc.), but again this won't work at runtime.

Best,
Sean


On Fri, Mar 1, 2013 at 7:09 AM, Topcraft <devalc...@gmail.com> wrote:
Hello,

How can I read the contents of a textfile into a string in clojurescript? I've only found some information about how to do this in Clojure.  http://clojuredocs.org/clojure_core/clojure.core/slurp But not how to use this in clojurescript. If I put this:

(:use [clojure.walk :only [walk]]
      [clojure.core :only [slurp]])

in my namespace declaration I receive an error : Uncaught Error: goog.require could not find: clojure.core . Does anybody has an idea how I can manage to read the contents of a file into a string.

Thanks

--
Note that posts from new members are moderated - please be patient with your first post.
---
You received this message because you are subscribed to the Google Groups "ClojureScript" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojurescrip...@googlegroups.com.
To post to this group, send email to clojur...@googlegroups.com.
Visit this group at http://groups.google.com/group/clojurescript?hl=en.



Dave Sann

unread,
Mar 2, 2013, 2:50:31 AM3/2/13
to clojur...@googlegroups.com
You can put the file on a server and read from there (via http).

or, you could try accessing the javascript filesystem api if you really want to read from the local filesystem.

http://www.html5rocks.com/en/tutorials/file/filesystem/

chrome only.

On Saturday, 2 March 2013 13:32:58 UTC+11, Evan Mezeske wrote:
> Reading a file is a platform-specific thing in JavaScript (ugh), and AFAIK there is not currently any abstraction in ClojureScript that allows you to read a file in a platform-independent way.
>
> If you are running on node.js, which I assume you are because you want to read a file, you can use JS interop to use node's API for reading files:
>
> http://docs.nodejitsu.com/articles/file-system/how-to-read-files-in-nodejs
>
> I have not run this code, so I'm sure it's horribly broken, but this is a rough idea of what that node.js example would look like in ClojureScript:
>
> (let [fs (.require "fs")]
> (.readFile fs "/etc/hosts" "utf8" (fn [err data]
> (.log console (or err data)))))

Reply all
Reply to author
Forward
0 new messages