Is it possible in Apache's velocity template engine to partially parse html template?
For example:
If I have template like this:
<div class="container">
<div id="section1">Some content of section 1....</div>
<div id="section2">Some content of section 2....</div>
</div>
I would like to parse only contents of section1 div. How can I accomplish this?
I'm using Spring MVC 3.0 with this.
There is no direct way to achieve this, but you can use variables to define parts and assist in partial parsing:
<div class="container">
#if ($model.part1)
<div id="section1">Some content of section 1....</div>
#end
#if (model.part2)
<div id="section2">Some content of section 2....</div>
#end
</div>
where model
public class PartialDef {
boolean part1;
boolean part2;
//setters and getters
}
So based on which part of html you want to include/exclude, define and turn-On/Off variables accordingly.
Related
I'm trying to create a fragment that represents a card with custom content. I would like to do something like:
<div class="card" th:fragment="myfragment" th:utext="${content}">
</div>
And then use as
<th:block th:replace="myfragment">
<p>Some custom content that would be the value of 'content'</p>
</th:block>
This would make it a lot easier to work with bigger html that would be kinda ugly to write in an attribute. (Basically I'm looking for a similar functionality to Blade's views and slots)
EDIT:
I'm aware of fragment parameterisation but passing long and complex html code in an attribute is pretty ugly and hard to manage.
A more descriptive example would be a card where the card body is not a p but a table for example.
Soo, maybe not the best solution but I managed to get this working based on this Other SO thread and this
Example code
I've created a new dialect so I can say this:
<zms:card header="'ASD Title'">
<div th:text="${first_name}"></div>
asdasd card works asdasd
</zms:card>
And it will render this:
<div class="card shadow mb-4">
<div class="card-header py-3">
<div class="d-inline-block">
<h6 class="m-0 font-weight-bold">ASD Title</h6>
</div>
</div>
<div class="card-body">
<div>Name</div>
asdasd card works
</div>
</div>
Thymeleaf supports parameterized fragments. Here's how you can use it:
Fragment Definition:
_fragments/my_fragments.html:
<th:block th:fragment="myFragment(p1, p2, p3)">
//do sth with p1, p2, p3
</th:block>
Using it:
<th:block th:replace="_fragments/my_fragments :: myFragment(${p1}, ${p2}, ${p3} )">
</th:block>
Ref:
https://ganeshtiwaridotcomdotnp.blogspot.com/2020/09/thymeleaf-pass-parameter-to-fragment.html
https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#parameterizable-fragment-signatures
https://github.com/gtiwari333/spring-boot-web-application-seed/blob/master/main-app/src/main/resources/templates/_fragments/_utils.html
I'm looking for a way to include the structure of a thymeleaf fragment into a page.
What i mean by that is as follows:
The fragmend defined as follows:
<div class="container" th:fragment="container">
<div class="row">
{the content of the page continues here}
</div>
</div>
The page template:
<div th:replace="fragments/main:: container">
{I can continue here for eq, <div class="col-md-5"></div>}
</div>
I dont't know if this is possible but i'm looking for a way to do this.
Due to layout dialect of Thymeleaf, this can be done by adding
<th:block layout:fragment="content"/> to desired layout and using this as parent element in your view.
I have a string in java,I need to append html tag to it dynamically so that when displayed in the frond it,the html tags behavior is felt.
Eg:
String content="Hello World,this is a test <em>content</em> to demonstrate the requirement";
In the above string content is wrapped inside the <em> tag.But when I am trying to display it in angularjs front end, the string is not taking the tag behavior and displayed as "Hello World,this is a test <em>content</em> to demonstrate the requirement".
use angular-sanitize.js for the same -
example
<div ng-controller="testCtrl">
<div ng-bind-html="stringTest"></div>
</div>
you can use ng-bind-html
<div ng-controller="testCtrl">
<div ng-bind-html="stringTest"></div>
</div>
However, if you find this directive too restrictive and when you absolutely trust the source of the content you are binding to, then you can also use ng-bind-html-unsafe.
<div ng-controller="testCtrl">
<div ng-bind-html-unsafe="stringTest"></div>
</div>
How can I display a string that contains HTML tags in Thymeleaf?
So this piece of code:
<div th:each="content : ${cmsContent}">
<div class="panel-body" sec:authorize="hasRole('ROLE_ADMIN')">
<div th:switch="${content.reference}">
<div th:case="'home.admin'">
<p th:text="${content.text}"></p>
</div>
</div>
</div>
//More code....
And at this line of piece of code ${content.text} it literally generates this on the browser:
<p>test</p>
But I want to show this instead on the browser:
test
You can use th:utext (unescaped text) for such scenarios.
Simply change
<p th:text="${content.text}"></p>
to
<p th:utext="${content.text}"></p>
I will suggest to also have a look into documentation here to know all about using Thymeleaf.
I'm trying to get value of a href attribute from an anchor tag (a tag) using Java, without a third party API. I know the class of the label. The website looks like this:
<html>
<body>
<div id="uix_wrapper">
<div id="button">
<label class="downloadButton">
Button
</label>
</div>
</div>
</body>
</html>
You can use a regular expression if you try to avoid using any third-party libraries. A very basic expression for your example is
<a href="(.*?)">
and should work. You can try out yourself using https://www.debuggex.com/
The result will be in the second group.