Good morning everyone,
I was wondering if someone can help me to get this done? I'm trying to render an ArrayList as a table using Apache Freemarker.
Imagine the following code (java):
public static void main(String[] args) throws Exception {
// freemarker
Configuration cfg = new Configuration(Configuration.VERSION_2_3_28);
cfg.setDirectoryForTemplateLoading(new File("./src/"));
cfg.setDefaultEncoding("UTF-8");
cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
cfg.setLogTemplateExceptions(false);
;
Template temp = cfg.getTemplate("template.ftl");
Map m = Collections.singletonMap("names", Arrays.asList("foo", "bar", "baz", "qux", "quux",
"corge", "grault", "garply", "waldo", "fred",
"plugh", "xyzzy", "thud"
));
//
Writer out = new OutputStreamWriter(System.out);
temp.process(m, out);
}
With this template:
<Start>
<#list 0..names?size-1 as i>
${names[i]}
</#list>
<end>
It Will be outputed like this:
<Start>
foo
bar
baz
qux
quux
corge
grault
garply
waldo
fred
plugh
xyzzy
thud
<end>
I was wondering if it was possible with freemarker to have it in columns:
<Start>
foo bar baz
qux quux corge
grault garply waldo
fred plugh xyzzy
thud
<end>
Any idea? Any suggestion is more than welcome.
?chunk can slice up a sequence to smaller sequences (see https://freemarker.apache.org/docs/ref_builtins_sequence.html#ref_builtin_chunk), so with the example of a HTML table you could do this:
<table>
<#list names?chunk(3) as row>
<tr>
<#list row as name><td>${name}</td></#list>
</tr>
</#list>
</table>
Related
For Example, I have a variable like this.
var fooBar = 12;
I want a something like this in Dart lang.
print_var_name(fooBar);
which prints:
fooBar
How can I achieve that? Is this even possible?
Thank you.
There is no such thing in Dart for the web or for Flutter.
Reflection can do that, but reflection is only supported in the server VM because it hurts tree-shaking.
You need to write code for that manually or use code generation where you need that.
An example:
class SomeClass {
String foo = 'abc';
int bar = 12;
dynamic operator [](String name) {
switch(name) {
case 'foo': return foo;
case 'bar': return bar;
default: throw 'no such property: "$name"';
}
}
}
main() {
var some = SomeClass();
print(some['foo']);
print(some['bar']);
}
output:
abc
123
I'm not sure if I am doing something wrong, but I can't get codehilite to generate code properly, example:
from markdown import markdown
self.content_html = markdown(self.content, ['codehilite'])
and I'm using this C# from wikipedia to test
public class GenericList<T>
{
void Add(T input) { }
}
class TestGenericList
{
private class ExampleClass { }
static void Main()
{
// Declare a list of type int.
GenericList<int> list1 = new GenericList<int>();
// Declare a list of type string.
GenericList<string> list2 = new GenericList<string>();
// Declare a list of type ExampleClass.
GenericList<ExampleClass> list3 = new GenericList<ExampleClass>();
}
}
and the result is
<p>public class GenericList<T>
{
void Add(T input) { }
}</p>
<p>class TestGenericList
{
private class ExampleClass { }
static void Main()
{
// Declare a list of type int.
GenericList<int> list1 = new GenericList<int>();</p>
<div class="codehilite"><pre> <span class="c1">// Declare a list of type string.</span>
<span class="nx">GenericList</span><span class="o"><</span><span class="kt">string</span><span class="o">></span> <span class="n">list2</span> <span class="o">=</span> <span class="nb">new</span> <span class="nx">GenericList</span><span class="o"><</span><span class="kt">string</span><span class="o">></span><span class="p">();</span>
<span class="c1">// Declare a list of type ExampleClass.</span>
<span class="nx">GenericList</span><span class="o"><</span><span class="nx">ExampleClass</span><span class="o">></span> <span class="n">list3</span> <span class="o">=</span> <span class="nb">new</span> <span class="nx">GenericList</span><span class="o"><</span><span class="nx">ExampleClass</span><span class="o">></span><span class="p">();</span>
<span class="p">}</span>
</pre></div>
<p>}</p>
As you can see, it didn't mark first few lines, and then started with comment // Declare a list of type string. What's wrong?
you should use pygments.
then try putting a :::<language> block up in your markdown before the block of code so it knows how to interpret it.
I wrote a article series about using markdown pygments to create a django blog that may be helpfull as well.
EDIT (this is whats up i think)
you need 4 spaces preceding each line of code for codehilite to pick up on it. ALSO, the first and last lines of code need a \n before and after, your code cannot be butted up against regular text (not indented by 4 spaces). ALSO, your code cannot be embedded into a bulleted list
I have a problem using knockoutjs with custom template bindings.
Suppose I have a HTML body like this:
<div id="1">
<div data-bind="template:{name: '2', data: data}"></div>
</div>
<div id="2">
<h3 data-bind="text: caption"></h3>
</div>
JS code looks like this:
var ViewModel2 = function () {
this.caption = ko.observable("Caption");
}
var ViewModel1 = function () {
this.data = new ViewModel2();
}
ko.applyBindings(new ViewModel1(), document.getElementById("1"));
If we test this code, everything will work just fine;
See JSFiddle example: http://jsfiddle.net/4eTWW/33/
Now suppose we want to make our custom template binding. We'll use 'templatex' binding instead of 'template'.
In HTML we need to change just one line:
<div data-bind="templatex:{name: '2', data: data}"></div>
Next, let's add custom template binding to JS:
/*Custom binding*/
ko.bindingHandlers.templatex = {
init: function (element) {
ko.bindingHandlers.template.init.apply(this, arguments);
},
update: ko.bindingHandlers.template.update
}
See: http://jsfiddle.net/4eTWW/35/
But in this case we have an error, saying that it can't find 'caption' in the model.
Now let's add template {} to html bindings:
<div data-bind="template: {}, templatex:{name: '2', data: data}"></div>
See: http://jsfiddle.net/4eTWW/36/
And now everything works just fine.
It seems that while binding parent div it can't determine that child div is a template.
So how can I mark it as a template in my custom template binder?
Thanks.
You have wrong update handler, change to this:
ko.bindingHandlers.templatex= {
init: function(element) {
// do things
return ko.bindingHandlers.template.init.apply(this, arguments);
},
update: function(element) {
return ko.bindingHandlers.template.update.apply(this, arguments);
}
}
Here is working fiddle: http://jsfiddle.net/vyshniakov/4eTWW/39/
I don't think you can use a custom binding to create a new template engine. You need to register your custom engine with ko.setTemplateEngine().
From the knockoutjs source:
If you want to make a custom template engine,
[1] Inherit from the ko.templateEngine class (like ko.nativeTemplateEngine does)
[2] Override 'renderTemplateSource', supplying a function with this signature:
function (templateSource, bindingContext, options) {
// - templateSource.text() is the text of the template you should render
// - bindingContext.$data is the data you should pass into the template
// - you might also want to make bindingContext.$parent, bindingContext.$parents,
// and bindingContext.$root available in the template too
// - options gives you access to any other properties set on "data-bind: { template: options }"
//
// Return value: an array of DOM nodes
}
[3] Override 'createJavaScriptEvaluatorBlock', supplying a function with this signature:
function (script) {
// Return value: Whatever syntax means "Evaluate the JavaScript statement 'script' and output the result"
// For example, the jquery.tmpl template engine converts 'someScript' to '${ someScript }'
}
This is only necessary if you want to allow data-bind attributes to reference arbitrary template variables.
If you don't want to allow that, you can set the property 'allowTemplateRewriting' to false (like ko.nativeTemplateEngine does)
and then you don't need to override 'createJavaScriptEvaluatorBlock'.
Example: http://jsfiddle.net/6pStz/ (see Note 7 on this page)
I'd like to return matches for a given search-string in a string. Plus the next word after the search-string.
Phrase to search for: "foobar foo"
Example Input:
foo foobar foo bar1 foobar1
foobar foos bar2 foobar2
foo barfoobar foos bar3 foobar3
Desired Matches:
foobar foo bar1
foobar foos bar2
barfoobar foos bar3
Use regex pattern
\b\w*foobar foo\w*\s+\w+\b
I am new to Javascript and Dojo. I apologize in advance if my question below has an obvious answer.
A cookie is set to be
var foo = "abcde";
dojo.cookie("bar", foo);
Later foo became foo = "abcdefghijk".
Is it possible to update dojo.cookie("bar") so that it always contains the most updated information of foo (i.e., must not change the cookie name, "bar")?
Thanks!
Simply do this:
var foo = "abcde";
dojo.cookie("bar", foo);
foo = "abcdefghijk";
dojo.cookie("bar", foo);
See example http://jsfiddle.net/JszB7/4/ and read about dojo cookie http://dojotoolkit.org/reference-guide/1.7/dojo/cookie.html