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

        66 Views Asked by nTn At 02 January 2023 at 11:07 2025-12-02T21:46:27.710000

        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

        • Spark .mapValues setup with multiple values
        • Where do 'normal' println go in a scala jar, under Spark
        • Serializing to disk and deserializing Scala objects using Pickling
        • Where has "Show Type Info on Mouse Motion" gone in Intellij 14
        • AbstractMethodError when mixing in trait nested in object - only when compiled and imported
        • Scala POJO Aggregator Exception
        • How to read in numbers from n lines into a Scala list?
        • Spark pairRDD not working
        • Scala Eclipse IDE compiler giving errors until "clean" is run
        • How to port Slick 2.1 plain SQL queries to Slick 3.0
        • Log of dependency does not show
        • Getting unary error for escaped characters in Scala
        • Akka actor invoked with a function delegate - is this bad practice?
        • Json implicit format with recursive class definition
        • How to create a executable jar reading files from local file system

        Related Questions in MODEL-VIEW-CONTROLLER

        • WebApi: Reading errors
        • i want to create a service that does the login functionality?
        • What is the point of the name method in the symfony2 annotation?
        • Is it recommended to use Node.js for an online room booking web application?
        • CodeIgniter - How to get a list of all my controllers dynamically?
        • MVC WPF DataContext for two UserControls
        • Is bootstrap file a controller?
        • PHP/Zend Framework 2 - Unable to display table field values within dynamically generated table
        • Laravel MVC application structure on UML class diagram
        • Select2 using Ajax (multi select) - when selecting second one first one disappars
        • How to only let correct_user create or destroy?
        • Google OAuth 2.0 .NET
        • How to Minitest Controller :create action with Paperclip Validators
        • Cannot get FullCalendar to work (Laravel 5)
        • Enable/ Disable different sets of Controllers in based on mode specified at startup Spring MVC

        Related Questions in PLAYFRAMEWORK

        • Auto reload with play2
        • Log of dependency does not show
        • Json implicit format with recursive class definition
        • Async LDAP authentication with play framework
        • Why do I get an IndexOutOfBoundsException when my else should prevent it?
        • Play 2.4 scala I am facing issues getting messages implicit in my code
        • Play template project requires subscription to typesafe, why?
        • hot swap in sbt project without play-plugin
        • Getting "Cannot find HTTP header here" in play framework Scala
        • How can I redirect to an error page in my Play app?
        • How do you iterate over json when the schema is not known up front?
        • Play Scala Converting sync to async
        • morphia Geo-spatial "near" method in embedded list
        • How to modify queryString and body of the request before being processed by the routes in Playframework 2 Scala?
        • Not able to access key-values pairs in a JSON using Play library in scala

        Related Questions in TWIRL

        • Rendering newline charactors from variable in Twirl
        • Unable to display data from relationships in templates
        • How to select theme based template in in play 2.5?
        • Scala/Twirl variables and scope/defining
        • Confusion regarding the use of @ in view classes of the Play framework
        • scala twirl autocomplete in intelliJ outside play
        • Twirl templates cannot be seen in the code (spray application)
        • How to prevent Twirl from HTMLentities-encode strings in script scetion?
        • Undesirable syntax in Play framework templates
        • ScalaJS: Referring to non-existent class play.twirl.api.Html
        • IntelliJ Format/colour issues when injecting dependencies to templates in Scala Play 2 Views
        • Play Framework Dependency Injection Template in Eclipse doesn't highlight
        • Scala template how to loop over two lists simultaneously
        • Including the result of calling a route inside a view in Play
        • How to change the location of twirl templates for integration tests?

        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

        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?

        Copyright © 2021 Jogjafile Inc.

        • Disclaimer
        • Privacy
        • TOS
        • Homegardensmart
        • Math
        • Aftereffectstemplates