Code Monkey home page Code Monkey logo

Comments (5)

rtomayko avatar rtomayko commented on May 25, 2024

ERB, Haml, Builder all support separate scope and locals and both are used heavily in typical Sinatra apps. They're definitely treated as different things in most web frameworks I have experience with. It's true that Liquid and Mustache have single context objects that bring scope/locals under a single abstraction (I like this approach, personally), but I don't see any way to change the interface and support the more prevalent case.

from tilt.

 avatar commented on May 25, 2024

I don't think ERB and Builder support both natively. Looks like Tilt is adding the locals to the scope. But yes, I see that Haml does take both --in fact it looks like Tilt's #render method was patterned after Haml's?

Yet if you look at the Haml code, all it is doing is converting the scope to a Binding and then adding the locals to the binding via:

def set_locals(locals, scope, scope_object)
  scope_object.send(:instance_variable_set, '@_haml_locals', locals)
  set_locals = locals.keys.map { |k| "#{k} = @_haml_locals[#{k.inspect}]" }.join("\n")
  eval(set_locals, scope)
end

See http://github.com/nex3/haml/blob/master/lib/haml/engine.rb#L165

I'm inclined to simplify this procedure and make it a general extension, something like:

object.to_binding(locals)

So I'm not sure if Tilt needs to cater to the greatest common multiple --Haml is the one outlay here. Consider, I originally had my template engine accept any number of scopes and locals and it effectively merged them together for rendering, but in the end I realized it was just overkill. If my end user need to merge different data sources, it is better that I just give them to the tools to easily do so before passing it into my template engine --that gives them the most control.

Anyway, that's just my "thinking aloud" on the subject.

(Sorry, I may have double posted this.)

from tilt.

rtomayko avatar rtomayko commented on May 25, 2024

ERB and Builder don't support locals natively but frameworks like Sinatra and Rails have layered that on top and benefit from having an interface that provides that abstraction.

That being said, this object.to_binding(locals) concept is really compelling. If you can find some way of 1.) providing a #render interface that takes a single scope/locals argument, and 2.) is backward compatible with the current interface, I'd certainly be interested in getting it merged in.

from tilt.

 avatar commented on May 25, 2024

I think #1 and #2 are mutually exclusive. If you were ever to do it, it would be "2.0" type deal. It's not worth breaking backward compatibility at this point (perhaps never, although I suspect most use cases wouldn't be effected). However I do think it is worth using something along the lines of object.to_binding(locals) as well as a function for the opposite effect, converting a scope to locals, under the hood, so that there is a uniform bases that all the templates can use for doing so.

I'll make a example implementation for you to consider. In the meantime, a reminder that I sent a pull request for fixing the RadiusTemplate.

from tilt.

trans avatar trans commented on May 25, 2024

I an running into issues with Tilt where different engines are not giving consistent results. Depending on the type of scope/locals being fed in: Binding, Struct, OpenStruct, Object, and/or Hash Locals, different engine renders differently for equivalent templates. To remedy I started writing some Cucumber features, and realized that there were at least 9 variations to account for: Binding, Struct, OpenStruct, Object, Locals, Binding/Locals, Struct/Locals, OpenStruct/Locals and Object/Locals. Not sure how Hash or Hash/Locals should behave. And hopefully I haven't missed any!

I am working on a project that uses Tilt extensively. I've decided that it's interface will only allow a single argument.

from tilt.

Related Issues (20)

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.