Pass parameter from AsciiDoc CLI to HAML template

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

Pass parameter from AsciiDoc CLI to HAML template

wesruv
Hi, I'd like to provide slightly different markup in document.html.haml depending on whether or not we're compiling for local, dev, qa, or production.

I was thinking the easiest way would be to pass a flag into the AsciiDoctor CLI.

My wheelhouse is web front-end development, so I'm a little out of my depth.

I searched through the documentation, found this option in the CLI:
-a, --attribute=ATTRIBUTE

I tried to figure out how an attribute might show up (if it shows up) in HAML, but didn't get very far.

Any help would be greatly appreciated, thank you!
Reply | Threaded
Open this post in threaded view
|

Re: Pass parameter from AsciiDoc CLI to HAML template

abelsromero
Welcome!

To obtain the value inside content use #{@document.attr :cosa}, where "cosa" is the name of the attribute you passed to the cli. Also, to check if it is present use #{@document.attr? :cosa}.
You can also obtain attributes defined in the docs with this.

If you are not in content, for instance and if, remove the surrouding #{}.
Reply | Threaded
Open this post in threaded view
|

Re: Pass parameter from AsciiDoc CLI to HAML template

wesruv
This post was updated on .
Thanks, that was extremely helpful!

-------
Updating my example per discussion (see the original below)

For posterity, my ascii doctor command is:
asciidoctor -T pantheon/templates/haml/html5 -a pantheonenv=localwebassets dev-assets/ascii-doc-styleguide.adoc

-a env=localwebassets being the part that sets the variable for HAML

Pantheon is the name of the project, so I'm namespacing env with that.

Then in HAML I have:
    - if (@document.attr :pantheonenv) == 'localwebassets'
      %link(rel="stylesheet" href="https://static.redhat.com/libs/redhat/redhat-font/2/webfonts/red-hat-font.css")
      %link(rel="stylesheet" href="rhdocs.css")

Alternatively you can use this syntax for the condition, but I prefer the above.
    - if @document.attr? :pantheonenv, 'localwebassets'


--------
Original post:

For posterity, my ascii doctor command is:
asciidoctor -T pantheon/templates/haml/html5 -a env=localwebassets dev-assets/ascii-doc-styleguide.adoc

-a env=localwebassets being the part that sets the variable for HAML

Then in HAML I have:

    - if @document.attr? :env, 'localwebassets'
      %link(rel="stylesheet" href="https://static.redhat.com/libs/redhat/redhat-font/2/webfonts/red-hat-font.css")
      %link(rel="stylesheet" href="rhdocs.css")

Very confused why I'm using @document.attr? :env, instead of @document.attr? :env ==, BUT I confirmed the if statement evaluates properly if the var is different.

Thanks again!
Reply | Threaded
Open this post in threaded view
|

Re: Pass parameter from AsciiDoc CLI to HAML template

abelsromero
It it helps, under the hood all templates get an insance of the Asciidoctor processor (https://github.com/asciidoctor/asciidoctor/blob/88bc891d6a0143f3a2142f41d29cabe3cb21fe3f/lib/asciidoctor/document.rb#L49). The attributes are stored in a hashmap, thus the "colon" notation.

Then, for each template, an instance of the correspondent node/block matching that part of he document in the AST. So, if in doubt you can check the code to see the property names.

Truth, I am not familiar with Ruby, but this with some trial & error has helped me a couple of times when using templates and also extensions.
Reply | Threaded
Open this post in threaded view
|

Re: Pass parameter from AsciiDoc CLI to HAML template

wesruv
Oh, nice!

I've never done anything for a Ruby backend, but knowing where the data is prepped for the template is _extremely_ helpful.
Reply | Threaded
Open this post in threaded view
|

Re: Pass parameter from AsciiDoc CLI to HAML template

mojavelinux
Administrator
In reply to this post by wesruv
> Very confused why I'm using @document.attr? :env, instead of @document.attr? :env ==, BUT I confirmed the if statement evaluates properly if the var is different.

This code:

if @document.attr? :env, 'localwebassets'

is shorthand for

if (@document.attr :env) == 'localwebassets'

And :env is just shorthand for 'env' (since the method coerces it to a string)

On another note, I don't recommend using the attribute name "env" since that's an intrinsic attribute used by the processor to identify the environment. It won't break your code, but I still recommend using another name.

Best Regards,

-Dan

--
Dan Allen | @mojavelinux | https://twitter.com/mojavelinux
Reply | Threaded
Open this post in threaded view
|

Re: Pass parameter from AsciiDoc CLI to HAML template

wesruv
Ahh, makes sense about naming it env. Thanks for taking the time @mojavelinux

Sorry, I didn't explain my confusion well, it's that this:

(@document.attr? :env) == 'localwebassets'

Evaluates to false. I don't really understand the comma syntax and can't find any documentation on it... I figured out it existed by looking through the rest of the templates.

Doesn't seem to be something this project has to fill in since I'm guessing it's a HAML thing, but I can't even find particularly good documentation on HAML, for instance the official docs don't seem to cover if statements:
http://haml.info/docs/yardoc/file.REFERENCE.html

The best docs I can find are in Stack Overflow

If there's interest in porting the the templating syntax to something like nunjucks, mustache, or some other template language that has a more docs I would definitely be interested in helping!

AsciiDoc(ter) is going to be a good sized part of my job at Redhat through the end of the year.
Reply | Threaded
Open this post in threaded view
|

Re: Pass parameter from AsciiDoc CLI to HAML template

mojavelinux
Administrator
> (@document.attr? :env) == 'localwebassets'}

This is not a valid use of the attr? method. The attr? method combines the attr method (without ?) with a comparison operation. So you either write:

(@document.attr :env) == 'localwebassets'

or

@document.attr? :env, 'localwebassets

See the difference in the two methods?




If there's interest in porting the the templating syntax to something like nunjucks, mustache, or some other template language that has a more docs
 
It has nothing to do with the template language (and Asciidoctor supports every template language supported by Tilt). This is about the Asciidoctor model to which the template has access. The template has access to the model for the node being converted (which I linked to above).

-Dan

--
Dan Allen | @mojavelinux | https://twitter.com/mojavelinux
Reply | Threaded
Open this post in threaded view
|

Re: Pass parameter from AsciiDoc CLI to HAML template

wesruv
Gooootcha, thanks!