added possibility to template i count

This commit is contained in:
arnaucode
2017-05-31 14:24:56 +02:00
parent 08b773e666
commit d6c359c1f4
4 changed files with 17 additions and 15 deletions

View File

@@ -3,7 +3,7 @@
web templating engine for static websites, written in Go lang web templating engine for static websites, written in Go lang
![konstrui](https://raw.githubusercontent.com/arnaucode/konstrui/master/konstrui.png "konstrui") ![konstrui](https://raw.githubusercontent.com/arnaucode/konstrui/master/media/konstrui.png "konstrui")
## Example ## Example
@@ -41,9 +41,9 @@ webInput/
- Set the template file: - Set the template file:
```html ```html
<div class="class1"> <div class="class1" id="user[[i]]">
<div class="class2">{{username}}</div> <div id="username[[i]]" class="class2">{{username}}</div>
<div class="class2">{{description}}</div> <div id="description[[i]]" class="class2">{{description}}</div>
<div class="class2">{{phone}}</div> <div class="class2">{{phone}}</div>
</div> </div>
``` ```
@@ -99,21 +99,21 @@ webInput/
<html> <html>
<body> <body>
<h1>My First Heading</h1> <h1>Heading</h1>
<p>My first paragraph.</p> <p>Paragraph.</p>
<div class="class1"> <div class="class1" id="user0">
<div class="class2">Michaela Doe</div> <div id="username0" class="class2">Michaela Doe</div>
<div class="class2">Hi, I'm here to code</div> <div id="description0" class="class2">Hi, I'm here to code</div>
<div class="class2">456456456</div> <div class="class2">456456456</div>
</div> </div>
<div class="class1"> <div class="class1" id="user1">
<div class="class2">John Doe</div> <div id="username1" class="class2">John Doe</div>
<div class="class2">Hi, I'm here</div> <div id="description1" class="class2">Hi, I'm here</div>
<div class="class2">123456789</div> <div class="class2">123456789</div>
</div> </div>
<div class="class1"> <div class="class1" id="user2">
<div class="class2">Myself</div> <div id="username2" class="class2">Myself</div>
<div class="class2">How are you</div> <div id="description2" class="class2">How are you</div>
<div class="class2">no phone</div> <div class="class2">no phone</div>
</div> </div>
</body> </body>

View File

@@ -5,6 +5,7 @@ import (
"encoding/json" "encoding/json"
"io/ioutil" "io/ioutil"
"os" "os"
"strconv"
"strings" "strings"
) )
@@ -50,6 +51,7 @@ func generateFromTemplateAndData(templateContent string, entries []dataEntry) st
//now, replace the keys with the values //now, replace the keys with the values
for j := 0; j < len(keys); j++ { for j := 0; j < len(keys); j++ {
entryContent = strings.Replace(entryContent, "{{"+keys[j]+"}}", entries[i][keys[j]], -1) entryContent = strings.Replace(entryContent, "{{"+keys[j]+"}}", entries[i][keys[j]], -1)
entryContent = strings.Replace(entryContent, "[[i]]", strconv.Itoa(i), -1)
} }
newContent = newContent + entryContent newContent = newContent + entryContent

View File

Before

Width:  |  Height:  |  Size: 110 KiB

After

Width:  |  Height:  |  Size: 110 KiB

View File

Before

Width:  |  Height:  |  Size: 72 KiB

After

Width:  |  Height:  |  Size: 72 KiB