04-templating.html
1 <!DOCTYPE html> 2 <html lang="en-US"> 3 <head> 4 <meta charset="utf-8"> 5 <meta name="viewport" content="width=device-width,initial-scale=1"> 6 <title>Templating | dt-cli</title> 7 <meta name="description" content="Documentations for dt-cli <https://github.com/blurgyy/dt>"> 8 <link rel="stylesheet" href="/assets/style.42fa774d.css"> 9 <link rel="modulepreload" href="/assets/plugin-vue_export-helper.f07d1dea.js"> 10 <link rel="modulepreload" href="/assets/Home.74659134.js"> 11 <link rel="modulepreload" href="/assets/features_04-templating.md.adcf805f.lean.js"> 12 <link rel="modulepreload" href="/assets/app.2b904881.js"> 13 14 <link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Roboto+Mono:wght@400;700&family=Rubik:wght@300..900&display=swap"> 15 <meta property="twitter:card" content="summary_large_image"> 16 <meta property="twitter:image" content="/home-everywhere.png"> 17 <meta property="og:image" content="/home-everywhere.png"> 18 <meta property="og:image:width" content="1200"> 19 <meta property="og:image:height" content="794"> 20 <meta name="twitter:title" content="Templating | dt-cli"> 21 <meta property="og:title" content="Templating | dt-cli"> 22 </head> 23 <body> 24 <div id="app"><!--[--><div class="theme"><header class="nav-bar" data-v-bd29f18c><div class="sidebar-button" data-v-bd29f18c><svg class="icon" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z" class></path></svg></div><a class="nav-bar-title" href="/" aria-label="dt-cli, back to home" data-v-bd29f18c data-v-47b7809c><!----> dt-cli</a><div class="flex-grow" data-v-bd29f18c></div><div class="nav" data-v-bd29f18c><nav class="nav-links" data-v-bd29f18c data-v-155438fc><!--[--><div class="item" data-v-155438fc><div class="nav-link" data-v-155438fc data-v-2624cfdf><a class="item active" href="/" data-v-2624cfdf>Overview <!----></a></div></div><div class="item" data-v-155438fc><div class="nav-link" data-v-155438fc data-v-2624cfdf><a class="item" href="/config/guide/" data-v-2624cfdf>Guide <!----></a></div></div><div class="item" data-v-155438fc><div class="nav-link" data-v-155438fc data-v-2624cfdf><a class="item" href="/config/key-references" data-v-2624cfdf>Key References <!----></a></div></div><!--]--><!----><div class="item" data-v-155438fc><div class="nav-link" data-v-155438fc data-v-2624cfdf><a class="item isExternal" href="https://github.com/blurgyy/dt" target="_blank" rel="noopener noreferrer" data-v-2624cfdf>GitHub <svg class="icon outbound" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewbox="0 0 100 100" width="15" height="15" data-v-2624cfdf><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path><polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></div></div></nav></div><!--[--><!--]--></header><aside class="sidebar" data-v-69967ef1><nav class="nav-links nav" data-v-69967ef1 data-v-155438fc><!--[--><div class="item" data-v-155438fc><div class="nav-link" data-v-155438fc data-v-2624cfdf><a class="item active" href="/" data-v-2624cfdf>Overview <!----></a></div></div><div class="item" data-v-155438fc><div class="nav-link" data-v-155438fc data-v-2624cfdf><a class="item" href="/config/guide/" data-v-2624cfdf>Guide <!----></a></div></div><div class="item" data-v-155438fc><div class="nav-link" data-v-155438fc data-v-2624cfdf><a class="item" href="/config/key-references" data-v-2624cfdf>Key References <!----></a></div></div><!--]--><!----><div class="item" data-v-155438fc><div class="nav-link" data-v-155438fc data-v-2624cfdf><a class="item isExternal" href="https://github.com/blurgyy/dt" target="_blank" rel="noopener noreferrer" data-v-2624cfdf>GitHub <svg class="icon outbound" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewbox="0 0 100 100" width="15" height="15" data-v-2624cfdf><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path><polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></div></div></nav><!--[--><!--]--><ul class="sidebar-links" data-v-69967ef1><!--[--><li class="sidebar-link"><a class="sidebar-link-item" href="/">👀 Overview</a><!----></li><li class="sidebar-link"><a class="sidebar-link-item" href="/features/">💠Features</a><ul class="sidebar-links"><li class="sidebar-link"><a class="sidebar-link-item" href="/features/01-host-specific">Host-specific Syncing</a><!----></li><li class="sidebar-link"><a class="sidebar-link-item" href="/features/02-scope">Priority Resolving</a><!----></li><li class="sidebar-link"><a class="sidebar-link-item" href="/features/03-filename-manipulating">Filename Manipulating</a><!----></li><li class="sidebar-link"><a class="sidebar-link-item active" href="/features/04-templating">Templating</a><ul class="sidebar-links"><li class="sidebar-link"><a class="sidebar-link-item" href="#background">Background</a><!----></li><li class="sidebar-link"><a class="sidebar-link-item" href="#syntax">Syntax</a><ul class="sidebar-links"><li class="sidebar-link"><a class="sidebar-link-item" href="#configuring">Configuring</a><!----></li><li class="sidebar-link"><a class="sidebar-link-item" href="#applying">Applying</a><!----></li></ul></li></ul></li></ul></li><li class="sidebar-link"><a class="sidebar-link-item" href="/installation">🚀 Installation</a><!----></li><li class="sidebar-link"><a class="sidebar-link-item" href="/contributing">📨 Contributing</a><!----></li><!--]--></ul><!--[--><!--]--></aside><div class="sidebar-mask"></div><main class="page" data-v-7f9de529><div class="container" data-v-7f9de529><!--[--><!--]--><div style="position:relative;" class="content" data-v-7f9de529><div><h1 id="templating-examples" tabindex="-1">Templating<sub>[<a href="/config/guide/07-templating.html"><strong>Examples</strong></a>]</sub> <a class="header-anchor" href="#templating-examples" aria-hidden="true">#</a></h1><h2 id="background" tabindex="-1">Background <a class="header-anchor" href="#background" aria-hidden="true">#</a></h2><p>As is always the case, there are quite a few applications that share a same set of properties. For example, we want to have uniform looks for Qt and GTK applications. Templating utility is developed under the <strong>DRY</strong> (<strong>D</strong>on't <strong>R</strong>epeat <strong>Y</strong>ourself) principle, it allows to manage these shared properties in one place: change once, apply everywhere.</p><h2 id="syntax" tabindex="-1">Syntax <a class="header-anchor" href="#syntax" aria-hidden="true">#</a></h2><h3 id="configuring" tabindex="-1">Configuring <a class="header-anchor" href="#configuring" aria-hidden="true">#</a></h3><p>To manage shared properties, add a section <code>[context]</code> to <code>dt-cli</code>'s config file. For example, to set a property named <code>cursor-size</code> for the <code>gui</code> group to value <code>24</code>:</p><div class="language-toml"><pre><code><span class="token comment"># ~/.config/dt/cli.toml</span> 25 <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span> 26 <span class="token punctuation">[</span><span class="token table class-name">context</span><span class="token punctuation">]</span> 27 <span class="token key property">gui.cursor-size</span> <span class="token punctuation">=</span> <span class="token number">24</span> 28 <span class="token comment">## Or, as TOML allows it:</span> 29 <span class="token comment">#[context.gui]</span> 30 <span class="token comment">#cursor-size = 24</span> 31 <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span> 32 </code></pre></div><p>See the <a href="/config/guide/07-templating.html">configuration guide</a> for detailed usages.</p><h3 id="applying" tabindex="-1">Applying <a class="header-anchor" href="#applying" aria-hidden="true">#</a></h3><p><code>dt-cli</code> uses Rust's <a href="https://docs.rs/handlebars/latest/handlebars/" target="_blank" rel="noopener noreferrer">Handlebars crate</a> to render templates. Handlebars is tested and widely used, according to its descriptions:</p><div class="info custom-block"><p class="custom-block-title">INFO</p><p>Handlebars-rust is the template engine that renders the official Rust website <a href="http://rust-lang.org" target="_blank" rel="noopener noreferrer">rust-lang.org</a>.</p></div><p>For example, to apply a property named <code>cursor-size</code> to all source files under the <code>gui</code> group:</p><div class="language-ini"><pre><code>... 33 <span class="token key attr-name">gtk-cursor-theme-size</span><span class="token punctuation">=</span><span class="token value attr-value">{{{ gui.cursor-size }}}</span> 34 ... 35 </code></pre></div><p>With <code>context.gui.cursor-size</code> being set to <code>24</code> (as in <a href="#configuring">previous section</a>), the above template (in a group with name <code>gui</code>) will be rendered as:</p><div class="language-ini"><pre><code><span class="token comment"># ~/.config/gtk-3.0/settings.ini</span> 36 ... 37 <span class="token key attr-name">gtk-cursor-theme-size</span><span class="token punctuation">=</span><span class="token value attr-value">24</span> 38 ... 39 </code></pre></div><div class="warning custom-block"><p class="custom-block-title">INFO</p><p>The time consumed while rendering can be quite noticeable if the template being rendered is huge. To skip rendering for a group, use the <a href="/config/key-references.html#renderable-1"><code>renderable = false</code></a> option in the config file.</p></div><p><code>dt-cli</code> also supports basic control flow syntaxes like looping and conditioning, and other helper directives that boosts the productiveness of the templating system. Check out the <a href="/config/guide/07-templating.html#advanced-syntaxes">hands-on guide</a> if interested!</p></div></div><footer class="page-footer" data-v-7f9de529 data-v-f487177c><div class="edit" data-v-f487177c><div class="edit-link" data-v-f487177c data-v-7ca0917a><!----></div></div><div class="updated" data-v-f487177c><!----></div></footer><div class="next-and-prev-link" data-v-7f9de529 data-v-34768bda><div class="container" data-v-34768bda><div class="prev" data-v-34768bda><a class="link" href="/features/03-filename-manipulating" data-v-34768bda><svg xmlns="http://www.w3.org/2000/svg" viewbox="0 0 24 24" class="icon icon-prev" data-v-34768bda><path d="M19,11H7.4l5.3-5.3c0.4-0.4,0.4-1,0-1.4s-1-0.4-1.4,0l-7,7c-0.1,0.1-0.2,0.2-0.2,0.3c-0.1,0.2-0.1,0.5,0,0.8c0.1,0.1,0.1,0.2,0.2,0.3l7,7c0.2,0.2,0.5,0.3,0.7,0.3s0.5-0.1,0.7-0.3c0.4-0.4,0.4-1,0-1.4L7.4,13H19c0.6,0,1-0.4,1-1S19.6,11,19,11z"></path></svg><span class="text" data-v-34768bda>Filename Manipulating</span></a></div><div class="next" data-v-34768bda><a class="link" href="/installation" data-v-34768bda><span class="text" data-v-34768bda>🚀 Installation</span><svg xmlns="http://www.w3.org/2000/svg" viewbox="0 0 24 24" class="icon icon-next" data-v-34768bda><path d="M19.9,12.4c0.1-0.2,0.1-0.5,0-0.8c-0.1-0.1-0.1-0.2-0.2-0.3l-7-7c-0.4-0.4-1-0.4-1.4,0s-0.4,1,0,1.4l5.3,5.3H5c-0.6,0-1,0.4-1,1s0.4,1,1,1h11.6l-5.3,5.3c-0.4,0.4-0.4,1,0,1.4c0.2,0.2,0.5,0.3,0.7,0.3s0.5-0.1,0.7-0.3l7-7C19.8,12.6,19.9,12.5,19.9,12.4z"></path></svg></a></div></div></div><!--[--><!--]--></div></main></div><!----><!--]--></div> 40 <script>__VP_HASH_MAP__ = JSON.parse("{\"config_guide_01-default-behaviours.md\":\"bcd25e88\",\"config_guide_02-groups.md\":\"54c6ed9d\",\"config_guide_03-syncing-methods.md\":\"1ebfcfdd\",\"config_guide_04-host-specific.md\":\"3be46361\",\"config_guide_05-priority.md\":\"f64e8a7d\",\"config_guide_06-filename-manipulating.md\":\"aba8fc76\",\"config_guide_07-templating.md\":\"f7f33668\",\"config_guide_99-error-handling.md\":\"e05b2975\",\"config_guide_index.md\":\"a2153e28\",\"config_key-references.md\":\"df45381e\",\"contributing.md\":\"8dac4517\",\"features_01-host-specific.md\":\"857720b4\",\"features_02-scope.md\":\"bdd8e6d2\",\"features_03-filename-manipulating.md\":\"4f4fb6b1\",\"features_04-templating.md\":\"adcf805f\",\"features_index.md\":\"27034f59\",\"index.md\":\"d0fcd5a8\",\"installation.md\":\"d7a4ab95\"}")</script> 41 <script type="module" async src="/assets/app.2b904881.js"></script> 42 43 </body> 44 </html>