With AsciiDoctor, how to pass variables in source and example blocks?
Asked Answered
I

1

9

Any one knows how to pass variables {var} into [source] blocks and example blocks (with ====) in Asciidoc?

I have tried the following

:country: France
:city: Shanghai

[source]
----
print("{country} is a country")
print("{city} is a city")
----

.Example
====
{country} is a country +
{city} is a city
====

.Example with better alignment
====
    {country} is a country
    {city} is a city
====

But this is what I get:

result

Actually the first "example" is working but it is not the ideal solution because:

  • It does not have the grey area like other examples
  • I need to add a + at the end of each line

Looking forward your inputs. Thanks in advance!

Impenetrable answered 27/9, 2018 at 2:43 Comment(0)
S
11

As described here you need to switch on attribute replacement in code blocks. You can achieve it with [subs="attributes"]complete example should look something like:

[source, subs="attributes"]
----
  print("{country} is a country")
  print("{city} is a city")
----

.Example with better alignment
====
[subs="attributes"]
    {country} is a country
    {city} is a city
====
Susannasusannah answered 27/9, 2018 at 8:6 Comment(3)
Awesome! Note that you could simplify with [source, subs="attributes"]... but the question remains for the "Example with better alignment" that uses ====: any idea?Impenetrable
Just put [subs="attributes"] under =. (Difficult to read in comment) .Example with better alignment ==== [subs="attributes"] {country} is a country {city} is a city ==== Or did I not understand you correct? (Updated answer)Rhizocarpous
Asciidoc novice here, but this only worked for me after adding an empty line before [source, subs="attributes"]Diplostemonous

© 2022 - 2024 — McMap. All rights reserved.