all repos — h3rald @ 5ce44db426a8f765bb63a8201daaf6191a6d8848

The sources of https://h3rald.com

contents/glyph/book/text_editing/conditionals.html

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
-----
title: "Glyph – Conditional Macros"
content-type: page
-----
<nav class="navigation"><a href="/glyph/book/text_editing/inclusions.html">← Content Reuse</a> | <a href="/glyph/book/index.html">Contents</a> | <a href="/glyph/book/text_editing/evaluation.html">Simple Programming and Code Evaluation →</a></nav>
  <p>Sometimes you may want text to be included in a document only if certain conditions are satisfied. For example, you may want to display a disclaimer section only if the document is a draft (see the <a href="/glyph/book/config/document.html#s_document_draft"><code>document.draft</code></a> setting), or use a particular stylesheet only if when you generate a <span class="caps">PDF</span> document.</p>
<p>To do so, you can use the <a href="/glyph/book/macros/macros_core.html#m_condition"><code>condition</code></a> macro (aliased by <code>?</code>), and a set of additional macros that can be used as conditional operators i.e.:</p>
<ul>
	<li><a href="/glyph/book/macros/macros_core.html#m_eq"><code>eq</code></a> macro</li>
	<li><a href="/glyph/book/macros/macros_core.html#m_not"><code>not</code></a> macro</li>
	<li><a href="/glyph/book/macros/macros_core.html#m_and"><code>and</code></a> macro</li>
	<li><a href="/glyph/book/macros/macros_core.html#m_or"><code>or</code></a> macro</li>
</ul>
<p>Consider the following code:</p>
  <div class="CodeRay">
  <div class="code"><pre><span class="line-numbers"><a href="#n1" name="n1">1</a></span>?[$[document.draft]|
<span class="line-numbers"><a href="#n2" name="n2">2</a></span>This is a first draft of the Glyph Book|
<span class="line-numbers"><a href="#n3" name="n3">3</a></span>This is the official version of the Glyph Book]</pre></div>
</div>

  <p>In this case, if <code>document.draft</code> is set to <code>true</code>, &#8220;This is a first draft of the Glyph Book&#8221; will be displayed; if not, &#8220;This is the official version of the Glyph Book&#8221; will be displayed instead.</p>
<p>The <a href="/glyph/book/macros/macros_core.html#m_condition"><code>condition</code></a> macro takes up to three parameters:</p>
<ol>
	<li>the condition to evaluate</li>
	<li>the text to include in the document only if the condition is satisfied.</li>
	<li><em>(Optional)</em> the text to include in the document if the condition is <em>not</em> satisfied.</li>
</ol>
<p>Note that <em>all</em> parameters can contain macros, of course, so you can write things like:</p>
  <div class="CodeRay">
  <div class="code"><pre><span class="line-numbers"><a href="#n1" name="n1">1</a></span>?[and[
<span class="line-numbers"><a href="#n2" name="n2">2</a></span>    eq[$[document.output]|pdf]
<span class="line-numbers"><a href="#n3" name="n3">3</a></span>    |
<span class="line-numbers"><a href="#n4" name="n4">4</a></span>    eq[$[tools.pdf_generator]|prince]
<span class="line-numbers"><a href="#n5" name="n5">5</a></span>    ]
<span class="line-numbers"><a href="#n6" name="n6">6</a></span>  |
<span class="line-numbers"><a href="#n7" name="n7">7</a></span>  style[pagination.css]]</pre></div>
</div>

  <p>In this case, the <code>pagination.css</code> stylesheet is included only when you're generating a PDF document using Prince XML.</p>
  <section class="section">
<header><h1 id="h_42" class="toc">Results of conditional expressions</h1></header>
<p>The <a href="/glyph/book/macros/macros_core.html#m_condition"><code>condition</code></a> macro in Glyph works in a similar way as conditionals in programming languages: if the conditional expression (supplied as first parameter) is satisfied then the second parameter is executed or displayed. But when is a conditional expression satisfied? Glyph is a simple mini-language to perform text manipulation, and has no types, it can only understand text, therefore:</p>
<ul>
	<li>A conditional expression is satisfied if it evaluates to a non-empty string except &#8220;false&#8221;.</li>
	<li>A conditional expression is not satisfied if it evaluates to an empty string or the string &#8220;false&#8221;.</li>
</ul>

</section>
<nav class="navigation"><a href="/glyph/book/text_editing/inclusions.html">← Content Reuse</a> | <a href="/glyph/book/index.html">Contents</a> | <a href="/glyph/book/text_editing/evaluation.html">Simple Programming and Code Evaluation →</a></nav>