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

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"
}

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".

#session.get not working in scala html template

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.

Replacing XML nodes using perl and Mojo::DOM

I would like to exchange node in an XML file using Mojo::DOM.
I'm pretty sure it is possible but I didn't find a way yet.
Given the following XML:
my $xml = q~
<html>
<div>
<p>1</p>
<p>2</p>
<img />
</div>
</html>
~;
I would like to remove the div and instead insert a body tag, so that the result looks like this:
my $xml = q~
<html>
<body>
<p>1</p>
<p>2</p>
<img />
</body>
</html>
~;
I thought about replace, but I didn't find an example where the replacement is the $dom of the replaced tag.
It's very simple to just find the <div> element and use the tag method to change its tag
This program demonstrates. The CSS selector html > div finds the (first) <div> element that is a child of an <html> element
use strict;
use warnings;
use Mojo::DOM;
my $xml = q~
<html>
<div>
<p>1</p>
<p>2</p>
<img />
</div>
</html>
~;
my $dom = Mojo::DOM->new($xml);
$dom->at('html > div')->tag('body');
print $dom, "\n";
output
<html>
<body>
<p>1</p>
<p>2</p>
<img>
</body>
</html>

In Tritium, how do I set a CSS class as a variable?

I'm using the Moovweb SDK, and writing Tritium to modify my HTML.
How do I save a CSS class as a variable?
I want to grab an existing class and apply it to other elements.
You can use the fetch tritium function to get the value of the class attribute in the element you're looking for and store it in a variable.
So given the following html:
<html>
<head>
<title> Tritium Tester </title>
</head>
<body>
<div id="one" class="random"></div>
<div id="two"></div>
</body>
</html>
You could write the following Tritium:
html() {
$("/html/body") {
$class_name = fetch("./div[#id='one']/#class")
$("./div[#id='two']") {
add_class($class_name)
}
}
}
Here's a live example link: http://play.tritium.io/331dfa6d01a7dd52261a9eaf812bdc5c7fb8c293

FBML and HTMLParser error

The below code:
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:fb="http://www.facebook.com/2008/fbml"
xml:lang="en" lang="en">
<head>
<title>FB Test</title>
</head>
<body>
Test
<fb:serverfbml style="width: 350px;">
<script type="text/fbml">
<fb:connect-form action="http://127.0.0.1/post_invite">
</fb:connect-form>
</script>
</fb:serverfbml>
</body>
</html>
Results in the following error:
- Warning: Compilation failed
- Warning: <class 'zope.tal.htmltalparser.NestingError'>: Open tags <html>, <body>, <fb:serverfbml>, <script> do not match close tag </fb:connect-form>, at line 16, column 4
PTRuntimeError: ['Compilation failed', u"<class 'zope.tal.htmltalparser.NestingError'>: Open tags <html>, <body>, <fb:serverfbml>, <script> do not match close tag </fb:connect-form>, at line 16, column 4"]
Yet the structure seems valid to me...
You can't put tags inside of a <script> tag, and the strict ZPT parser is complaining about that. You'll have to somehow escape the contents, like with a tal:content="structure string:" construct:
<script type="text/fbml" tal:content="structure string:
<fb:connect-form action="http://127.0.0.1/post_invite"<
>/fb:connect-form<
"></script>
The script tag must not contain xml to my knowledge.
You could enclose the contents in xml comments and see if that works.