Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Haml-Coffee smart package #138

Closed
wants to merge 1 commit into from
Closed

Haml-Coffee smart package #138

wants to merge 1 commit into from

Conversation

huetsch
Copy link

@huetsch huetsch commented May 14, 2012

I've added a smart package to be able to render .hamlc files via https://github.com/9elements/haml-coffee. I had to ever-so-slightly tweak their npm package into my own version, called 'haml-coffee-meteor', to get it to work. Templates generated by .hamlc files are currently available via a global HAML object. So calling HAML['filename'] when you have a file named filename.hamlc in your app will give you a function that you can call to render.

Please let me know what else I might need to do to get the package merged.

@ncri
Copy link

ncri commented Jun 3, 2012

Just trying this out using HAML['filename'], getting an error: Uncaught ReferenceError: HAML is not defined

@huetsch
Copy link
Author

huetsch commented Jun 5, 2012

Hmm, are you sure your file extension is .hamlc? The only reason I can think of the HAML object not being defined is if meteor never saw a .hamlc file.

@ncri
Copy link

ncri commented Jun 5, 2012

Yep.

@huetsch
Copy link
Author

huetsch commented Jun 5, 2012

Can you post your .meteor/packages?

This relies on the haml-coffee-meteor package. My installation process was as follows:

install meteor (I'm assuming to /usr/local/meteor, which is the default on OS X)
cd /usr/local/meteor/lib/node_modules && npm install haml-coffee-meteor
cd wherever_your_app_is && meteor add haml-coffee

Regarding the haml-coffee-meteor npm package, I wish I had a better way of including it, but all the smart package dependencies are bundled up in a file that's hosted on the meteor devs' server. I would suggest the current way of smart packaging is far from ideal. If the install process isn't your issue, I'll try to figure out what else could be it.

@ncri
Copy link

ncri commented Jun 5, 2012

I did exactly the same process. Hmm. Thanks a lot for helping. I guess if you run a meteor app on heroku (for example) you can simply ship the meteor version with it that has the required npm packages.

@ncri
Copy link

ncri commented Jun 5, 2012

Oh wait, actually I did another thing: I copied your haml-coffee package to my original meteor installation, to /usr/local/meteor/packages. So I did not install meteor from your git repo. Maybe I missed something?

@huetsch
Copy link
Author

huetsch commented Jun 5, 2012

Copying haml-coffee to /usr/local/meteor/packages should work fine too.

So 'meteor add haml-coffee' worked fine?

@huetsch
Copy link
Author

huetsch commented Jun 5, 2012

So, presuming you have foo.hamlc in your app directory, .meteor/local/build/static/ should contain foo.hamlc.js. Does it?

@ncri
Copy link

ncri commented Jun 5, 2012

Yes, I could do meteor add haml-coffee fine. Yes, my myfile.hamlc.js is in the app dir.

@ncri
Copy link

ncri commented Jun 5, 2012

Can you show me a small working example app i could test out?

@huetsch
Copy link
Author

huetsch commented Jun 5, 2012

Here's a skeleton app: https://gist.github.com/2876037
I deployed it directly to: http://test_hamlc.meteor.com/

@ncri
Copy link

ncri commented Jun 5, 2012

Your example works fine for me.

My code is somewhat different and uses coffeescript:

 if Meteor.is_client

   HAML['myfile'].my_template.myvariable = ->
   ...

Maybe there is the issue? Works fine with html templates though.

@ncri
Copy link

ncri commented Jun 5, 2012

Yep, as soon as i try to do the code from my previous comment in your testapp, i get the same error as in my app:

Uncaught TypeError: Cannot read property 'my_template' of undefined

@huetsch
Copy link
Author

huetsch commented Jun 5, 2012

I've used this extensively with coffeescript without issue. You should note that your current error suggests meteor is no longer having issues finding HAML, but rather it looks as if it's not finding 'myfile.hamlc'. So something has changed. Moreover, "HAML['myfile'].my_template.myvariable = ->", doesn't make sense to me. HAML['myfile'] should be a function which you call, passing an object with your desired locals. Look more closely at my example.

I suggest you dig in a bit - try translating my example to coffeescript and then work from there.

@ncri
Copy link

ncri commented Jun 5, 2012

The error is coming from your example now which i changed. HAML['myfile'].my_template.myvariable is the default way templates are accessed in meteor. They use it in their screencast. I simply replaced Template.xyz.abc -> with HAML['myfile'].xyz.abc is that not the way to go? I think ideally Template should just work with your haml package too, would be more convenient...

See the screencast, the first screen. They use Template.xxx.yyy: http://meteor.com/screencast

@ncri
Copy link

ncri commented Jun 5, 2012

Here is a very basic example that works fine with html, but not with haml:

testhaml.hamlc

%head

%body
  hello
  {{> my_template}}

%template{name: "my_template"}
  {{my_var}}

testhaml.coffee

if Meteor.is_client

  HAML['testhaml'].my_template.my_var = ->
    3

if Meteor.is_server
  Meteor.startup ->

It gives me the "HAML is not defined error".

As soon as I rename the haml file to somethingelse.hamlc it does actually find HAML['somethingelse'], but not the template. I then get: "Cannot set property 'my_var' of undefined "

@huetsch
Copy link
Author

huetsch commented Jun 5, 2012

This is not how the package works. You seem to be conflating HAML with handlebars. Handlebars uses {{}} syntax to evaluate locals passed to the context, whereas HAML uses #{} or =. Regarding meteor's way of passing locals to Handlebars templates, I found no need to replicate that. Adding partial support (or %template support, as you have it) was well beyond my needs and looked like a lot of work. Currently, you can render a partial by doing !=HAML.my_partial({foo: 'bar'}). I simply followed the lead of the original haml-coffee package. Feel free to make any of your own modifications that you would like.

@ncri
Copy link

ncri commented Jun 5, 2012

Ah, that clears things up! Sorry, this was obviously a misunderstanding then. I was assuming I get full haml support with your package, and handlebars stuff as well. So that I would have a full replacement for html. Maybe it's because I'm coming from a rails background, where you can swap erb with haml. But yes, thinking about it, not sure how handlebars could be combined with haml. Thanks for helping anyway. ;-)

@dlee
Copy link

dlee commented Jun 13, 2012

So what's meteor's stance on including this in meteor's smart packages? I'm hoping it's 👍!

@dlee
Copy link

dlee commented Jun 14, 2012

@huetsch It would be nice to have capture to string support for chunks. For example:

= Meteor.ui.chunk ->
  %h1= Session.get('title')
  %ul
    - renderElement = (e)->
      %li= e.name
    - renderEmpty = ->
      %li No elements found!
    = Meteor.ui.listChunk Elements.find({}), renderElement, renderEmpty

The example above wouldn't work because the render* functions don't return strings.

@audreyfeldroy
Copy link
Contributor

You can add npm packages to the dev bundle. If a Meteor smart package depends on a particular npm package, then that npm package should be added to the generate-dev-bundle.sh script: https://github.com/meteor/meteor/blob/master/admin/generate-dev-bundle.sh

However, we prefer if all the code for a package goes into the Meteor smart package if possible, rather than creating an accompanying npm package like "haml-coffee-meteor".

Packaging issues aside, just a heads up that Spark changes the underlying API that template engines will use to render. See this thread for more details: https://groups.google.com/forum/?fromgroups#!topic/meteor-core/p2wV-anQMjI%5B1-25%5D

HAML is a big project. The meteor-core mailing list is a good place to discuss architecture and coordinate with Spark work.

StorytellerCZ pushed a commit that referenced this pull request Oct 1, 2021
filipenevola pushed a commit that referenced this pull request Oct 19, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants