1

Say I want to print out the numbers 0 to 4 on an HTML page. How would I change the following code to do it:

<ul> {%for i in range(5) %} <li><a>i</a></li> {%endfor %} </ul> 
1
  • 3
    first you'd have to explain what templating system you'd use. but note that your i is just a plaintext i. If you want it to be treated as your loop variable, you'll have to mark it as a variable - otherwise the i in <li> would also become <l1>, <l2> etc... Commented Dec 3, 2015 at 18:45

1 Answer 1

1

If you have a serverside language, say, PHP:

<ul> <?php for ($i = 0; $i < 5; $i++): ?> <li><a><?=$i?></a></li> <?php endfor; ?> </ul> 

Or, Django:

<ul> {% for i in 5|get_range %} <li>{{ i }}</li> {% endfor %} </ul> 

Otherwise, you'll need to do it after the page loaded, with javascript, here's a jsfiddle.

<ul id="dynamicList"></ul> <script type="text/javascript"> (function() { var list = document.getElementById('dynamicList'); for (var i = 0; i < 5; i++) { var li = document.createElement('li'); li.innerHTML = i; list.appendChild(li); } })(); </script> 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.