I'm a JS programmer trying to write some Groovy code (with no Java background at all). I need to write some templates using Groovy, so I created a function in order to avoid repetition. My goal is to be able to pass html objects to the function (for example: p(), div(), span() and so on), but is isn't working as I expected:
The function
void addon ( addonType, mainHolder, content ) {
div( class: "addon " + addonType ) {
div( class: "addon-main" ) {
div( class: "addon-main-holder" ) {
yieldUnescaped mainHolder
}
}
div( class: "addon-content" ) {
yieldUnescaped content
}
}
}
Doesn't works:
[...]
body {
addon( 'addon-warning', p('Lorem Ipsum'), p('Dolor sit amet consectetur') )
}
[...]
Works:
[...]
body {
addon( 'addon-warning', '<p>Lorem Ipsum</p>', '<p>Dolor sit amet consectetur</p>') )
}
[...]
I tried some variations, using yield rather than yieldUnescaped, but no success. Is it possible? Any help would be much appreciated.
Assuming you want to pass in further DSL-based tags into your addon function, I'd pass closure to that function instead. This is an simplified, self containing version (which makes it bit harder to read, as the template is in a string; take a look at the XXX comments):
import groovy.text.markup.*
def config = new TemplateConfiguration()
def engine = new MarkupTemplateEngine(config)
def template = engine.createTemplate('''
html {
body {
addon("Hello World", { // XXX: pass a closure
p("Lorem Ipsum")
p("Lorem Ipsum")
p("Lorem Ipsum")
})
}
}
void addon(title, content) {
h1(title)
div {
content() // XXX call the closure
}
}
''')
Writable output = template.make([:])
println output
// => <html><body><h1>Hello World</h1><div><p>Lorem Ipsum</p><p>Lorem Ipsum</p><p>Lorem Ipsum</p></div></body></html>
Related
I'm developing a Jenkins shared library right now.
I wasn't able to figure how to easily "wrap" a code inside a function without copy-pasting the whole code. For example: If a developer sets a value to true, then I want to wrap the whole code inside a function. Right now I want to use this to allow e.g. the gitlabIntegration to be turned off from the Jenkinsfile.
Example:
// vars/stageWrapper.groovy
def call(Map parameters = [:], body) {
stage(stageName) {
if (pushtoGitlab) {
gitlabCommitStatus(stageName) {
if (!containerName) body()
else {
container(containerName) {
body()
}
}
}
} else {
if (!containerName) body()
else {
container(containerName) {
body()
}
}
}
}
}
let the user select if the stage should be pushed to gitlab via the gitlabCommitStatus wrapper.
switch to a specified container or use default container (if none is specified)
To realize this I currently repeat the code, which I really don't like...
Is there any way of achieving the same, but without repeating the same code over and over?
Thank You!
In Groovy you can reuse a Closure in different DSL-Builders by setting it's delegate to builder's delegate.
Something like this should work:
def containerNameBody = { body ->
if (!containerName)
body()
else
container(containerName) {
body()
}
}
def call(Map parameters = [:], body) {
stage(stageName) {
containerNameBody.delegate = delegate
if (pushtoGitlab)
gitlabCommitStatus(stageName) {
containerNameBody body
}
else
containerNameBody body
}
}
How about following approach to pass down the param into function, then decide how to do inside the function by the param value.
def gitHub(gitHubOn) {
}
def gitLab(gitLabOn) {
}
def call(Map parameters = [:], body){
//some code....
foo=bar
gitLab(parameters.gitLabOn)
gitHub(parameters.gitHubOn)
body()
}
I'm working on a custom Twig filter which resizes your images to a set width (and auto height).
{# TWIG CODE #}
{% set imgpath = page.header.custom.bgimage|first.path %}
<div class="thumb" style="background-image: url({{ url(imgpath|resize(240)) }})"></div>
It works great so far, but I encountered some errors when the ordering of the pages is changed. I'd like to use the Grav Debug Bar for debugging, since it's very convenient and keeps the code clean.
Inside Twig, you can simply use {{ dump(message) }}.
Unfortunately the resizing process happens inside native PHP, so I need a way to output messages from PHP to the Grav Debug Bar.
As stated inside the Docs, you can use $grav['debugger']->addMessage($this).
This throws an error when calling the resize Twig filter:
Twig_Error_Runtime
An exception has been thrown during the rendering of a template ("Undefined variable: grav").
Why is the variable $grav undefined?
<?php namespace Grav\Common;
use \Grav\Common\Grav;
use \Grav\Common\Page\Page;
use \RocketTheme\Toolbox\ResourceLocator\UniformResourceLocator;
use \Eventviva\ImageResize;
include_once getcwd().'/user/plugins/resizer/lib/ImageResize.php';
class TwigResizerFilters extends \Twig_Extension
{
private $grav;
public function __construct() {
$this->grav = Grav::instance();
}
public function getName() {
return 'TwigResizeFilters';
}
public function getFilters() {
return [
new \Twig_SimpleFilter( 'resize', [$this, 'resizeImage'] )
];
}
public function resizeImage($mediapath, $maxWidth = 1920) {
if (file_exists($mediapath)) {
// if file exists
if ($currImg = getimagesize($mediapath)) {
// if is image
if (preg_match('(jpg|jpeg|png)', $currImg['mime'])) {
// if file format correct
$resizedFolder = 'images/resized/';
// calculate exact img dimensions for proper naming
$maxHeight = floor(($maxWidth/$currImg[0]) * $currImg[1]);
if (!file_exists($resizedFolder)) {
// create folder if it does not exist
mkdir($resizedFolder, 0777, true);
}
// create filename
$resizedExtension = '.'.pathinfo($mediapath, PATHINFO_EXTENSION);
$resizedFilename = basename($mediapath, $resizedExtension).'#'.$maxWidth.'x'.$maxHeight.$resizedExtension;
if (file_exists($resizedFolder.$resizedFilename)) {
// if file already has been cached, just potput
return $resizedFolder.$resizedFilename;
} else {
// if not cached, resize to desired size
$image = new ImageResize($mediapath);
$image->resize($maxWidth, $maxHeight);
$image->save($resizedFolder.$resizedFilename);
return $resizedFolder.$resizedFilename;
}
} else {
$grav['debugger']->addMessage("File type of ".$mediapath." is not supported.");
}
} else {
$grav['debugger']->addMessage($mediapath." is not an image.");
}
} else {
$grav['debugger']->addMessage("File ".$mediapath." does not exist.");
}
}
private function mergeConfig( Page $page ) {
$defaults = (array) $this->grav['config']->get('plugins.resizer');
if ( isset($page->header()->resizer) ) {
$this->grav['config']->set('plugins.resizer', array_merge($defaults, $page->header()->resizer));
}
}
}
I have a simple Groovy method that uses Groovy's MarkupBuilder to print HTML, very simplified version below:
void writeHtmlFile(<args>) {
def writer = new FileWriter(fileName.toFile())
def html = new MarkupBuilder(writer)
html.mkp.yieldUnescaped '<!DOCTYPE html>'
html.mkp.xmlDeclaration(version: "1.0", encoding: "utf-8")
html.html {
head { ... }
body(id: 'main') {
h1 "Report Title"
}
}
writer.flush()
writer.close()
}
This works well. Say I wanted to call a method after the h1 that does some calculations and adds more to the MarkupBuilder. How do I get the elements defined in the called method added to the MarkupBuilder? Here's something I tried that doesn't cause an exception, but also doesn't work (the resulting HTML has no <h2> element):
Closure testNested() {
println '---'
return { h2 "here's a subheading" }
}
// .... other stuff from above example not repeated ...
html.html {
head {...}
body(id: 'main') {
h1 "Report Title"
testNested()
}
I know I can easily do this inline. I'm trying to deepen my understanding of how Groovy uses closures and delegates in DSLs and clearly I'm missing something.
Consider the following code, which executes fine for me, using Groovy 2.4.5.
The builder pattern is a bit tricky because it can be viewed as hierarchical data and/or code, depending on your perspective. With practice, one can switch perspectives as necessary.
import groovy.xml.*
void testNested(def html) {
html.h2("here's a subheading from testNested")
}
void writeHtmlFile(def fileName) {
def writer = new FileWriter(fileName)
def html = new MarkupBuilder(writer)
html.mkp.yieldUnescaped '<!DOCTYPE html>'
html.mkp.xmlDeclaration(version: "1.0", encoding: "utf-8")
html.html {
body(id: 'main') {
h1 "Report Title"
testNested(html)
}
}
writer.flush()
writer.close()
}
writeHtmlFile("out.html")
I have a function like this:
private downloadAllFiles() {
sftpRetriever.listFiles().findAll {
filter.isResponse(it) || filter.isResponseTurned(it)
}.each { String fileName ->
log.info 'Downloading file: {}', fileName
sftpRetriever.downloadFile(fileName)
log.info 'File downloaded'
removeRemoteFile(fileName)
}
}
I am looking for a simple way of modyfing this closure inside of that function so if the size() of findAll is 0 it will simply log 'No more files to download' and .each won't be executed. Is there any simple way to make it in single closure? It is really simply task if I divide it in several parts, but trying to learn closures here and improve my expressiveness :) Thank you in advance for your help.
Take a look at creature below :) It works due the fact that each returns the collection on which it's invoked (+ elvis operator and quite nice Groovy's truth evaluation):
def printContents(Collection collection) {
collection.each {
println it
} ?: println('Collection is empty')
}
printContents([1,2,'hello'])
printContents([])
I don't like this syntax but it's the shorter version which came to my mind.
You can also use metaprogramming to add the method provided by Steinar. It must be added to metaClass before first use but you'll avoid an effort to make extension module:
Collection.metaClass.doIfEmpty { Closure ifEmptyClosure ->
if (delegate.empty) {
ifEmptyClosure()
}
return delegate
}
def printContents(Collection collection) {
collection.doIfEmpty {
println "Collection is empty"
}.each {
println it
}
}
printContents([1,2,'hello'])
printContents([])
One rather generic and reusable option is to extend Collection using an extension module. This is surprisingly easy to do and is even recognized in IDE's (at least in IntelliJ) so you get code completion, etc.
For example, write an the extension class for collections which will perform the closure if the collection is empty. In addtion, it should always return the collection to allow further chaining:
package stackoverflow
class CollectionExtension {
static <T extends Collection> T doIfEmpty(T self, Closure closure) {
if (self.empty) {
closure()
}
return self
}
}
You will also need to tell groovy that this file is an extension module. Add a property file as a resource on the classpath: META-INF/services/org.codehaus.groovy.runtime.ExtensionModule (note: this name and location is mandatory for extension modules, i.e. you cannot change it).
moduleName=stackoverflow-module
moduleVersion=1.0
extensionClasses=stackoverflow.CollectionExtension
Finally a simple test script to show how this can be used:
def printContents(Collection collection) {
collection.doIfEmpty {
println "Collection is empty"
}.each {
println it
}
}
printContents([1,2,'hello'])
printContents([])
Output:
1
2
hello
Collection is empty
You may try the following piece of code:
def l1 = [1,2,3,4]
def l2 = [5,6,7,8]
def m(list) {
list.findAll { it < 5}.with { l ->
size > 0 ?
l.each { e ->
println e
}
:
println('Zero elements found')
}
}
m(l1)
m(l2)
No better idea at the moment.
Dynamically generated closure
I've written soap request in groovy wslite :
def request = {
envelopeAttributes('xmlns:art': 'http://url')
body {
'art:validate' {
item(itemValue)
}
}
}
It's working fine, but now I have to change this to list, so at the end it will be something like that:
def request = {
envelopeAttributes('xmlns:art': 'http://url')
body {
'art:validate' {
item(itemValue)
item(itemValue2)
item(itemValue3)
}
}
}
But have know Idea how I can dynamically create this request from List. I've even extracted this to variable:
def items = {
item(itemValue)
item(itemValue2)
item(itemValue3)
}
but I don't know how to add new items to this closure. Is there any easy way ?
Builder closures are normal Groovy code, so something like
def values = [itemValue, itemValue2, itemValue3]
def request = {
envelopeAttributes('xmlns:art': 'http://url')
body {
'art:validate' {
values.each { item(it) }
}
}
}
should work fine. Or if you have
def items = {
item(itemValue)
item(itemValue2)
item(itemValue3)
}
then you can do
def request = {
envelopeAttributes('xmlns:art': 'http://url')
body {
'art:validate'(items)
}
}
(passing the existing closure to art:validate rather than defining a new one inline).
With your given items Closure, this may work:
def request = {
envelopeAttributes('xmlns:art': 'http://url')
body {
'art:validate' {
items.delegate = delegate
items()
}
}
}
if you need other things inside art:validate