Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rendering custom properties from jbake.properties into Thymeleaf or markdown templates #326

Closed
ottlinger opened this issue Jan 8, 2017 · 12 comments
Labels

Comments

@ottlinger
Copy link
Contributor

ottlinger commented Jan 8, 2017

I'd like to render my own properties into thymeleaf template files (the variable is correctly injected into asciidoc documents but does not work in thymeleaf template files)

Example

My jbake.properties contains:

foo_version=0.1.2-SNAPSHOT

and my menu.thyme tries to reference this in a menu item header:

<li><a th:with="rootpath=(${content.rootpath != null} ? ${content.rootpath} : '')" th:href="${rootpath}+'apidocs/index.html'" th:text="'Javadoc '+ ${foo_version} +' (external)'"></a></li>

but is rendered as

Javadoc null (external)

What do I do wrong? Is that a missing feature in jbake 2.5.0 or do I have to add another configuration optional to allow parsing self-defined variables?

BTW: I can easily reference

in Markdown and Asciidoc files and the variable is substituted as expected.

Thanks for any hint

@ancho
Copy link
Member

ancho commented Jan 9, 2017

Hi. You can reference configurations with configuration.[option]. In your case configuration.foo_version. See http://jbake.org/docs/2.5.0/#global

@ottlinger
Copy link
Contributor Author

ottlinger commented Jan 9, 2017

Thanks @ancho, after changing the template to:

<li><a th:with="rootpath=(${content.rootpath != null} ? ${content.rootpath} : '')" th:href="${rootpath}+'apidocs/index.html'" th:text="'Javadoc '+ ${configuration.foo_version} +' (external)'"></a></li>

the baking does not work and yields:

	at org.jbake.template.ThymeleafTemplateEngine.renderDocument(ThymeleafTemplateEngine.java:79)
	at org.jbake.template.DelegatingTemplateEngine.renderDocument(DelegatingTemplateEngine.java:65)
	at org.jbake.app.Renderer.render(Renderer.java:207)
	... 6 more
Caused by: ognl.OgnlException: source is null for getProperty(null, "foo_version")
	at org.thymeleaf.standard.expression.OGNLShortcutExpression.evaluate(OGNLShortcutExpression.java:89)
	at org.thymeleaf.standard.expression.OGNLVariableExpressionEvaluator.executeExpression(OGNLVariableExpressionEvaluator.java:310)
	at org.thymeleaf.standard.expression.OGNLVariableExpressionEvaluator.evaluate(OGNLVariableExpressionEvaluator.java:170)

Any ideas?

@ancho
Copy link
Member

ancho commented Jan 9, 2017

Hmm....have you tried to rename the property to foo.version in jbake.properties?

@ottlinger
Copy link
Contributor Author

ottlinger commented Jan 9, 2017

@ancho the references get resolved in markdown and asciidoc, only Thymeleaf seems to be problematic :-( That's why I thought I need to set an "enabling" property in jbake.properties for Thymeleaf, but did not find any.

BTW: Changing the property to foo.version seems to introduce a hierarchy and the error message changes to:

Caused by: ognl.OgnlException: source is null for getProperty(null, "foo")
	at org.thymeleaf.standard.expression.OGNLShortcutExpression.evaluate(OGNLShortcutExpression.java:89)

@ancho
Copy link
Member

ancho commented Jan 9, 2017

Ahh. Ok. I didn't get that. Sorry.

@ottlinger ottlinger changed the title Rendering additional properties into Thymeleaf templates Rendering custom properties from jbake.properties into Thymeleaf templates Jan 9, 2017
@ottlinger ottlinger changed the title Rendering custom properties from jbake.properties into Thymeleaf templates Rendering custom properties from jbake.properties into Thymeleaf or markdown templates Jan 9, 2017
@ottlinger
Copy link
Contributor Author

@ancho I rechecked my repository:

  • Asciidoc injection works with:
jbake.properties:
asciidoctor.attributes.export=true
foo_version=0.1.2-SNAPSHOT

example in adoc:
<version>{foo_version}</version>

while markdown and thymeleaf do neither work with/out the configuration prefix.

Is there a config option I missed? Thanks.

@ancho
Copy link
Member

ancho commented Jan 11, 2017

@ottlinger have you updated your project to jbake 2.5.0? notice that the version of thymeleaf is 3.0.2.

According to the documentation http://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#more-on-texts-and-variables you need to access variables with a tag attribute like th:text or th:utext.

I tried it with the following tag in a thymeleaf footer template like:

<p>foo version: <span th:text='${config.foo_version}'>1234</span></p>

Set foo_version=1.0.0-alpha in the jbake.properties file. And it rendered like expected to <p>foo version: <span>1.0.0-alpha</span></p>.

Compare your templates to the example project https://github.com/jbake-org/jbake-example-project-thymeleaf.

I hope that helps.

@ottlinger
Copy link
Contributor Author

@ancho Thanks for your help, again - I thought to have tried this option before, maybe ran into caching issues. The prefix config. allows injection of the variable into thymeleaf files.

Is there a simple/same way to do this in markdown? Neither prefix nor maven syntax with dollar seems to work. All I see is the version placeholder in my generated md file.

@ancho
Copy link
Member

ancho commented Jan 11, 2017

I don't think so. The MarkdownEngine doesn't seem to handle variables.
Maybe another markdown parser like flexmark-java implements something like that.

As pegdown has a big fat deprecation notice we should change it anyways....

@ottlinger
Copy link
Contributor Author

ok - can't be helped at the moment. At least Asciidoc and Thymeleaf work properly. I'm going to raise a PR updating the documentation to make this more clear.

@ancho thanks for figuring this out.

@ancho
Copy link
Member

ancho commented Jan 11, 2017

Nice. Thank you. I opened an issue to evaluate a new markdown parser. See #327

@jonbullock
Copy link
Member

Looks like @acho has solved the initial query - thanks for that :)

To clarify properties in the configuration are made available to all of the template engines via the config. prefix. However Asciidoctor does allow you to inject attributes so that are available when the AsciiDoc content is processed as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants