DEVHIDE
        • Home (current)
        • About
        • Contact
        • Cookie
        • Home (current)
        • About
        • Contact
        • Cookie
        • Disclaimer
        • Privacy
        • TOS
        Login Or Sign up

        Including the result of calling a route inside a view in Play

        69 Views Asked by nTn At 02 January 2023 at 11:07 2025-12-22T16:44:23.346000

        I have this view that displays a forum post:

        
        @(answers: Set[Answer])(implicit request: RequestHeader, flash : Flash)
        {
        <div class="row">
            <ul class="list-group list-group-flush align-items-center">
        
                @for(answer <- answers) {
        
                <li class="list-group-item bg-dark">
                    <div class="card border" style="width: 40rem;" [email protected]>
        
                        <div class="card-body">
                            <div class="row d-flex align-items-center">
                                @if(request.session.get("loggedIn").isDefined) {
                                <div class="col-sm-auto">
                                    <div class="row">
        
                                        <svg class="btn btn-outline-success btn-sm" onclick="location.href='/post/@answer.id/upvote'" xmlns="http://www.w3.org/2000/svg" viewBox="-2 -2 24 24" width="24" fill="currentColor"><path d="M4 0h12a4 4 0 0 1 4 4v12a4 4 0 0 1-4 4H4a4 4 0 0 1-4-4V4a4 4 0 0 1 4-4zm0 2a2 2 0 0 0-2 2v12a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V4a2 2 0 0 0-2-2H4zm6 7.172l-3.536 3.535a1 1 0 1 1-1.414-1.414L9.293 7.05a1 1 0 0 1 1.414 0l4.243 4.243a1 1 0 0 1-1.414 1.414L10 9.172z"></path></svg>
                                    </div>
                                    <div class="row">
                                        <h4 class="container d-flex justify-content-center">@answer.votes</h4>
                                    </div>
                                    <div class="row">
                                        <svg class="btn btn-outline-danger btn-sm" onclick="location.href='/post/@answer.id/downvote'" xmlns="http://www.w3.org/2000/svg" viewBox="-2 -2 24 24" width="24" fill="currentColor"><path d="M4 0h12a4 4 0 0 1 4 4v12a4 4 0 0 1-4 4H4a4 4 0 0 1-4-4V4a4 4 0 0 1 4-4zm0 2a2 2 0 0 0-2 2v12a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V4a2 2 0 0 0-2-2H4zm6 9.828l3.536-3.535a1 1 0 0 1 1.414 1.414l-4.243 4.243a1 1 0 0 1-1.414 0L5.05 9.707a1 1 0 0 1 1.414-1.414L10 11.828z"></path></svg>
                                    </div>
                                </div>
                                }
                                <div class="col-11 ">
                                    <div class="card-text d-flex justify-content-end">
                                        @answer.date
                                    </div>
                                    <h6 class="card-text">@answer.user</h6>
                                    <h3 class="card-title">@answer.title</h3>
                                    <p class="card-text">@answer.text</p>
                                    <div class=" container bg-light">
                                        <pre><code >@answer.code</code></pre>
                                    </div>
        
                                </div>
                            </div>
                            <div class="row">
                                <div class="col-10 col-xs-9 col-xs-offset-10"></div>
                                <div class="col-md-auto container d-flex justify-content-end">
                                    <a href="/post/@answer.postId" class="btn btn-secondary d-flex justify-content-end">
                                        <svg xmlns="http://www.w3.org/2000/svg" viewBox="-6 -2 24 24" width="24" fill="currentColor"><path d="M5 16.573V3.419L2.464 5.954A1 1 0 0 1 1.05 4.54L5.293.297a1 1 0 0 1 1.414 0L10.95 4.54a1 1 0 1 1-1.414 1.414L7 3.42v13.154l2.536-2.536a1 1 0 1 1 1.414 1.414l-4.243 4.243a.997.997 0 0 1-1.414 0L1.05 15.451a1 1 0 1 1 1.414-1.414L5 16.573z"></path></svg>
                                    </a>
                                </div>
        
                            </div>
                        </div>
                    </div>
                </li>
                @routes.Posts.answerComments(answer.id)
                }
            </ul>
        </div>
        }
        

        At the end, I want to show the comments to this post. I have a controller route set up to return the HTML of these comments. Ìs it possible to call a route and include the result of this request as html from within a view? I've tried using the @Html method, but that only formats HTML code, it does not call a route. I read somewhere about using the wAction method to call a route and include the result, but that does not seem to exist in the current Play version. I can't directly call another view because I need to fetch the comments from the server, and for this I have the controller action.

        scala model-view-controller playframework twirl
        Original Q&A
        1

        There are 1 best solutions below

        0
        Gaël J Gaël J On 02 January 2023 at 18:24

        To achieve that (i.e. render everything server side), what makes more sense is to:

        • have a common template (.scala.html) for displaying a list of comments
        • use this common template in your current HTML that you've shown to display a forum post, thus having comments as an input of the HTML (maybe as a Map if fits better your need)
        • in your controller, have a common Scala method to load comments and reuse this method in both routes

        Technically, you could also call the route to display comments from your controller and get that as a String that you'd inject in the main HTML but that would not be idiomatic at all.

        Or, move to a more dynamic approach with JavaScript and load comments HTML when the page is rendered in the browser.

        Related Questions in SCALA

        • Mocking AmazonS3 listObjects function in scala
        • Last SPARK Task taking forever to complete
        • How to upload a native scala project to local repo by sbt like using "maven install"
        • Folding a list of OR clauses in io.getquill
        • How to get latest modified file using scala from a folder in HDFS
        • Enforce type bound for inferred type parameter in pattern matching
        • can't write pyspark dataframe to parquet file on windows
        • spark streaming and kafka integration dependency problem
        • how to generate fresh singleton literal type in scala using macros
        • exception during macro expansion: type T is not a class, play json
        • Is there any benefit of converting a List to a LazyList in Scala?
        • Get all records within a window in spark structured streaming
        • sbt publishLocal of a project with provided dependencies in build.sbt doesn't make these dependencies visible to projects using the project as library
        • Scala composition of partially-applied functions
        • How to read the input json using a schema file and populate default value if column not being found in scala?

        Related Questions in MODEL-VIEW-CONTROLLER

        • Getting POST 500 Internal server error while sending request via ajax call
        • ASP.NET Core - Change Url of Product Details when select Color
        • How to execute functions from different classes in a Model-View-Controller (MVC) architecture pattern?
        • How to reroute role based user after login
        • Error Connecting to a remote Windows MYSQL Windows server using Windows ASP.NET
        • InvalidOperationException: Unable to resolve service for type 'Microsoft.Identity.Abstractions.IDownstreamApi'
        • Create REST API Endpoints from an Existing PHP CodeIgniter Project
        • Issue with Accessing Endpoints after Separating Controller Classes in Spring Boot
        • C# MVC net.7 Application goes in time out
        • In my .net core 8 mvc c# project, when I click on the category, the data comes as null listing the products in the relevant category
        • Why do I get 500 error on Azure after using ViewBag?
        • The jquery script function is not triggering in _layout.cshtml page while child page is loaded in ajax
        • How to structure frontend on a Vanilla JS app on Symfony
        • About Flutter MVVM Architecture with GetX
        • How to join tables from multiple dbcontexts in one ViewModel?

        Related Questions in PLAYFRAMEWORK

        • Install Play 1.7.1 on Windows
        • Handling of WebSocket Client Messages in Play Framework
        • Handling WebSocket Connections in Play Framework
        • Handling WebSocket Connections in Play Framework in Scala
        • build.sbt error: value addRepositoryAuthentication is not a member of lmcoursier.CoursierConfiguration
        • How to log incoming request with Scala & Play Framework?
        • Optionally enable Scala play to accept mLTS credentials?
        • reactivemongo with scala3, pekko, play-3
        • Specs 2 - I want to mock one of the injected modules into the controller class
        • Scala3 equivalent to -Wconf src filter
        • Using Selenium in Scala play not only for testing
        • How to execute PUT REST client code using playframework in scala
        • ERROR akka.actor.ActorSystemImpl(applica Uncaught error from thread, shutting down JVM since 'akka.jvm-exit-on-fatal-error' is enabled for ActorSystem
        • Autogenerating unnapply for play form handling in scala3 for single-element case classes
        • Play framework + Java +Ebean models.Donation is NOT an Entity Bean registered with this server?

        Related Questions in TWIRL

        • Including the result of calling a route inside a view in Play
        • Twirl dependencies required for scalatra project using Gradle/Mill
        • parsing a variable from a scala twirl template to javascrpt in a seperate file
        • IntelliJ Format/colour issues when injecting dependencies to templates in Scala Play 2 Views
        • ScalaJS: Referring to non-existent class play.twirl.api.Html
        • How to prevent Twirl from HTMLentities-encode strings in script scetion?
        • Upgrade project to Scala 2.13: JavaConversions is not a member of package collectio
        • Editing Play 2.8 Twirl templates in Eclipse
        • Unable to render a for loop in Twirl
        • Twirl with maven - Exception in thread "main" java.lang.NoSuchMethodError:
        • Playframework Twirl @form 'class
        • Dynamic size of form in Play framework 2.7 (Java)
        • How to pass implicit value from one template layout to another in Play Framework's Twirl?
        • Scala 2.13: Passing an explicit array value to a Scala varargs method is deprecated
        • Use forward slash before template variable

        Trending Questions

        • UIImageView Frame Doesn't Reflect Constraints
        • Is it possible to use adb commands to click on a view by finding its ID?
        • How to create a new web character symbol recognizable by html/javascript?
        • Why isn't my CSS3 animation smooth in Google Chrome (but very smooth on other browsers)?
        • Heap Gives Page Fault
        • Connect ffmpeg to Visual Studio 2008
        • Both Object- and ValueAnimator jumps when Duration is set above API LvL 24
        • How to avoid default initialization of objects in std::vector?
        • second argument of the command line arguments in a format other than char** argv or char* argv[]
        • How to improve efficiency of algorithm which generates next lexicographic permutation?
        • Navigating to the another actvity app getting crash in android
        • How to read the particular message format in android and store in sqlite database?
        • Resetting inventory status after order is cancelled
        • Efficiently compute powers of X in SSE/AVX
        • Insert into an external database using ajax and php : POST 500 (Internal Server Error)

        Popular # Hahtags

        javascript python java c# php android html jquery c++ css ios sql mysql r reactjs node.js arrays c asp.net json

        Popular Questions

        • How do I undo the most recent local commits in Git?
        • How can I remove a specific item from an array in JavaScript?
        • How do I delete a Git branch locally and remotely?
        • Find all files containing a specific text (string) on Linux?
        • How do I revert a Git repository to a previous commit?
        • How do I create an HTML button that acts like a link?
        • How do I check out a remote Git branch?
        • How do I force "git pull" to overwrite local files?
        • How do I list all files of a directory?
        • How to check whether a string contains a substring in JavaScript?
        • How do I redirect to another webpage?
        • How can I iterate over rows in a Pandas DataFrame?
        • How do I convert a String to an int in Java?
        • Does Python have a string 'contains' substring method?
        • How do I check if a string contains a specific word?
        dbora

        Copyright © 2021 Jogjafile Inc.

        • Disclaimer
        • Privacy
        • TOS
        • Homegardensmart
        • Pricesm.com
        • Aftereffectstemplates