{"id":16105208,"url":"https://github.com/mikeckennedy/markdown-subtemplate","last_synced_at":"2025-08-08T13:21:22.154Z","repository":{"id":41370885,"uuid":"241507935","full_name":"mikeckennedy/markdown-subtemplate","owner":"mikeckennedy","description":"A template engine to render Markdown with external template imports and basic variable replacements. #pypackage","archived":false,"fork":false,"pushed_at":"2020-12-19T00:19:06.000Z","size":633,"stargazers_count":61,"open_issues_count":5,"forks_count":6,"subscribers_count":4,"default_branch":"master","last_synced_at":"2024-04-28T23:23:52.616Z","etag":null,"topics":["django","flask","jinja2","markdown","pyramid","python","web"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mikeckennedy.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-02-19T01:44:01.000Z","updated_at":"2024-02-07T19:44:47.000Z","dependencies_parsed_at":"2022-07-30T07:18:05.260Z","dependency_job_id":null,"html_url":"https://github.com/mikeckennedy/markdown-subtemplate","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mikeckennedy%2Fmarkdown-subtemplate","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mikeckennedy%2Fmarkdown-subtemplate/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mikeckennedy%2Fmarkdown-subtemplate/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mikeckennedy%2Fmarkdown-subtemplate/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mikeckennedy","download_url":"https://codeload.github.com/mikeckennedy/markdown-subtemplate/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243806070,"owners_count":20350775,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["django","flask","jinja2","markdown","pyramid","python","web"],"created_at":"2024-10-09T19:08:52.786Z","updated_at":"2025-03-16T08:32:32.111Z","avatar_url":"https://github.com/mikeckennedy.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# markdown-subtemplate \n[![](https://img.shields.io/badge/python-3.6+-blue.svg)](https://www.python.org/downloads/) \n[![](https://img.shields.io/pypi/l/markdown-subtemplate.svg)](https://github.com/mikeckennedy/markdown-subtemplate/blob/master/LICENSE)\n[![](https://img.shields.io/pypi/dm/markdown-subtemplate.svg)](https://pypi.org/project/markdown-subtemplate/)\n\nA template engine to render Markdown with external template imports and basic variable replacements.\n\n## Motivation\n\nWe often make a choice between data-driven server apps (typical Flask app), CMSes that let us edit content on the web such as WordPress, and even flat file systems like Pelican.\n\nThese are presented as an either-or. You either get a full database driven app or you get a CMS, but not both. This project is meant to help add CMS like features to your data-driven web apps and even author them as static markdown files.\n\nHere's how it works:\n\n1. You write standard markdown content.\n2. Markdown content can be shared and imported into your top-level markdown.\n3. Fragments of HTML can be used when css classes and other specializations are needed, but generally HTML is avoided.\n4. A dictionary of variables and their values to replace in the merged markdown is processed.\n5. Markdown content is converted to HTML and embedded in your larger site layout (e.g. within a Jinja2 template).\n6. Markdown transforms are cached to achieve very high performance regardless of the complexity of the content.\n\n## Standard workflow\n\nWrite markdown content, merge it with other markdown files, deliver it as HTML as part of your larger site.\n\n![](https://raw.githubusercontent.com/mikeckennedy/markdown-subtemplate/master/readme_resources/workflow_image_layout.png)\n\n## Usage\n\nTo use the library, simply install it.\n\n```bash\npip3 install markdown-subtemplate\n``` \n\nNext, write a markdown template, `page.md`:\n\n```markdown\n## This is a sub-title\n\n* Here's an entry\n* And another\n```\n\nRegister the template engine in your web app startup:\n\n```python\nfrom markdown_subtemplate import engine\n# Set the template folder so that when you ask for page.md \n# the system knows where to look.\n\nengine.set_template_folder(full_path_to_template_folder)\n```\n\nThen generate the HTML content via:\n\n```python\ndata = {'variable1': 'Value 1', 'variable2': 'Value 2'}\ncontents = engine.get_page('page.md', data)\n```\n\nFinally, pass the HTML fragment to be rendered in the larger page context:\n\n```python\n# A Pyramid view method:\n\n@view_config(route_name='landing', renderer='landing.pt')\ndef landing(request):\n    data = {'variable1': 'Value 1', 'variable2': 'Value 2'}\n    contents = engine.get_page('page.md', data)\n\n    return {\n        'name': 'Project name',\n        'contents': contents\n    }\n```\n\nAnd the larger website template grabs the content and renders it, `landing.pt`:\n\n```html\n...\n\u003cdiv\u003e\n    ${structure:contents}\n\u003c/div\u003e\n...\n```\n\n## Beware the danger!\n\nThis library is meant for INTERNAL usage only. It's to help you add CMS features to your app. It is **not** for taking user input and making a forum or something like that.\n\nTo allow for the greatest control, you can embed small fragments of HTML in the markdown (e.g. to add a CSS class or other actions). This means the markdown is processed in **UNSAFE** mode. It would allow for script injection attacks if opened to the public.\n\n## Extensibility\n\n`markdown-subtemplate` has three axis of extensibility:\n\n* **Storage** - Load markdown content from disk, db, or elsewhere.\n* **Caching** - Cache generated markdown and HTML in memory, DB, or you pick!\n* **Logging** - If you are using a logging framework, plug in logging messages from the library.\n\nSee the [extensibility doc](https://github.com/mikeckennedy/markdown-subtemplate/blob/master/extensibility.md) for details and examples.\n \n \n## Nested markdown\n\nOne of the reason's this project exists, rather than just passing markdown text to a markdown library is allowing nesting / importing of markdown files.\n\nIf you have page fragments that need to appear more than once, create a dedicated markdown import file that can be managed and versioned in one place. Here's how:\n\n### Created an imported file in TEMPLATES/_shared\n\nAll imported markdown files are located in subpaths of `TEMPLATES/_shared` where `TEMPLATES` is the path you set during startup.\n\n```\nTEMPLATES\n    |- _shared\n        |- contact.md\n        |- footer.md\n    |-pages\n        | - page.md\n        | - about.md\n```\n\nWrite the imported / shared markdown, `contact.md`:\n\n```markdown\nContact us via email [us@us.com](mailto:us.com) or on \nTwitter via [@us](https://twitter.com/us)\n```\n\nThen in your page, e.g. `page.md` you can add an import statement:\n\n```markdown\n# Our amazing page\n\nHere is some info **about the page**. It's standard markdown.\n\nWant to contact us? Here are some options:\n[IMPORT CONTACT]\n\nAnd a footer:\n[IMPORT FOOTER]\n```\n\nThe resulting markdown is just replacing the `IMPORT` statements with the contents of those files, then passing the whole thing through a markdown to HTML processor.\n\n## Variables\n\n`markdown_subtemplate` has some limited support for variable replacements. Given this markdown page:\n\n```markdown\n# Example: $TITLE$\n\nWelcome to the $PROJECT$ project. Here are some details \n...\n```\n\nYou can populate the variable values with:\n\n```python\ndata = {'title': 'Markdown Transformers', 'project': 'sub templates'}\ncontents = engine.get_page('page.md', data)\n```\n\nNote that the variable names must be all-caps in the template. Missing variable statements in markdown that appear in the data dictionary are ignored.\n\n## Variables within nested templates\n\nOne problem you might run into is you have some reusable section, like this:\n\n```\n// shared_promo.md\n\nWe are running a special! Click here to see the featured item:\n\n[Our latest project, 50% off](/markdown-editor?utm_source={SOMETHING_FROM_THE_PAGE})\n```\n\nIt's generally reusable, but you want that `SOMETHING_FROM_THE_PAGE` to vary depending on which page they are on. \nYou could do this in Python code but that would be out of band and actually a little hard to do. So you can\nnow define hierarchical variables in markdown. \n\nDefine nested variables as follows with `name=VARNAME` and `value=var-value`:\n\n```\n[VARIABLE VARNAME=\"var-value\"]\n```\n\nIn the following page, we choose a `source` and reuse this shared template:\n\n```\n// top-level.md\n\n# Welcome to the page\n\nPage content here...\n\n[VARIABLE SOMETHING_FROM_THE_PAGE=\"editor-youtube-landing-page\"]\n[IMPORT shared_promo]\n```\n\nThe result would be effective this markdown, then turned into HTML:\n\n```markdown\n# Welcome to the page\n\nPage content here...\n\nWe are running a special! Click here to see the featured item:\n\n[Our latest project, 50% off](/markdown-editor?utm_source=editor-youtube-landing-page)\n```\n\nJust like regular variables, they are enclosed in `{` and `}` and are upper case where they are used.\n\n\n\n## Requirements\n\nThis library requires **Python 3.6 or higher**. Because, *f-yes*! (f-strings).\n\n## Licence\n\n`markdown-subtemplate` is distributed under the MIT license.\n\n## Authors\n\n`markdown_subtemplate` was written by [Michael Kennedy](https://github.com/mikeckennedy).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmikeckennedy%2Fmarkdown-subtemplate","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmikeckennedy%2Fmarkdown-subtemplate","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmikeckennedy%2Fmarkdown-subtemplate/lists"}