Template language similar to examples...

Discussion in 'Ruby' started by Edgard Riba, Feb 1, 2006.

  1. Edgard Riba

    Edgard Riba Guest

    Hi,

    I would like to process a text file which contains property-tokens which
    are replaced appropiately by the template processing library.
    Property-tokens can be single value, or can be collection of values.

    The format I would like to use is something like this (the specific
    characters used to signal might be different):

    #!This is a comment
    #DECLARE(%myProperty) #!Here I declare a property (variable) for
    #!use within the template
    #DECLARE(%myCollection),MULTI #!This would be an array collection
    #SET(%mySymbol,1) #!assign a value of 1
    #ADD(%myCollection,'A') #!add something to the collection
    #ADD(%myCollection,'B') #!add something to the collection
    #ADD(%myCollection,'C') #!add something to the collection

    #!The code that goes below goes straight into the output,
    #!except that it takes into account how the #IF is evaluated.
    class myTestClass
    def initialize
    #IF(%mySymbol = 1)
    @msg = 'Test1'
    #ELSE
    @msg = 'Test2'
    #ENDIF
    #INSERT(%myTemplateProcedure,%myCollection)
    end
    end

    #GROUP(%myTemplateProcedure,%pCollection)
    #!For this example, the code below generates:
    #! @msgA = 'A'
    #! @msgB = 'B'
    #! @msgC = 'C'
    #!Not very useful, but serves as an example of the things I'm looking
    for.
    #FOR(%myCollection)
    @msg%myCollection = '%myCollection'
    #ENDFOR


    The output would be:

    class myTestClass
    def initialize
    @msg = 'Test1'
    @msgA = 'A'
    @msgB = 'B'
    @msgC = 'C'
    end
    end

    Are any of you guys familiar with a library that will do something
    similar to the above?

    Thanks,
    Edgard

    --
    Posted via http://www.ruby-forum.com/.
     
    Edgard Riba, Feb 1, 2006
    #1
    1. Advertising

  2. Edgard Riba

    Ross Bamford Guest

    (Apologies if this is a dupe, messages that cross the gateway seem to
    have messed up headers - replies don't go to the list?)

    On Thu, 2006-02-02 at 05:32 +0900, Edgard Riba wrote:

    > #!This is a comment
    > #DECLARE(%myProperty) #!Here I declare a property (variable) for
    > #!use within the template
    > #DECLARE(%myCollection),MULTI #!This would be an array collection
    > #SET(%mySymbol,1) #!assign a value of 1
    > #ADD(%myCollection,'A') #!add something to the collection
    > #ADD(%myCollection,'B') #!add something to the collection
    > #ADD(%myCollection,'C') #!add something to the collection
    >
    > #!The code that goes below goes straight into the output,
    > #!except that it takes into account how the #IF is evaluated.
    > class myTestClass
    > def initialize
    > #IF(%mySymbol = 1)
    > @msg = 'Test1'
    > #ELSE
    > @msg = 'Test2'
    > #ENDIF
    > #INSERT(%myTemplateProcedure,%myCollection)
    > end
    > end
    >
    > #GROUP(%myTemplateProcedure,%pCollection)
    > #!For this example, the code below generates:
    > #! @msgA = 'A'
    > #! @msgB = 'B'
    > #! @msgC = 'C'
    > #!Not very useful, but serves as an example of the things I'm looking
    > for.
    > #FOR(%myCollection)
    > @msg%myCollection = '%myCollection'
    > #ENDFOR
    >
    >
    > The output would be:
    >
    > class myTestClass
    > def initialize
    > @msg = 'Test1'
    > @msgA = 'A'
    > @msgB = 'B'
    > @msgC = 'C'
    > end
    > end
    >
    > Are any of you guys familiar with a library that will do something
    > similar to the above?


    Wow. Couldn't you just use ERB?

    require 'erb'

    my_symbol = 1
    my_collection = ['A', 'B', 'C']
    my_template_proc = lambda do |coll|
    coll.map { |a| "@msg#{a}" }.join(', ') + " = #{coll.inspect}"
    end

    e = ERB.new <<-EOF
    class MyTestClass
    def initialize
    <% if my_symbol %>
    @msg = 'Test1'
    <% else %>
    @msg = 'Test2'
    <% end %>
    <%= my_template_proc.call(my_collection) %>
    end
    end
    EOF

    puts e.result

    --
    Ross Bamford -
     
    Ross Bamford, Feb 1, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. luvsat
    Replies:
    5
    Views:
    349
  2. Replies:
    3
    Views:
    307
    Simon Brunning
    Jun 6, 2009
  3. Chris Rebert
    Replies:
    1
    Views:
    378
    John Machin
    Jun 6, 2009
  4. Nando Sanchez
    Replies:
    5
    Views:
    170
    M. Edward (Ed) Borasky
    Mar 19, 2007
  5. jkn
    Replies:
    8
    Views:
    169
    Chris Angelico
    Jan 13, 2013
Loading...

Share This Page