Home
Head's Up: I'm in the middle of upgrading my site. Most things are in place, but there are something missing and/or broken including image alt text. Please bear with me while I'm getting things fixed.

Use Python's Built - In Template Strings To Generate Output

python
from string import Template

skeleton = """
The value is: $VALUE
"""

data = { "VALUE": "quick fox" }
template = Template(skeleton)
output = template.substitute(data)
print(output)
results start

This is a basic one where you pass it two paths and a data dictionary and it makes the output.

python
from string import Template

def make_page(template_path, output_path, data):
    with open(template_path) as _template:
        template = Template(_template.read())
        with open(output_path, 'w') as _output:
            _output.write(
                template.substitute(data)
            )

And here's a full boiler plate :

python
from string import Template

def data_builder():
    data = {}
    data['CODE'] = 'this is here'
    return data

def make_page(template_path, output_path, data):
    with open(template_path) as _template:
        template = Template(_template.read())
        with open(output_path, 'w') as _output:
            _output.write(
                template.substitute(data)
            )

if __name__ == "__main__":
    make_page(
        'src/PAGE.html',
        '../index.html',
        data_builder()
    )

** TL;DR

Create a template file named [TODO: Code shorthand span ] like this :

#+BLOCKNAME : template.txt #+begin _ src shell : tangle ~/Grimoire/ _ examples/template.txt

this is the name : $name and the color : $color

#+end _ src

Then make this

#+BLOCKNAME : generator.py #+begin _ src python : results output : dir ~/Grimoire/ _ examples : wrap example : post padder(data=*this*)

from string import Template

with open('template.txt') as _ tmpl : template = Template( _ tmpl.read()) output = template.substitute( name="Karl", color="Green" ) print(output)

#+end _ src

results start

** From A String

python
from string import Template

skeleton = Template('Name: $name - Color: $color')

output = skeleton.substitute(
  name="Karl", 
  color="Green"
)

print(output)
results start

#+REFERENCES :

docs : https : //docs.python.org/3/library/string.html#template - strings