Is it possible to do basic escaping (of ampersand, in my case) when generating a custom attribute? I have tried many ways (with normal escaping, th:text, th:utext, th:attr for all the dynamic attributes, substituting a custom tag in my Spring code), unfortunately found just some workaround, which is listed below.
Basically, it's about an AngularJS application with a piece of Thymealeaf 3 template:
<script th:inline="javascript">
function customSubmit() {
/*<![CDATA[*/
return /*[(${DIALOG_NAME} + '.$valid && submit()')]*/ null;
/*]]>*/
}
</script>
<form th:name="${DIALOG_NAME}"
th:action= "'/' + ${MODULE} + '.' + ${DIALOG_NAME}"
th:ng-app="${DIALOG_NAME} + 'App'"
th:ng-controller="${DIALOG_NAME} + 'Controller'"
ng-submit="customSubmit()"
...
>
...
</form>
What I am trying to make is one construct like
th:ng-submit="some Thymeleaf expression"
that generates the custom attribute value with the && within:
ng-submit="someDialog.$valid && submit()"
without any function redirection like in the workaround above.
Beside extending Thymeleaf (I am using SpringStandard dialect), is any straightforward way to generate such strings?
Thank you for any suggestion.
Ampersands (&) should be HTML-escaped in tag attributes so you can do it like this:
th:attr="ng-submit='some thymeleaf with && in it'"
EDIT:
If you also want to reference a value of some model attribute in the final form attribute value you have to use the expression preprocessing like this:
<form th:attr="ng-submit='__${DIALOG_NAME}__' + ' some ampersand && in it'">
</form>
Related
Hello I am building an application in Spring Boot and Thymeleaf and I have a paragraph that I want to make it appear only if it has a value. If it does not I do not want it to appear.
Here is the code that I have tried:
<h2 th:text="'Raspuns: ' + ${raspuns}" th:disabled="${raspuns}==null"></h2>
But when I enter the page it says: Raspuns: null I want to make that dissapear.
try this:
<h2 th:if="${raspuns} != null" th:text="'Raspuns: ' + ${raspuns}"></h2>
see more about conditions in the documentation:
Thymeleaf Conditional Evaluation
This is probably a simple question, but to which I havent found an answer yet.
How to escape the ' _ ' when creating an HtmlElement in razor?
To render a '-' in the final Html we put an ' _ ', but to render an '_' (underscore), How do we escape it? I tryed '#:', but it didn't work, and didn't find any other options...
Example:
#Html.CheckBox("Access_Groups", false, new
{
#class = "input-control checkbox",
#data_group = "I', looking for data-group",
#Description_pt = "<----- I'm looking for Description_pt"
})
#data_group will render as data-group as expected, but...
#Description_pt will render as Description-pt, and that is not what is expected (don't know how to escape the _ for this)
thank you
If you look at the signature of Html.Checkbox, we can see that it takes an object for the htmlAttributes. Further, looking at the syntax, its actually a key based collection of objects. A Dictionary<string,object> fits that bill and allows you to absolutely specify the name of the html attributes that you want to add (note each key is typed exactly how we want it to display).
#Html.CheckBox("Access_Groups", false, new Dictionary<string,object>
{{"class", "input-control checkbox"},
{"data-group", "I', looking for data-group"},
{"Description_pt", "SomeValue"}})
This renders the following HTML
<input Description_pt="SomeValue" class="input-control checkbox"
data-group="I', looking for data-group" id="Access_Groups"
name="Access_Groups" type="checkbox" value="true" />
Have you tried the HTML character code for underscore, i.e., _?
I would like to do sommeting like that:
<div id="header_nav" th:fragment="header_nav (someId)" th:with="navPrefix=${'/content' + (someId ? ('/'+someId) : '')"}">
and use it later as *{navPrefix} within the div. I somehow don't get it with the condition nesting.
Can anyone help me?
Thanx!
I suspect this is because you're using th:include when you use the fragment. According to the Thymeleaf Documentation th:include simply includes the fragments content:
Whereas th:include will include the contents of the fragment into its host tag, th:replace will actually substitute the host tag by the fragment’s
If you're using th:include then your th:with won't be getting included, so it will always be blank. You can observe this with other attributes in the same element as the th:fragment declaration, such as style.
You can get around this by using th:replace instead, which will swap the entire fragment, not just the contents. Or, you can wrap your fragment contents with a th:block which you can use to define navPrefix:
<div th:fragment="header_nav(someId)">
<th:block th:with="navPrefix = ${'/content' + (someId != null ? ('/' + someId) : '')} ">
... contents of fragment ...
</th:block>
</div>
The only issues I see are the extra double quotes at the end, and your ternary operator doesn't contain a boolean expression when you pass in a String.
The following works to display either the prefix or the passed parameter within the block:
<div id="header_nav"
th:fragment="header_nav(someId)"
th:with="navPrefix = ${'/content' + (someId != null ? ('/' + someId) : '')} ">
<div th:text="${someId}"></div>
<div th:text="${navPrefix}"></div>
</div>
I want to put a packman game on my *.xhtml page.(I am using jsf 2 and primefaces 3.5)
However,
when I "translated" the html page in xhtml I get an error at this script:
<script>
var el = document.getElementById("pacman");
if (Modernizr.canvas && Modernizr.localstorage &&
Modernizr.audio && (Modernizr.audio.ogg || Modernizr.audio.mp3)) {
window.setTimeout(function () { PACMAN.init(el, "./"); }, 0);
} else {
el.innerHTML = "Sorry, needs a decent browser<br /><small>" +
"(firefox 3.6+, Chrome 4+, Opera 10+ and Safari 4+)</small>";
}
</script>
At line:
if (Modernizr.canvas && Modernizr.localstorage &&
i get:
The entity name must immediately follow the '&' in the entity
reference.
Any idea how to fix that?
All answers posted so far are giving the right solutions, however no one answer was able to properly explain the underlying cause of the concrete problem.
Facelets is a XML based view technology which uses XHTML+XML to generate HTML output. XML has five special characters which has special treatment by the XML parser:
< the start of a tag.
> the end of a tag.
" the start and end of an attribute value.
' the alternative start and end of an attribute value.
& the start of an entity (which ends with ;).
In case of & which is not followed by # (e.g. , , etc), the XML parser is implicitly looking for one of the five predefined entity names lt, gt, amp, quot and apos, or any manually defined entity name. However, in your particular case, you was using & as a JavaScript operator, not as an XML entity. This totally explains the XML parsing error you got:
The entity name must immediately follow the '&' in the entity reference
In essence, you're writing JavaScript code in the wrong place, a XML document instead of a JS file, so you should be escaping all XML special characters accordingly. The & must be escaped as &.
So, in your particular case, the
if (Modernizr.canvas && Modernizr.localstorage &&
must become
if (Modernizr.canvas && Modernizr.localstorage &&
to make it XML-valid.
However, this makes the JavaScript code harder to read and maintain. In case when you want to continue using & instead of & in JavaScript code in a XML document, then you should be placing the JavaScript code in a character data (CDATA) block. Thus, in JSF terms, that would be:
<h:outputScript>
<![CDATA[
// ...
]]>
</h:outputScript>
The XML parser will interpret the block's contents as "plain vanilla" character data and not as XML and hence interpret the XML special characters "as-is".
But, much better is to just put the JS code in its own JS file which you include by <script src>, or in JSF terms, the <h:outputScript>.
<h:outputScript name="onload.js" target="body" />
(note the target="body"; this way JSF will automatically render the <script> at the very end of <body>, regardless of where <h:outputScript> itself is located, hereby achieving the same effect as with window.onload and $(document).ready(); so you don't need to use those anymore in that script)
This way you don't need to worry about XML-special characters in your JS code. As an additional bonus, this gives you the opportunity to let the browser cache the JS file so that total response size is smaller.
See also:
Error Parsing /page.xhtml: Error Traced[line: 42] The entity "nbsp" was referenced, but not declared
Is it possible to use JSF+Facelets with HTML 4/5?
How to reference CSS / JS / image resource in Facelets template?
You need to add a CDATA tag inside of the script tag, unless you want to manually go through and escape all XHTML characters (e.g. & would need to become &). For example:
<script>
//<![CDATA[
var el = document.getElementById("pacman");
if (Modernizr.canvas && Modernizr.localstorage &&
Modernizr.audio && (Modernizr.audio.ogg || Modernizr.audio.mp3)) {
window.setTimeout(function () { PACMAN.init(el, "./"); }, 0);
} else {
el.innerHTML = "Sorry, needs a decent browser<br /><small>" +
"(firefox 3.6+, Chrome 4+, Opera 10+ and Safari 4+)</small>";
}
//]]>
</script>
The parser is expecting some HTML content, so it sees & as the beginning of an entity, like è.
Use this workaround:
<script type="text/javascript">
// <![CDATA[
Javascript code here
// ]]>
</script>
so you specify that the code is not HTML text but just data to be used as is.
Do
<script>//<![CDATA[
/* script */
//]]></script>
If you use XHTML, for some reason, note that XHTML 1.0 C 4 says: “Use external scripts if your script uses < or & or ]]> or --.” That is, don’t embed script code inside a script element but put it into a separate JavaScript file and refer to it with <script src="foo.js"></script>.
Just in case someone from Blogger arrives, I had this problem when using Beautify extension in VSCode. Don´t use it, don´t beautify it.
I have a GSP page containing different input elements related to a specific context.
For example I can display a textfield for usecase A, but not for usecase B (short version, it's very complex actually)
For this I have a domain object, which is normally populated with plain static HTML. But now I need to add dynamic data, like this:
def field = new InputField()
field.code = '<input type="text" name="foo" value="${currentUser.name}" />'
// or: field.code = '<option value="1"><g:message code="someCode"/></option>'
This code is stored in database. It will be rendered later on in GSP:
<g:each in="${InputField.findAllBySomeCondition(...)}">
${it.code}
</g:each>
This will print the input element, but instead of evaluating the dynamic code (${currentUser.name}) it is just printed as plain text.
Unfortunately I can't change the whole process, there are over 3000 different input elements stored already, but none of them are dynamic.
Is there a way to tell Grails to evaluate code within the variable before printing it?
edit: I'm using Grails 2.2.4
That's because you're using ' for the string, not ". When using ' the result is a String, not a GString with variable evaluation.
Try changing to:
field.code = "<input type=\"text\" name=\"foo\" value=\"${currentUser.name}\" />"
or use the triple version """ to avoid escaping:
field.code = """<input type="text" name="foo" value="${currentUser.name}" />"""