Display length of my Dictionary

I am using Swift and Leaf Template to build a website and I am trying to render some cards based on the length of the array. I am stuck here. Someone should please help me out.

 func handleArticleList(_ req: Request) -> EventLoopFuture<View> {
        var articles = [ArticleList]()
        // Populate the articles array with some data
        let article1 = ArticleList(articleName: "Article 1", articleImage: "image1.jpg", articleDescription: "Description of Article 1")
        let article2 = ArticleList(articleName: "Article 2", articleImage: "image2.jpg", articleDescription: "Description of Article 2")
        let article3 = ArticleList(articleName: "Article 3", articleImage: "image3.jpg", articleDescription: "Description of Article 3")

        articles.append(article1)
        articles.append(article2)
        articles.append(article3)
        
        for article in articles {
            print("Article Name: \(article)")
//            print("Article Image: \(article.articleImage)")
//            print("Article Description: \(article.articleDescription)")
            print("---")
        }
        print("value: \(articles.count)")
        // Convert the array of ArticleList objects into an array of dictionaries
        let articleDictionaries = articles.map { article in
            return [
                "articleName": article.articleName,
                "articleImage": article.articleImage,
                "articleDescription": article.articleDescription
                
            ]
        }
        
        
//        print(articleDictionaries)
        
        let context: [String: Any] = [
            "articles": articles,
            "value": articles.count
        ]
//        print("CONTEXT : \(context.count)")

         return req.view.render("index", context )
    }

  }


struct ArticleList: Encodable {
    var articleName: String
    var articleImage: String
    var articleDescription: String
}

My Leaf Template

<main>
        //bunch of tailwind and HTML
           
            #for(planet in articles):
            <li>
                <h2>#(planet.articleName)</h2>
                
            </li>
            #endfor
            <p>#(articles.count)</p>
            </div>
           
        </div>
      </main>

Accepted Reply

length of my Dictionary or length of the array

Please be clear and tell which dictionary or which array.

This will give you the number of items (3) in the array of dictionaries:

print(articleDictionaries.count)

What else are you looking for ?

Replies

I am stuck here.

Stuck how? What’s the actual problem?

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

length of my Dictionary or length of the array

Please be clear and tell which dictionary or which array.

This will give you the number of items (3) in the array of dictionaries:

print(articleDictionaries.count)

What else are you looking for ?

@Claude31

I am still a rookie using Swift, so I couldn't really express myself properly.

I am building a website with using leaf template and Swift and I wanted to display some cards using the length of the dictionary. So to do that, I created a struct like below:


struct ArticleList: Encodable {

    var articleName: String

    var articleImage: String

    var articleDescription: String

}


I used an Encodable because the function req.view.render("index", context ) accepts an Encodable context.

So I created an array of ArticleList object (the values would be used to populate the card).

Furthermore, the req.view.render("index", context ) function can only accept a dictionary and so I had to convert my array of object using the map function like below:


  var articles = [ArticleList]()

        // Populate the articles array with some data

        let article1 = ArticleList(articleName: "Article 1", articleImage: "image1.jpg", articleDescription: "Description of Article 1")

        let article2 = ArticleList(articleName: "Article 2", articleImage: "image2.jpg", articleDescription: "Description of Article 2")

        let article3 = ArticleList(articleName: "Article 3", articleImage: "image3.jpg", articleDescription: "Description of Article 3")



        articles.append(article1)

        articles.append(article2)

        articles.append(article3)

        

//  

        print("value: \(articles.count)")

        // Convert the array of ArticleList objects into an array of dictionaries

        let articleDictionaries = articles.map { article in

            return [

                "articleName": article.articleName,

                "articleImage": article.articleImage,

                "articleDescription": article.articleDescription

                

            ]

        }

So I passed the articleDictionaries to a new variable and passed it to the render function and I used the leaf tag to access the list like below:


  //HTML file

#for(planet in articles):

            <li>

                <h2>#(planet.articleName)</h2>

                

            </li>

            #endfor



The above code works and returns articleNames but I tried passing the count as another value to the dictionary, but it throws an error.

See the image below:

So trying to get this value and pass it to the render function is the problem I am currently having.