Display an attachment's content nicely formatted

Hi all

As our XWiki instance is mainly a documentation system, we have quite some “.sql” files attached to certain pages. Is there a way to show the content of these files nicely formatted?

Like

{{code language="sql"}}
{{include attachment="my_file.sql"}}
{{/code}}

Or even

{{include attachment="my_file.sql" language="sql"}}

I played around with the include macro. It offers the ATTACHMENT type, but I didn’t manage to include anything …

Thanks in advance

André

I think the only way is to write a script which get the content of the file and produce a code macro with that content in it.

Something like:

{{velocity}}
#set($attachmentContent = $doc.getAttachment('my_file.sql').contentAsString)
{{code language="sql"}}
$attachmentContent
{{/code}}
{{/velocity}}
1 Like

See https://snippets.xwiki.org/xwiki/bin/view/Extension/Code%20macro%20from%20attachment/

1 Like

Awesome, thanks gents! I’m always flashed by the speed!

Hi Vincent!

Would you mind adding the possibility to attach a file from a different Space? I tried stuff like that:

{{velocity}}
#set($ndoc=$wiki.getDocument('Different.Space.Other.Page'))
{{code language='sql'}}
$ndoc.getAttachment('file.sql').getContentAsString('UTF-8')
{{/code}}
{{/velocity}}

but somehow all it showed me was the formatted line starting with $ndoc. Didn’t really get the velocity thing, to be honest.

Thanks in advance

André

My guess is that you’re forgetting the trailing .WebHome for nested pages. :slight_smile:

1 Like

Thanks! Sorry for the late reply, didn’t even have time to try it yet.