How to find out if a project is in debug mode and get a variable of a logical type in the freemarker template.
Example:
<#if (DEBUG)??>
<script src="/static/jquery/jquery.js"></script>
<#else>
<script src="/static/jquery/jquery.min.js"></script>
</#if>
I simplified the same using the equals operator:
<#if DEBUG == true>
<script src="/static/jquery/jquery.js"></script>
<#else>
<script src="/static/jquery/jquery.min.js"></script>
</#if>
Related
In Freemarker, how can I create a template that inherits from a template that itself inherits?
Single inheritance works fine with the <#nested> tag:
File base.ftl:
<#macro layout>
<html lang="en">
<head>...</head>
<body>
<div>... (navigation bar)</div>
<div class="container">
<#nested>
</div>
</body>
</html>
</#macro>
File normalBase.ftl:
<#import "base.ftl" as base>
<#base.layout>
<div class="row">
<div class="col-md-9">
${content.body}
</div>
<div class="col-md-3">
<p>Latest releases</p>
<ul>....</ul>
</div>
</div>
</#base.layout>
How do I turn this into double inheritance where useCaseBase.ftl extends normalBase.ftl which extends base.ftl?
This works like a charm:
File base.ftl:
<#macro layout>
<html lang="en">
<head>...</head>
<body>
... // Shared navigation bar
<div class="container">
<#nested>
</div>
... // Shared footer
</body>
</html>
</#macro>
<#layout>
${content.body}
</#layout>
File normalBase.ftl:
<#import "base.ftl" as parent>
<#macro layout>
<#parent.layout>
<div class="row">
<div class="col-md-9">
<#nested>
</div>
<div class="col-md-3">
... // Shared sidebar
</div>
</div>
</#parent.layout>
</#macro>
<#layout>
${content.body}
</#layout>
File useCaseBase.ftl:
<#import "normalBase.ftl" as parent>
<#parent.layout>
${content.body}
... // Shared content between all use case pages
</#parent.layout>
Now I can create *.adoc pages with jbake-type set to either base, normalBase or useCaseBase and it works.
I tried to implement extends and block like Jinja2.
extends.ftl for defining macros.
<#if !blocks??>
<#assign blocks = {} />
</#if>
<#macro extends ftl>
<#nested />
<#include ftl />
</#macro>
<#macro replace name>
<#local value>
<#nested />
</#local>
<#assign blocks += {name: value} />
</#macro>
<#macro block name>
<#if blocks[name]??>
<!-- replaced ${name} -->
${blocks[name]}
<#else>
<!-- default ${name} -->
<#nested />
</#if>
</#macro>
base.ftl for layout
<#import "extends.ftl" as layout />
<!DOCTYPE html>
<html>
<head>
<title>A demo of FreeMarker extends directive</title>
</head>
<body>
<#layout.block "message">
This is default message in base.ftl
</#layout.block>
</body>
</html>
finally, index.ftl like this:
<#import "extends.ftl" as layout />
<#layout.extends "base.ftl">
<#layout.replace "message">
This is the message from index.ftl
</#layout.replace>
</#layout.extends>
Take a look at https://github.com/emesday/freemarker-extends .
<html lang="en">
<head>
<meta charset="UTF-8">
<title> Java, Spring Boot, FreeMarker</title>
<link href="/css/main.css" rel="stylesheet">
</head>
<script>
function myFunction() {
<#test/>
}
</script>
<body>
<h2>Java, Spring Boot, FreeMarker</h2>
<form action="/search" method="post">
Search : <input type="text" name="firstName" onkeyup="myFunction()" id = "fname">
<input type="submit" value="Submit">
</form>
<div style="background-color:lightblue">
<#macro test>
<#list empList as emp>
<div id="emp-data">
<ul>
<li>${emp}</li>
</ul>
</#list>
</div>
</#macro>
<script src="/js/main.js"></script>
</div>
</body>
When I run this code I am getting some errors on the browser console:
(index):60 Uncaught ReferenceError: myFunction is not defined at HTMLInputElement.onkeyup ((index):60) onkeyup # (index):60 – PCS 1 hour ago
Is it possible in FreeMarker to do something like that?
In a sense you can... but it doesn't do what you apparently believe it does. First all FreeMarker instructions, like <#test/>, are resolved on the server, then later the resulting output runs in the browser. So as far as the browser sees, function myFunction() { ... } contains HTML div-s directly in inside the { ... }, which is invalid JavaScript.
How do I add the js file to my page?
I have the following structure:
public
- css
- js
- vendor
- bootstrap
- jquery
I want to use the jquery and bootstrap that are in my vendor folder.
I'm trying the following way.
<script type="text/javascript" src=" {{ asset('vendor/jquery/js/jquery-3.2.1.min.js) }} "> </script>
<script type="text/javascript" src=" {{ asset('vendor/bootstrap/js/bootstrap.min.js) }} " > </script>
but does not work, error appears
syntax error, unexpected 'vendor' (T_STRING)
you have forgot to put ' at the end of assert function
<script type="text/javascript" src="{{asset('vendor/jquery/js/jquery-3.2.1.min.js')}}"> </script>
<script type="text/javascript" src="{{ asset('vendor/bootstrap/js/bootstrap.min.js')}}"> </script>
You are missing the closing quote:
<script type="text/javascript" src=" {{ asset('vendor/jquery/js/jquery-3.2.1.min.js') }} "> </script>
<script type="text/javascript" src=" {{ asset('vendor/bootstrap/js/bootstrap.min.js') }} " > </script>
I want to format numeric column value as 55,25,236.30 in grid. I created a JS function and called from column template in grid. My JS function:
function NumberFormater(Price) {
kendo.culture("en-US");
kendo.cultures.current.numberFormat.groupSize = [3,2];
return kendo.toString(Price, "##,#.00");
}
but the value is showing like 5,525,236.30. According to this link the default value for groupSize is [3]. Is it not possible to set multiple values like [3,2] like numberformat in C#?
Make sure that the NumberFormater function receives a numeric argument. Below is a test page that works.
On a side note, I am not sure there is a need to set the culture and groupSize every time you execute the function.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled</title>
<link rel="stylesheet" href="http://kendo.cdn.telerik.com/2016.3.914/styles/kendo.common.min.css">
<link rel="stylesheet" href="http://kendo.cdn.telerik.com/2016.3.914/styles/kendo.default.min.css">
<script src="http://code.jquery.com/jquery-1.12.3.min.js"></script>
<script src="http://kendo.cdn.telerik.com/2016.3.914/js/kendo.all.min.js"></script>
</head>
<body>
<input class="k-textbox" type="number" value="5525236.3" />
<button id="format" class="k-button">Format numeric value</button>
<script>
$("#format").click(function(){
var formattedValue = NumberFormater(parseFloat($(".k-textbox").val()));
alert(formattedValue);
});
function NumberFormater(Price) {
kendo.culture("en-US");
kendo.cultures.current.numberFormat.groupSize = [3,2];
return kendo.toString(Price, "##,#.00");
}
</script>
</body>
</html>
I have freemarker template as below .
<!DOCTYPE html>
<html>
<head>
<title>My Blog</title>
</head>
<body>
<#if username??>
Welcome ${username} Logout | New Post
<p>
</#if>
<h1>My Blog</h1>
<#list myposts as post>
<h2>${post["title"]}</h2>
Posted ${post["date"]?datetime} <i>By ${post["author"]}</i><br>
Comments:
<#if post["comments"]??>
<#assign numComments = post["comments"]?size>
<#else>
<#assign numComments = 0>
</#if>
${numComments}
<hr>
${post["body"]!""}
<p>
<p>
<em>Filed Under</em>:
<#if post["tags"]??>
<#list post["tags"] as tag>
${tag}
</#list>
</#if>
<p>
</#list>
</body>
</html>
Where ${post["body"]!""} represents body of my blog and display.
My question : How to represent body tag as html content.
Example : if body has table string
<TABLE BORDER="1" WIDTH="100%" CELLPADDING="3" CELLSPACING="0" SUMMARY=""> ... " ,
then blog also represent same.
How to convert this string to html table.enter code here
Note: body may also have anything (dynamic) and body is not fixed one.