Giter Site home page Giter Site logo

hogan_assets's Introduction

HoganAssets

HoganAssets compiles your mustache templates with hogan.js on sprockets and the Rails asset pipeline.

hogan.js is a templating engine developed at Twitter that follows the mustache spec and compiles the templates to JavaScript. The first bit is cool, since mustache is cool. The second bit is awesome and full of win because we can now compile our mustache templates on the server using the asset pipeline/sprockets.

This gem contains hogan.js 3.0.0 as of this commit

Installation

Installation with Rails 3.1+

Add this to your Gemfile as part of the assets group

group :assets do
  gem 'hogan_assets'
end

And then execute:

$ bundle

Require hogan.js somewhere in your JavaScript manifest, for example in application.js if you are using Rails 3.1+:

//= require hogan.js

Locate your .mustache templates with your other JavaScript assets, usually in app/assets/javascripts/templates. Require your templates with require_tree:

//= require_tree ./templates

Templates are named for the sub-path from your manifest with require_tree. For example, the file app/assets/javascripts/templates/pages/person.mustache will be named templates/pages/person. See path_prefix below!

Installation with sprockets

Add this line to your Gemfile:

gem 'hogan_assets'

And then execute:

$ bundle

Require hogan.js somewhere in your JavaScript.

Hamstache!

hamstache is the quite popular combination of haml and mustache, a more robust solution exists using haml_assets, but if all you want is nicer markup, you need to take these two additional steps:

Add this line to your Gemfile:

group :assets do
  gem 'haml'
end

And then execute:

$ bundle

Hamstache compilation can be configured using Haml options. For example:

HoganAssets::Config.configure do |config|
  config.haml_options[:ugly] = true
end

You can configure which recognized as hamstache. For example:

HoganAssets::Config.configure do |config|
  config.hamstache_extensions = %w(hamstache hamlhbs)
end

Slimstache!

slimstache is the also popular combination of slim and mustache. Works just like hamstache above.

Configuration

You can configure options using either an intializer or with a YAML file (config/hogan_assets.yml). See lib/hogan_assets/config.rb for details.

Lambda Support

mustache lambdas are off by default. (Not sure what that is? Read the mustache man page!) If you want them on, set the lambda_support option to true. This will include the raw template text as part of the compiled template; each template will be correspondingly larger. TODO Should this be on by default?

HoganAssets::Config.configure do |config|
  config.lambda_support = true
end

Path Prefix

You can strip a prefix from your template names. For example, when using Rails, if you place your templates in app/assets/javascripts/app/templates and organize them like Rails views (i.e. posts/index.mustache); then the index.mustache template gets compiled into:

HoganTemplates['app/templates/posts/index']

You can strip the common part of the template name by setting the path_prefix option. For example:

HoganAssets::Config.configure do |config|
  config.path_prefix = 'app/templates'
end

will give you a compiled template:

HoganTemplates['posts/index']

TODO Can this be done in a nicer way?

Template namespace

You can change the namespace for the generated templates. By default, the namespace is HoganTemplates. To change it, use the template_namespace option. For example:

HoganAssets::Config.configure do |config|
  config.template_namespace = 'JST'
end

Template Extensions

By default, templates are recognized if they have an extension of .mustache (and if you have haml available, .hamstache.) You can change the template extensions by setting the template_extensions configuration option in an initializer:

HoganAssets::Config.configure do |config|
  config.template_extensions = %w(mustache hamstache stache)
end

Usage

Templates are compiled to a global JavaScript object named HoganTemplates. To render pages/person:

HoganTemplates['templates/pages/person'].render(context, partials);

Maintainer

This gem is maintained by Alex Riedler Github.

Author

Les Hill, follow me on Github and Twitter.

Contributors

  • @mdavidn (Matthew Nelson) : Remove unnecessary template source
  • @ajacksified (Jack Lawson) : Configurable file extension
  • @mikesmullin (Mike Smullin) : hamstache support
  • @gleuch (Greg Leuch) : Mustache lambdas
  • @lautis (Ville Lautanala) : hamstache fix
  • @adamstrickland (Adam Strickland) : Custom template namespace
  • @lautis (Ville Lautanala) : haml_options configuration
  • @sars (Rodion) : slimstache support
  • @apai4 : YAML configuration
  • @AlexRiedler (Alex Riedler) : hamstache/slimstache extensions and helper support

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Added some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

hogan_assets's People

Contributors

adamstrickland avatar ajacksified avatar apai4 avatar gleuch avatar lautis avatar leshill avatar mikesmullin avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

hogan_assets's Issues

lambda appears don't works

http://jsfiddle.net/czuxH/320/

simple example works on hogan.js site documentation, but, on hogan_asset, not true.

var template = "{{#wrapped}} {{name}} is awesome.{{/wrapped}} ";
var data = {
  "name": "Willy",
  "wrapped": function(context) {
    return "<b>"+context+"</b>";
  }
};

var hello = Hogan.compile(template);
console.log(hello.render(data));
$('#content').html(hello.render(data));

HTML contents converted to string?

Hi,

I am using hogan with Fruitmachine.js,

Sample senario, Here i define modules as

var Masthead = fruitmachine.define({
name: 'masthead',
template: templateMasthead,
});

var templateMasthead = Hogan.compile("{{title}}");

Now i provide json data for fruitmachine as

var layout = {
module: 'masthead',
model: {
title: '

Article viewer

Main name

'
}
};

var view = fruitmachine(layout);
view
.render()
.inject(document.getElementById('app'))

when i inject view to div, it displays html tags along with string values?

How to handle these kind of situations?
(json response from server contains html tags)

Thank you,

Setting template_namespace not working

Hi I am having issues setting the template_namespace via the initializer it doesn't seem to be working, it keeps falling back to HoganTemplates instead of the one I set.

move to views?

Is it possible to grab the mustache templates from the views folder?

I'm trying to make it so rails can use the views too, by doing:

module MustacheTemplateHandler
  def self.call(template)
    "Mustache.render(#{template.source.inspect}, Yajl::Encoder.encode(mustache)).html_safe"
  end
end
ActionView::Template.register_template_handler(:mustache, MustacheTemplateHandler)

lambdas and precompiled templates

I can get lambdas to working with the precompiled templates (but do work if compiled on the client site).

Maybe there is a work around? Anyone else having an issue?

hogan_assets stopped working in Rails when upgrading sprockets-rails to latest version (3.0)

Ever since the most recent release of sprockets-rails 3.0 in December, hogan_assets is currently not working properly in Rails. In my case, all my Javascript calls to HoganTemplates are now undefined. I did a bit of digging and it seems like the updated version of Sprockets has plenty of changes, and now the hogan_assets gem is not registering the .mustache / .slimstache extensions when it initializes.

In particular, in lib/hogan_assets/engine.rb, after defining the initializer, there's a there's a check to see if app.assets is defined. In previous versions of Sprockets, this was set up, but with the latest version, it's now nil. I don't know a much about Sprockets, but this issue seems to explain why: rails/sprockets-rails#292

Out of curiosity, I cloned hogan_assets and modified the initializer based on what was mentioned in the issue I linked to as so:

module HoganAssets
  class Engine < ::Rails::Engine
    initializer "sprockets.hogan", :group => :all do |app|
      HoganAssets::Config.load_yml! if HoganAssets::Config.yml_exists?
      Rails.application.config.assets.configure do |env|
        HoganAssets::Config.template_extensions.each do |ext|
          env.register_engine(".#{ext}", Tilt)
        end
      end 
    end 
  end 
end

When I did this change, everything worked as expected - the Slimstache files were being properly registered and included in my app as it was before.

I was going to submit a pull request, but as I mentioned, I don't know much about Sprockets, so I don't know if this has adverse effects outside of my use case. Let me know if I can help with that somehow.

Full path name required in HoganTemplates object

I know there was a ticket before about this that seemed to be resolved, but for some reason, I have to provide the full pathname to my templates:

HoganTemplates['templates/home/index'].render()

Which renders the template at app/assets/javascripts/templates/home/index.mustache

My application.js file seems fine:

//= require hogan
//= require_tree ./templates
//= require_self

Am I missing something?

HoganTemplates is missing

I'm using Rails 4. I added the hogan_assets gem to my Gemfile, required hogan in my application.js manifest. When I render the page and view the source, I confirmed that hogan.js is included on the page. The directories that contain my templates are required in my application.js manifest as well. I have tried the .mustache, .hamstache, .mustache.haml extensions (with and without the haml_assets gem) It doesn't appear to be working.

When I render the page, it says that HoganTemplates is missing. I confirmed this through the console. I also noticed that none of my templates are included anywhere in the page, either referenced with script tags or otherwise. I think I have followed the instructions in the README pretty closely. Am I missing something?

full template path included in compiled template

Documentation says:

Templates are named for the sub-path below require_tree. For example, the file app/assets/javascripts/templates/pages/person.mustache will be named pages/person.

But the template gets named HoganTemplate["templates/pages/person"], would be nice to skip the "template" part

I'm including the templates from my app/asssets/javascripts/application.js with:
//= require_tree templates
the templates are located in app/asssets/javascripts/templates

Other than that: Thanks for a great tool!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.