Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7691569
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T20:39:19+00:00 2026-05-31T20:39:19+00:00

I have an options object in my CS class, and I’d like to keep

  • 0

I have an options object in my CS class, and I’d like to keep some templates in it:

class MyClass
    options:
        templates:
            list: "<ul class='#{ foo }'></ul>"
            listItem: "<li>#{ foo + bar }</li>"
            # etc...

Then I’d like to interpolate these strings later in the code… But of course these are compiled to "<ul class='" + foo +"'></ul>", and foo is undefined.

Is there an official CoffeeScript way to do this at run-time using .replace()?


Edit: I ended up writing a little utility to help:

# interpolate a string to replace {{ placeholder }} keys with passed object values
String::interp = (values)->
    @replace /{{ (\w*) }}/g,
        (ph, key)->
            values[key] or ''

So my options now look like:

templates:
    list: '<ul class="{{ foo }}"></ul>'
    listItem: '<li>{{ baz }}</li>'

And then later in the code:

template = @options.templates.listItem.interp
    baz: foo + bar
myList.append $(template)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T20:39:21+00:00Added an answer on May 31, 2026 at 8:39 pm

    I’d say, if you need delayed evaluation, then they should probably be defined as functions.

    Perhaps taking the values individually:

    templates:
        list: (foo) -> "<ul class='#{ foo }'></ul>"
        listItem: (foo, bar) -> "<li>#{ foo + bar }</li>"
    

    Or from a context object:

    templates:
        list: (context) -> "<ul class='#{ context.foo }'></ul>"
        listItem: (context) -> "<li>#{ context.foo + context.bar }</li>"
    

    Given your now-former comments, you could use the 2nd example above like so:

    $(options.templates.listItem foo: "foo", bar: "bar").appendTo 'body'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an object now: class Items attr_accessor :item_id, :name, :description, :rating def initialize(options
I have a list of options in a data object, and I want to
I have writen options to a <select> using something like Id.innerHTML = <option value='foo'>Foo</option>;
Let's say I have the following class class Parent(object): Options = { 'option1': 'value1',
I have a class object System.Windows.Documents.Run, I need it to copy. There are options
I have some options for pricing using radio buttons. When a user selects the
so with this class i have public class Options { public bool show {
I'd like to create a class called Object to be able to implement toString(),
I am binding a list of objects to a select using knockout. Object Class
I have a command object associated with a spring form controller: public class PluginInstance

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.