2

I have a programm where I need to have long multi line strings. It's a pain to store them in the .js document, because js doesn't have multi line strings and I end up having a twice as long as the screen width line looking as ugly as "This is an example.\n"

Is there a way to have a txt file, from where I can import strings with new lines (or at least just import strings)?

Euphe
  • 3,531
  • 6
  • 39
  • 69
  • More information about how you are rendering the string would be helpful. Is it html? Is it in a dialog box? What does the program do? – Dropzilla Jul 03 '13 at 16:57
  • 1
    possible duplicate of [How do I load the contents of a text file into a javascript variable?](http://stackoverflow.com/questions/196498/how-do-i-load-the-contents-of-a-text-file-into-a-javascript-variable) – zurfyx Jul 03 '13 at 20:33
  • 2
    Nope, definitely not a duplicate; this is much harder to do in Meteor and a feature that isn't implemented yet. – Andrew Mao Jul 04 '13 at 16:27

3 Answers3

7

There is a Meteor Assets object that allows you to read files in the private directory of your app, in the following way for example for text files.

Assets.getText("foo.txt", function (err, res) { ... });

See full documentation: http://docs.meteor.com/#assets

Andrew Mao
  • 35,740
  • 23
  • 143
  • 224
2

Previous answer works only for public files. If you want to access file data that is visible only on the server you should probably use 'fs' npm module. It's described in details here: http://www.eventedmind.com/posts/meteor-file-uploader-part-2-server-side-save

Łukasz Jagodziński
  • 3,009
  • 2
  • 25
  • 33
1

The meteor-yaml package makes this easy - it automatically loads any .yaml files in your project, parses them into JavaScript objects, and makes them available in YAML.data.

In my application I have some code outside of the meteor app that needs the same settings, so I prefer to have the config file outside of the meteor project directory. Then I load the file like this:

var fs = Npm.require('fs');
fs.readFile('<path to file>.yaml', 'utf8', function(err, data) {
  if(err) { 
    //Throw exception if the file is missing
    throw new Error("Missing config file")
  }
  else {
    //Read the file into a JavaScript object
    config = YAML.parse(data); 
  }
});

Unfortunately, the meteor-yaml package is a little out of date with how the meteor team wants node packages to be loaded now, so if you're using a recent version of meteor the package won't work out of the box.

I filed a bug about this, but in the meantime to get around it I installed it as a private package, as opposed to installing it from atmosphere, and fixed the bug. To do this:

  1. Clone the repo under your projects packages/ directory
  2. Comment out the Npm.require lines.
  3. Add a call to depends:

    Npm.depends({yamljs: "0.1.4"});

  4. Run meteor. Meteor will detect the meteor-yaml private package and install the dependencies.

jrullmann
  • 2,912
  • 1
  • 25
  • 27
  • Oh it appears it's for meteorite. I can't use that. Thanks anyway – Euphe Jul 10 '13 at 19:04
  • Oh, that reminds me - the meteor-yaml package has a silly bug that makes it not work with recent versions of meteor. To get around it I installed it as a custom package instead of an atmosphere package. I'll update my answer to include that info – jrullmann Jul 10 '13 at 19:55
  • 1
    Euphe - the instructions for installing it as a private package should work without meteorite. – jrullmann Jul 10 '13 at 20:06