is it possible to use thymyleaf conditions for building javascript function ?
For example I have a flag specialClient which I will pass from java code to template render engine. So now I want to write something like followed code in my template:
...
<th:if=${specialClient}>
callbackForSpecialCLient()
<else>
plainCallbackWithAdForPoorClients
<endif>
...
which (after rendering) should result in:
...
callbackForSpectialClient()
...
As you can see we don't have any if conditions in rendered result. That's what I want so much to achieve.
You could achieve your desired functionality with following code:
<script th:inline="javascript">
<th:block th:if="${specialClient}">
callbackForSpecialCLient();
</th:block>
<th:block th:unless="${specialClient}">
plainCallbackWithAdForPoorClients();
</th:block>
</script>
Related
Going through the docs, it seems like Sapper's template.html only has these tags available:
%sapper.base%
%sapper.styles%
%sapper.head%
%sapper.html%
%sapper.scripts
What I'd love to have is the current page's slug to use it like so:
<!-- bottom of template.html -->
<img src="https://piratepx.com/page={currentPageSlug}"
alt="" style="display:none;"/>
</body>
</html>
It doesn't need to be available to JS, just added in the HTML.
I'm exporting to static and this would be a trivial task with most Static Site Generators but I can't find the obvious solution with Sapper.
Considering you are using Sapper I would do this in _layout.svelte instead:
<script>
import { stores } from '#sapper/app';
const { page } = stores();
$: currentPageslug = $page.params.slug
</script>
<img src="https://piratepx.com/page={currentPageSlug}" alt=""/>
With this technique you can also add a default value, or some condition in case there is no slug.
i using simple_html_dom.php
how to get textarea value if the website has used bad tag.
the textarea tag already closed before </textarea> like input tag.
Textarea HTML like below:
<textarea name="xxx" id="xxx" />this is value</textarea>
When i use this function, i dont get anything
$textarea = $html->find("textarea[name=xxx]");
$contents = $textarea->innertext;
echo $contents;
how to get 'this is value' using simple_html_dom.php or other alternative?
Thank you
Well, my previous comment won't work in this case, I'll leave it for info though...
Another approach is to clean it up before parsing it with simple_html_dom using Tidy extension. But it seems not to be working here either...
A last approach I can think of, and if this is your only problematic case, is to use regex to get what you want:
Using <textarea.*?name="xxx".*?id="xxx".*?\/>([^<]+)<\/textarea> ==> RegEx DEMO
The output will be in group one of the resulting array $match. Check this working code:
$input = <<<_DATA_
<textarea name="xxx" id="xxx" />this is value</textarea>
_DATA_;
$pattern = '/<textarea.*?name="xxx".*?id="xxx".*?\/>([^<]+)<\/textarea>/';
preg_match($pattern, $input, $match);
var_dump($match)
Working DEMO
It is easy to get the value of a Teaxtarea in javascript:
<script type=text/javascript>
function getValueTextarea()
{
var vl=document.getElementById("tx").value;
alert(vl);
}
</script>
<body>
<textarea id="tx">Value Of Textarea</textarea>
<input id="button" value="Get Value" onclick="getValueTextarea()">
</body>
I have 2 HTML files, suppose one.html and two.html. In one.html I want to include two.html.
In JSF I can do it like that:
It means that inside one.xhtml file, I can include two.xhtml.
How can we do it in *.html file? in thymeleaf
you can do it easily. could you share your header.html file?
or, let me show a bit like I do for my projects
in header.html put in side the <body> a code:
<div th:fragment="client_header">
<p>This is for client</p>
</div>
<div th:fragment="admin_header">
<p>This is for admin</p>
</div>
and let's say you want to add client_header into the index.html. Do follow these in your index.html page (inside <body>)
<div th:replace="includes/header :: client_header">
</div>
note: includes/header before :: refers the html path (excludes .html) and client_header after :: refers the part in header.html you want to insert.
I hope you understand everything that I have explain here.
In one.html you can include two.html by following this steps:
Place both pages under the resources folder, something like /resources/one.html, /resources/two.html
Include two.html in one.html by adding this line in one.html:
<div th:insert="two :: two"></div>
Include this bit of code in two.html just after the <body> tag:
<div th:fragment="two">
This should include the bit of code surrended by the <div th:fragment="two">into one.html
More info in the thymeleaf documentation
I have a grails render tag that renders a small chunk of HTML. Sometimes the HTMl needs to display some text, and sometimes some text and a link.
This chunk of code is working fine:
<g:render template='accountInfo' model="[
'accountStatus':subscription.status,
'subscription':subscription.plan.name,
'msg':g.message (code: 'stripe.plan.info', args:'${[periodStatus, endDate]}')]"/>
But I want to be able to pass in some HTML for the 'msg' variable in the model, like this to ouput some text and a link:
<g:render template='accountInfo' model="[
'accountStatus':profile.profileState,
'subscription':'None selected yet',
'msg':Your profile is currently inactive, select a plan now to publish your profile.]"/>
That code does not work. I've tried adding the link to a taglib, but I can't figure out how to pass the taglib to the 'msg' either:
<g:render template='accountInfo' model="[
'accountStatus':profile.profileState,
'subscription':'None selected yet',
'msg':<abc:showThatLink/>]"/>
I am open to suggestions on how best to achieve passing in text only, and text and a grails createLink closure along with some text for the link.
You have multiple options:
1 Use quotes:
<g:render template='accountInfo' model='${ [msg: "<a href='www.someurl.com'>.. </a>"]}' />
2 Use another variable:
<g:set var="myMsg>
<a href="www.someurl.com>...</a>
</g:set>`
<g:render template='accountInfo' model='[ 'msg': ${myMsg} ]'/>
3 Use the body content of <g:render />:
<g:render template="accountInfo" model="[ .. ]">
..
</g:render>
You can use to body() method inside the template (accountInfo) to render the body content. Some time ago I wrote a small blog post about this which gives some more details.
I'm implementing a simple JSP page using JSTL 1.2 (Apache Taglibs). The page does the following:
<c:catch var="error">
<fmt:parseNumber var="parsedNum" value="${param.num}" />
</c:catch>
The HTML input element looks like this:
<input type="text" name="num" size="3"/>
I'm aware that the input "12a" is permitted due to the way the parsing mechanism works. Nevertheless, I would like to catch completely wrong input, e.g., "aaa". Unfortunately, the thrown exception is not managed by <c:catch>, resulting in Tomcat 7 showing the whole stack trace.
Any advice? Thanks.
Please post a testable page for us. For example, the following page works fine for me.
<%#taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%# taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<c:catch var="error">
<fmt:parseNumber var="parsedNum" value="aaa" />
</c:catch>
${parsedNum}
${error}