Using a map of keywords is a good solution, but I think it is somewhat
just boilerplate code to satisfy the compiler, not to satisfy
developers (yes, human). In view we have to write (:my-var map)
everywhere. Is there a better way, something like lazy variable
binding or lazy function binding?

To repair the Ruby code, instance variable or method can be used:

def my_view
  "hello #...@name}"
end
@name = "bilbo"
puts my_view

Or
def my_view
  "hello #{name}"
end
def name "bilbo" end
puts my_view

Ruby does not force @name or name in my_view to exist before hand,
this is really dynamic.

Well, the idea is I want to create a web framework based on Compojure
with as less code as possible in view (may be designers would like it,
it's not normal HTML anyway). Place-holders in view should be lazy,
then when an action wants to use a view, it just has to declare a
bunch of concrete values and include the desired view, something like:

(defn my-action []
  (with-variables [foo "hello", bar "world"] my-view))

The Clojure code may be incorrect, but I think you have captured the
idea.


On Sep 3, 10:32 pm, tmountain <tinymount...@gmail.com> wrote:
> I believe the way this works in rails has to do with the order in
> which variables are resolved. In this case, @name is an instance
> variable that's already been assigned elsewhere (your controller).
> Rails loads the view after the controller class has been instantiated.
> For this to work, the view is probably "mixed-into" the controller
> class somehow to ensure that the instance variable is visible. If you
> try this with a standard ruby function, it doesn't work.
>
> def my_view
>   "hello #{name}"
> end
>
> name = "bilbo"
> puts my_view()
> `my_view': undefined local variable or method `name' for main:Object
> (NameError)
>
> You could obviously use a global variable, but that would be really
> nasty. With Clojure, your function can see the variable so long as
> it's defined ahead of time.
>
> (def my-name "bilbo") ; name is a reserved word
>
> (defn my-view []
>   (str "hello " my-name))
> (my-view)
> "hello bilbo"
>
> The my-name variable doesn't even have to be bound in this case, just
> created. I think what you're really looking for though is a way to
> define a function with no arguments referencing a variable that isn't
> defined at all. I'm not sure how you would accomplish that.
>
> -Travis
>
> On Sep 3, 8:42 am, ngocdaothanh <ngocdaoth...@gmail.com> wrote:
>
>
>
>
>
> > Hi,
>
> > In Rails you can create a view like this:
>
> > my_view.erb:
> > <%= "hello" + @name %>
>
> > I'm new to Clojure. I want to create something like the above like
> > this:
>
> > (defn my-view []
> >   (str "hello" name))
>
> > The point is:
> > * name is not known when my-view is defined
> > * I don't want to pass name as a argument of my-view, this is kind of
> > ugly boilerplate
>
> > How can I implement this feature in Clojure?
>
> > Thanks.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to