# Using template language to generate dot files

**URL:** <https://forum.graphviz.org/t/using-template-language-to-generate-dot-files/1189>\
**Category:** Dev\
**Created:** [May 18, 2022, 3:08pm UTC](https://forum.graphviz.org/t/using-template-language-to-generate-dot-files/1189 "2022-05-18T15:08:36Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ooker](https://sea2.discourse-cdn.com/graphviz/user_avatar/forum.graphviz.org/ooker/32/385_2.png) [@Ooker](https://forum.graphviz.org/u/Ooker)\
**Post date:** [May 18, 2022, 3:08pm UTC](https://forum.graphviz.org/t/using-template-language-to-generate-dot-files/1189/1 "2022-05-18T15:08:36Z")

</div>

I wonder if we have any example for using a template language to generate the dot files. I’m trying to rewrite my plugin [Graphvidian](https://github.com/ooker777/Graphvidian) (importing data from Obsidian).

---

<div class="post-metadata">

**Author:** ![steveroush](https://avatars.discourse-cdn.com/v4/letter/s/a9adbd/32.png) [@steveroush](https://forum.graphviz.org/u/steveroush)\
**Post date:** [May 19, 2022, 12:06am UTC](https://forum.graphviz.org/t/using-template-language-to-generate-dot-files/1189/2 "2022-05-19T00:06:40Z")

</div>

Good question. This is an on-going need.  
Here are some answers from the forum & from [stackoverflow](https://stackoverflow.com):

- [Using node attributes in HTML style label](https://forum.graphviz.org/t/using-node-attributes-in-html-style-label/356)
- [graphviz - Set pre-defined node styles? - Stack Overflow](https://stackoverflow.com/questions/63140881/set-pre-defined-node-styles/63142066#63142066)
- [How can I create named edge "types" in Graphviz/dot/neato? - Stack Overflow](https://stackoverflow.com/questions/46595101/how-can-i-create-named-edge-types-in-graphviz-dot-neato/70023759#70023759)
- [flowchart - Can you build reusable styles in GraphViz? - Stack Overflow](https://stackoverflow.com/questions/27262465/can-you-build-reusable-styles-in-graphviz)  
And future solutions:
- [[Other] [New Feature Request] Add a CSS-like "class" attribute (#694) · Issues · graphviz / graphviz · GitLab](https://gitlab.com/graphviz/graphviz/-/issues/694)
- [Styles for GraphViz (#1566) · Issues · graphviz / graphviz · GitLab](https://gitlab.com/graphviz/graphviz/-/issues/1566)
- [[Other] Classes for more layout less code (#673) · Issues · graphviz / graphviz · GitLab](https://gitlab.com/graphviz/graphviz/-/issues/673)

---

<div class="post-metadata">

**Author:** ![smattr](https://sea2.discourse-cdn.com/graphviz/user_avatar/forum.graphviz.org/smattr/32/85_2.png) [@smattr](https://forum.graphviz.org/u/smattr)\
**Post date:** [May 19, 2022, 1:51am UTC](https://forum.graphviz.org/t/using-template-language-to-generate-dot-files/1189/3 "2022-05-19T01:51:52Z")

</div>

Not entirely sure if this is what the OP is after, but the Graphviz language is partly designed such that the C pre-processor can be used on sources. So you can do some creative things with C macros.

For something more advanced you might want to use something like [Jinja](https://palletsprojects.com/p/jinja/).

---

<div class="post-metadata">

**Author:** ![Ooker](https://sea2.discourse-cdn.com/graphviz/user_avatar/forum.graphviz.org/ooker/32/385_2.png) [@Ooker](https://forum.graphviz.org/u/Ooker)\
**Post date:** [May 19, 2022, 1:09pm UTC](https://forum.graphviz.org/t/using-template-language-to-generate-dot-files/1189/4 "2022-05-19T13:09:05Z")

</div>

Are there any project that already use template language for graphviz so that I can use and don’t have to reinvent the wheel?

---

<div class="post-metadata">

**Author:** ![steveroush](https://avatars.discourse-cdn.com/v4/letter/s/a9adbd/32.png) [@steveroush](https://forum.graphviz.org/u/steveroush)\
**Post date:** [May 19, 2022, 3:15pm UTC](https://forum.graphviz.org/t/using-template-language-to-generate-dot-files/1189/5 "2022-05-19T15:15:44Z")

</div>

You might search github & gitlab, otherwise reinventing the wheel.

---

<div class="post-metadata">

**Author:** ![mark](https://sea2.discourse-cdn.com/graphviz/user_avatar/forum.graphviz.org/mark/32/84_2.png) [@mark](https://forum.graphviz.org/u/mark)\
**Post date:** [May 20, 2022, 6:59am UTC](https://forum.graphviz.org/t/using-template-language-to-generate-dot-files/1189/6 "2022-05-20T06:59:42Z")

</div>

The tricky bit in templating is usually the string-escaping for different contexts. So you probably can’t just use (say) an HTML templating engine and use that for Graphviz; Graphviz probably has different escaping rules for strings.

Without knowing much about your problem you’re trying to solve, usually instead of templating, I’d encourage using Graphviz libraries/APIs to build up a dot-string then output that. Less likely to screw up the string-escaping.

---

<div class="post-metadata">

**Author:** ![Ooker](https://sea2.discourse-cdn.com/graphviz/user_avatar/forum.graphviz.org/ooker/32/385_2.png) [@Ooker](https://forum.graphviz.org/u/Ooker)\
**Post date:** [May 22, 2022, 9:26am UTC](https://forum.graphviz.org/t/using-template-language-to-generate-dot-files/1189/7 "2022-05-22T09:26:45Z")

</div>

if there is no problem from string-escaping, would there be any other advantages of using the API to build up the dot string?

---

<div class="post-metadata">

**Author:** ![mark](https://sea2.discourse-cdn.com/graphviz/user_avatar/forum.graphviz.org/mark/32/84_2.png) [@mark](https://forum.graphviz.org/u/mark)\
**Post date:** [May 22, 2022, 9:58am UTC](https://forum.graphviz.org/t/using-template-language-to-generate-dot-files/1189/8 "2022-05-22T09:58:10Z")

</div>

Depends what problem you’re trying to solve?

---

<div class="post-metadata">

**Author:** ![Ooker](https://sea2.discourse-cdn.com/graphviz/user_avatar/forum.graphviz.org/ooker/32/385_2.png) [@Ooker](https://forum.graphviz.org/u/Ooker)\
**Post date:** [May 22, 2022, 10:30am UTC](https://forum.graphviz.org/t/using-template-language-to-generate-dot-files/1189/9 "2022-05-22T10:30:27Z")

</div>

Well I just want to auto generate graphs with data from Obsidian API (Javascript)
