Path of current template file within Velocity - velocity

Suppose I have the following templates:
.../main.vm
.../sections/footer.vm
Suppose main.vm looks like this:
Hello world, this is the main template, it's name is $name
This template also has the following footer:
#parse("sections/footer.vm")
And suppose footer.vm looks like this:
Hi there, I'm the footer! My file name is $name!
I would like for $name in main.vm to be "main.vm", and $name in footer.vm to be "sections/footer.vm". Is something like this possible?

Not unless you override the behavior of #parse, or provide your own parsing macro or directive which does something like:
#macro(include $templateName)
#set($name = $templateName)
#parse($templateName)
#end

Related

template formatting - allow readable format

I'm creating some apache velocity templates and formatting the code(intellij Idea) breaks the template, because the text parts of the template are automatically indented.
When there's only a single line of the output text, then the indent is not preserved in the output:
#macro(body)
#if(${someCondition})
Hello
#end
#end
The output is:
Hello
The problem with multiline text output is this:
#macro(body)
#if(${someCondition})
Hello
World
#end
#end
And the output:
Hello
World
Is there some way to strip the indent of the text part based on the sorrounding code? I'd like to avoid ugly formatted templates like this:
#macro(body)
#if(${someCondition})
Hello
World
#end
#end
The code which uses the template:
final VelocityEngine engine = new VelocityEngine();
engine.init();
final VelocityContext context = new VelocityContext();
//put variables into context
engine.evaluate(context, outputWriter, "LOG", inputString);
Maybe there's something I can put into VelolocityContext, but I wasn't able to find it.

Code styling without modifying the binaries

On the WebStorm for example and I believe in any Intellij product.
You can easily refeactor the code and style it as you like from the setting 'Code Style'.
But, the styling and refactoring actually change the binaries of the file.
for example if you decide you want new line after { it will add \n in each place.
I want to know if it possible to only visually display those different to the coder.
If I'm coding like this:
var func = function()
{
// Blah
}
And another programmer on the team code like this:
var func = function() {
// Blah
}
and also if I code like this:
var text = "";
and the other like this:
var text = '';
The thing is that I don't really care how it would actually be like in the saved file. I only care how it would be displayed to the programmer.
It is possible to achieve this ?
Simple answer: No. That's because coding rules exist and besides things like changed binaries you have the problem of version control as well. which style of your code should be versioned? While VCS' are able to deal with different line endings, what you ask for isn't supported anywhere.

hide some fields in a XWiki page

I've attached an object to an XWiki page. Now when the page is displayed, all the attributes of the object are displayed in the page too. I don't want to dispaly all the fields, how do I hide some of them?
Short answer: if you want to modify how an object is displayed in an XWiki page, you can edit the class sheet for this.
Longer anser: Assume that the object if of class BarClass in Space Foo, or short in Foo.BarClass. This class page, which defines what fields your objects have, has two accompanying pages, the class template at Foo.BarTemplate, and a class sheet Foo.BarSheet, which contains the code to display objects of this class. You want to look into the sheet.
This can be done by opening the page in the wiki editor, e.g.
http://localhost:8080/xwiki/bin/edit/Foo/BarSheet?editor=wiki
You will see a code like:
{{velocity}}
## You can modify this page to customize the presentation of your object.
## At first you should keep the default presentation and just save the document.
#set($class = $doc.getObject('Foo.BarClass').xWikiClass)
#foreach($prop in $class.properties)
; $prop.prettyName
: $doc.display($prop.getName())
#end
{{/velocity}}
If you want to hide, say field2, you can do this by changing the foreach loop to:
#foreach($prop in $class.properties)
#if ($prop.name != 'field2' || $xcontext.action == 'edit')
; $prop.prettyName
: $doc.display($prop.getName())
#end
#end
The $prop.name != 'field2' makes sure the field is not shown if that field's name is field2 and the $xcontext.action == 'edit' takes care that your field is still shown in the edit mode (otherwise your users will not be able to edit the field, which is probably not what you want).
If instead you have created your class with the AppWithinMinutes, the class sheet looks differernt:
{{velocity}}
{{html wiki="true"}}
#set ($discard = $doc.use('FooBarCode.FooBarClass'))
#set ($discard = $services.localization.use('document', 'FooBarCode.FooBarTranslations'))
(% class="xform" %)
(((
; <label for="FooBarCode.FooBarClass_0_field1">$escapetool.xml($doc.displayPrettyName('field1', false, false))</label>
: $doc.display('field1')
; <label for="FooBarCode.FooBarClass_0_field2">$escapetool.xml($doc.displayPrettyName('field2', false, false))</label>
: $doc.display('field2')
; <label for="FooBarCode.FooBarClass_0_field3">$escapetool.xml($doc.displayPrettyName('field3', false, false))</label>
: $doc.display('field3')
)))
{{/html}}
{{/velocity}}
In that case you need to find the two lines displaying the field you want to hide, and wrap then into a simple #if ($xcontext.action == 'edit') like:
#if ($xcontext.action == 'edit')
; <label for="FooBarCode.FooBarClass_0_field2">$escapetool.xml($doc.displayPrettyName('field2', false, false))</label>
: $doc.display('field2')
#end
If you want to know more about how XWiki classes are used, read the tutorial at http://platform.xwiki.org/xwiki/bin/view/DevGuide/FAQTutorialManual
Usually you want to create and manage your wiki classes via the "App within minutes" application: http://extensions.xwiki.org/xwiki/bin/view/Extension/App+Within+Minutes+Application which creates an user friendly interface, but for customizations like the one you wanted, you need to edit the sheet directly.

Unable to set the colour for gxt-TextField in IE

I'm not able to set the background color for the textfield in GXT. I have used
textfield.setStyleName("backgroung-color:red;")
textfield.setStylePrimaryName("backgroung-color:red;")
But it is not working in IE. How can i do this ?
The function "setStyleName()" is not working as you expect, it sets the component class name. You can create a style class with name for example 'field-bgColor' in your css file like:
.field-bgColor {
background-color: red;
}
after that, you would use it like following:
textfield.setStyleName("field-bgColor");
Or you can use "setStyleAttribute()" function like:
textfield.setStyleAttribute("backgroundColor", "red");
Hope it works for you :)

RDiscount custom generated html

I'm using RDiscount to convert Markdown to html in my application. Actually when I add some code in my markdown it generates code and pre tags but I want to add a class to the code tag how can I do this ? I need to parse generated HTML with Nokogiri or something like this ?
Rdiscount does not seem able to do this on its own. Parsing the result (with Nokogiri or whatever) might do the trick, but could be expensive. If you can, switch to another lib, like Redcarpet for example. This one lookes like it can be extended quite easily to fit your needs
One thing is sure, you can't tell Rdiscount to do it, it has no option for it. I looked at Rdiscount source code and this is what I end up in the file generate.c:
static void
printcode(Line *t, MMIOT *f)
{
int blanks;
Qstring("<pre><code>", f);
for ( blanks = 0; t ; t = t->next ) {
if ( S(t->text) > t->dle ) {
while ( blanks ) {
Qchar('\n', f);
--blanks;
}
code(f, T(t->text), S(t->text));
Qchar('\n', f);
}
else blanks++;
}
Qstring("</code></pre>", f);
}
No option to add any class to code and pre.
If you need to add a class to format the markup, maybe you can try a workaround and generate a div with a class before the
<div class="myclass">
<pre><code>
Is it feasible for you?