#session.get not working in scala html template - scala

I have my #session.get("id") not working below. after logging into the system, I set the session value id with .withSession("id" -> id) in my controllers. Is there a way to get this session.get working?
main.scala.html
#(title: String)(content: Html)
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" cantent="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimun-scale=1.0, user-scalable=no" />
<title>#title</title>
</head>
<body>
<div class="main container">
<div class="navigation container">
<div class="logo container">
<div class="user">#session.get("id")</div>
<div class="separator"></div>
</div>
<div class="separator"></div>
<ul>
<li class="user">USER</li>
<li class="code">CODE</li>
</ul>
</div>
<div class="header container">
<div class="left"></div>
<a class="right" href="#all-menu"></a>
</div>
<div id="mainnav" class="mainnav">
#content
</div>
</div>
</body>
This cause not found: value session error. Same not found issue even after adding
#()(implicit session: play.api.mvc.Session)
And this is my index.scala.html which becomes a basic structure of my templates.
#*
* This template takes a single argument, a String containing a
* message to display.
*#
#(message: String)
#*
* Call the `main` template with two arguments. The first
* argument is a `String` with the title of the page, the second
* argument is an `Html` object containing the body of the page.
*#
#main("Welcome") {
#*
* Get an `Html` object by calling the built-in Play welcome
* template and passing a `String` message.
*#
#message
}
Please Help!

You need to explicitly pass the session to your template:
vies.htlm.index(message,session) from your action. And then further pass this session to your main.scala.html.
And in index.scala.html, remove implicit from the session parameter.
Or, pass (implicit request: Request[T]) in your index.scala.html and also in your main.scala.html.
#(message: String)(implicit request: Request[T]) -> index
#(title: String)(content: Html)(implicit request: Request[T]) -> main
You can access the session like:
request.session.get("key")
This will do the needful.

Related

Form get detail not found fastapi

I am using FastAPI to render a jinja2 template using a simple get request
This is my template
<!-- list.html !>
<html>
<head>
<title>Item Details</title>
</head>
<body>
{% for item in items %}
<p>{{ item }}</p>
{% endfor%}
</body>
</html>
#app.get("/list_items?search_string={search_string}", response_class=HTMLResponse)
async def list_items(req: Request, search_string: str):
print(8*'*', list(search_string))
items = list(search_string)
return templates.TemplateResponse("list.html", {"request": req, "items": items})
My form is as follows:
#app.get("/")
async def read_items():
html_content = """
<html>
<head>
<title></title>
</head>
<body>
<form action="/list_items/" method="get">
<input type="text" placeholder="Search.." name="search_string">
<button type="submit">Search</button>
</form>
</body>
</html>
"""
return HTMLResponse(content=html_content, status_code=200)
However, I get a detail not found error.
You should not include the {search_string} part in the route path:
#app.get("/list_items?search_string={search_string}", response_class=HTMLResponse)
^^^^
async def list_items(req: Request, search_string: str):^
You're already asking for this when including it as a parameter in your controller definition.
Instead, use the actual path you want to register the route for:
#app.get("/list_items", response_class=HTMLResponse)
async def list_items(req: Request, search_string: str):
.. and use the same path in your form action:
<form action="/list_items" method="get">
A small side note: since REST endpoints usually represent resources, a standard naming scheme would use just /items instead of /list_items, since GET on /items would mean "list the current items available".

Why is the page refreshing after I add an item to the To Do list?

I just learned how to create and append items. I wanted to practice using a button to submit the text to be appended. It works, however, the page seems to refresh right afterwards and removes the text from the list. What am I doing wrong?
HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>To Do List</title>
<link rel="stylesheet" href="ToDo.css">
</head>
<body>
<h1>To Do:</h1>
<ul>
<li class="todo">Create another todo.</li>
</ul>
<form>
<input type="text" placeholder="Add another item!">
<button onclick="output()">Add</button>
</form>
<script src="ToDo.js"></script>
</body>
</html>
JS:
let ul = document.querySelector('ul');
newItem = document.querySelector('input');
function output() {
let item = newItem.value;
let newTodo = document.createElement('li');
newTodo.innerText = item;
newTodo.classList.add('todo');
ul.append(newTodo);
}
Your button is submitting the form which is causing the page to refresh. Buttons are made to be type submit by default.
It looks like you don't actually need the form so you could either remove it or set the type on the button:
<button type="button" onclick="output()">Add</button>

How to add multiple 'OR' and 'AND' condition in a if block in Scala template in Play framework

messageViewer.scala.html
#(formData: Map[String, String] )
<!DOCTYPE html>
<html>
<head lang="sv">
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>Java Play - Scala Template</title>
</head>
<body>
#if(formData.get("nockOutRuleStatus1").equals("SUBMITTED") && formData.get("nockOutRuleStatus2").equals("APPROVED")){
//This statement should execute. But it is not executed.
<button type="button" class="btn btn-default btn-lg hide-element" id="openSuccessModalBtn" data-toggle="modal" data-target="#successModal">Success</button>
}
#if(formData.get("nockOutRuleStatus1").equals("REJECTED") || formData.get("nockOutRuleStatus2").equals("AUTO-REJECTED")){
<button type="button" class="btn btn-default btn-lg hide-element" id="openErrorModalBtn" data-toggle="modal" data-target="#errorModal">Error</button>
}
</body>
</html>
From my java controller(messageCtrl.java) I am passing the Map type formData.
Map<String, String> formData = new HashMap<String, String>();
formData.put("nockOutRuleStatus1","SUBMITTED");
formData.put("nockOutRuleStatus2","APPROVED");
return ok(messageViewer.render(formData));
But the statement inside the first IF block, is not executed.

Show method from resource controller returns null

I'm having trouble with learning laravel and decided to ask my question here since I can't find an answer via Google etc.
I am trying to return a client from the database via Id, maybe later via name.
This is my form:
<<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Klant invoeren</title>
<link rel="stylesheet" href="{{asset('css/app.css')}}">
</head>
<body>
<p><<form action="{{route('client/'.$client->id.'/show/')}}"
method="get">
<div class="row">
<div class="col-md-4"></div>
<div class="form-group col-md-4">
<label for="price">Achternaam:</label>
<input type="text" class="form-control" name="id">
</div>
</div>
<div class="row">
<div class="col-md-4"></div>
<div class="form-group col-md-4">
{{csrf_field()}}
<button type="submit" class="btn btn-success" style="margin-
left:38px">Zoek klant</button>
</div>
</div>
</form></p>
</body>
</html>
Which redirects to:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Klant invoeren</title>
<link rel="stylesheet" href="{{asset('css/app.css')}}">
</head>
<body>
<div class="container">
<h2>Voer een klant in</h2><br />
<h1>Showing {{ var_dump($client) }}</h1>
</div>
</body>
</html>`
And this is the show method from the Clientcontroller:
public function show($id)
{
$client = Client::find($id);
return view('clients.show', compact('client'));
}
This is the route I'm using:
Route::get('client/{id}/show','ClientController#show');
Can someone spot my mistake, because I can't after messing with this the last few hours.
EDIT: updated code and added the route, now getting the error that the client variable isn't defined in the
You must echo the client id in the form action. So, remove the inverted comma from $client->id. Use the following code :
<form action="{{action('ClientController#show', $client->id)}}"
method="get">
Additionally, remove backtick from the code. Use the following code :
public function show($id)
{
$client = Client::find($id);
return view('clients.show', compact('client'));
}
In your routes/web.php
Route::get('client/{id}/show','ClientController#show');
In your form:
<form action="{{URL::to('client/'.$client->id.'/show/')}}"
method="get">
In your controller:
public function show($id)
{
$client = Client::find($id);
return view('clients.show', compact('client'));
}
In your Client Controller make sure you use the Model that you are referencing to
Assuming App\Client.php Change it to App\User.php if you are referring to user
ClientController
use App\Client;
public function show($id)
{
$client = Client::find($id);
return view('clients.show', compact('client'));
}
Now you can fetch the client model with $client For example $client->id
Make sure you pass the {id} parameter in the route

Why does Lift escape this <lift:bind> value?

I have (roughly) this LIFT-ified HTML in my default template:
<html>
<head>
<title>FooBar Application | <lift:bind name="page-title"/></title>
</head>
<body>
<h1><lift:bind name="page-title" /></h1>
<div id="page-content">
<lift:bind name="page-content" />
</div>
</body>
</html>
...and then this in my main template:
<lift:surround at="page-content">
<lift:bind-at name="page-title">Home</lift:bind-at>
</lift>
...which give me this in the generated HTML:
<html>
<head>
<title>FooBar Application | <lift:bind name="page-title"/></title>
</head>
<body>
<h1>Home</h1>
<div id="page-content">
</div>
</body>
</html>
Why is the <lift:bind> tag in the <title> getting escaped, and the one in the <body><h2> not? And how do I prevent that from happening?
Are the pages defined through SiteMap? As was mentioned before, <title> can be a special case, and it is interpreted in several places - some of which might be doing the escaping. If you can, I'd try setting the page title in one of two ways:
Through the Sitemap you can use the Title Loc Param as referenced here: Dynamic title with Lift
You can also have something like: <title data-lift="PageTitle"></title> to have it invoke a snippet called page-title. Where, the snippet would be something like:
class PageTitle {
def render = "*" #> "FooBar Application | Home"
}