5

I am working on a web project. (Absolutely no previous experience). This project requires retrieving lot of content from the server dynamically(Like on the click of button). Now, the problem is, I have to display the content in some neat, formatted way. Like, collapsible lists, hyperlinks etc. I am currently using JavaScript for this purpose. Like this:

li = document.createElement("li");
li.innerHTML = "some_content";

I also need to add appropriate classes to those dynamically created elements in JavaScript as per the requirements of Bootstrap. But the code really looks very messy now. Are there any alternative solutions for avoiding all the dynamic creation of elements in JS, formatting etc.?

3
  • 2
    Sounds like you're looking for a templating engine (perhaps among other things)? Maybe take a look at this page for choosing between various templating options: garann.github.io/template-chooser Commented Mar 30, 2015 at 0:02
  • If you're targeting modern browser only, try HTML Templates developer.mozilla.org/en-US/docs/Web/HTML/Element/template Commented Mar 31, 2015 at 4:09
  • You could just use the jQuery way since you're using bootstrap: $("<li>") Commented Mar 31, 2015 at 4:45

4 Answers 4

4

Seems like a perfect scenario for a knockoutJS solution. I am just posting a sample code here from their live example.

you just create the template like this -

<p>First name: <input data-bind="value: firstName" /></p>
<p>Last name: <input data-bind="value: lastName" /></p>
<h2>Hello, <span data-bind="text: fullName"> </span>!</h2>

(notice the bindings in data-bind)

and create the view model and then apply it -

// Here's my data model
var ViewModel = function(first, last) {
    this.firstName = ko.observable(first);
    this.lastName = ko.observable(last);

    this.fullName = ko.pureComputed(function() {
        // Knockout tracks dependencies automatically. It knows that fullName depends on firstName and lastName, because these get called when evaluating fullName.
        return this.firstName() + " " + this.lastName();
    }, this);
};

ko.applyBindings(new ViewModel("Planet", "Earth")); // This makes Knockout get to work

and you are good to go with this output, only a matter of minutes.enter image description here

I created a Survey Builder page with it, the most important part is knockout is fast and also supports all kinds of js libraries to work with like jQuery, AngularJS, etc. You will never need to worry about rendering ui, just get/set with data.

Try it out here - http://jsfiddle.net/rniemeyer/LkqTU/

Sign up to request clarification or add additional context in comments.

1 Comment

Even knockout is overkill for the problem he's describing.
1

You can try some front end framework. eg: AngularJS (As mentioned in previous comment) It has built in template engine. (As mentioned in comment ) so your code would like following if you use angular

<ul>
  <li ng-repeat="user in users">{{user.name}}</li>
</ul>

and your json might be

{
  "users":[
     {
        "name":"jack",
        "age":18
     },
     {
        "name":"jimmy",
        "age":19
     }
  ]
}

the html user see will be

<ul>
  <li>jack</li>
  <li>jimmy</li>
</ul>

I didn't write angular's controller code,so the above code is just for demonstration, you can take a look at its doc and tutorials.

Angular is a MV* front end framework, it is really useful when you have a lot of data to display in the front end.(but too many data will cause performance issue because it use two-way data-binding, try bindonce)

If you don't want a framework, you can try mustache

1 Comment

In newer versions of angularjs, bindonce functionality is baked into the framework. As far as I understand it, pasvaz's module is no longer necessary.
1

You can use a templating solution like underscore templates, Handlebars, or (the best) JADE. Each of these can be used with a larger framework like angular or ember, but also work as standalone templating solutions.

Since you're using bootstrap, you're also using jQuery.

With jQuery, you can add elements, update their content and add classes like this:

var $li = $("<li>") // create the li element
    .text("My Text") // add a text node
    .addClass("class1 class2 class3") // add some classes
    .appendTo($("ul#myList")); // insert into your ul

the following is also allowed if you prefer a one-liner:

var $li = $("<li class='class1 class2 class3'>My Text</li>")
    .appendTo($("ul#myList"));

both of the above take this:

<ul id="myList"></ul>

and turn it into this:

<ul id="myList"><li class='class1 class2 class3'>My Text</li></ul>

1 Comment

It's not a bad idea. They do different things.
-1

Edit:

It's been a while, but I found Gatsy pretty easy to get up and running with. I've used it for a couple of projects.


Angular is another option, depending on how complex the site is (supported by google).

Ember is also a great option.

Depending on your skill sets, and the type of site this is, PHP could be an easy alternative for you as well.

2 Comments

These frameworks are OVERKILL for simple templating.
I agree ... "depending on how complex the site is".

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.