blob: 15b32aeca30f6471cd01539313cc83b472bb4ff0 [file] [log] [blame] [raw]
<!DOCTYPE html><html><head><meta charset="utf-8" /><meta content="IE=edge" http-equiv="X-UA-Compatible" /><meta content="width=device-width, initial-scale=1, maximum-scale=2.0, user-scalable=yes, minimal-ui=yes" name="viewport" /><title>facil.io - FIOBJ Mustache API</title><meta content="facil.io - FIOBJ Mustache API" name="description" /><link href="https://fonts.googleapis.com/css?family=Montserrat|Quicksand|Karla" rel="stylesheet" type="text/css" /><script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script><link href="/assets/styles/main.css" rel="stylesheet" type="text/css" /><script type="application/ld+json">{"@context":"http://schema.org","@type":"WebSite","url":"http://facil.io","name":"facil.io","description":"facil.io - a light web application framework in C, with support for HTTP, WebSockets and Pub/Sub out of the box.","keywords":"C, web, framework, websockets, websocket, realtime, real-time, easy","image":"http://facil.io/website/logo/facil-io.svg","author":[{"@type":"Person","name":"Bo (Myst)","url":"http://stackoverflow.com/users/4025095/myst","email":"bo(at)facil.io"}],"sourceOrganization":{"@context":"http://schema.org","@type":"Organization","name":"Plezi","url":"http://facil.io","description":"facil.io - a light web application framework in C, with support for HTTP, WebSockets and Pub/Sub out of the box.","logo":"http://facil.io/website/logo/facil-io.svg","image":"http://facil.io/website/logo/facil-io.svg","email":"bo(at)facil.io","member":[{"@type":"Person","name":"Bo (Myst)","url":"http://stackoverflow.com/users/4025095/myst","email":"bo(at)facil.io"}]}}</script><meta content="facil.io" name="apple-mobile-web-app-title" /><meta content="facil.io - the C Web Application Framework" name="application-name" /><meta content="#b91d47" name="msapplication-TileColor" /><meta content="/mstile-144x144.png" name="msapplication-TileImage" /><meta content="#ffffff" name="theme-color" /></head><body><a href="/" id="logo"></a><input id="show_nav" type="checkbox" /><nav id="top_nav"><ul><li><a href="/0.7.x/index">Latest Docs</a></li><li><a href="https://github.com/boazsegev/facil.io" target="_blank">Source Code</a></li><li><a href="javascript: change_themes();" id="theme">Night Theme</a></li></ul></nav><input id="show_sidebar" type="checkbox" /><nav id="side_bar"><h2 id="version-0-7-x"><a href="/0.7.x/index">Version 0.7.x</a></h2>
<h3 id="core-library"><a href="/0.7.x/fio">Core Library</a></h3>
<ul>
<li><a href="/0.7.x/fio#connection-protocol-management">Protocol Management</a></li>
<li><a href="/0.7.x/fio#running-facil-io">Running the IO reactor</a></li>
<li><a href="/0.7.x/fio#socket-connection-functions">Connection Functions</a></li>
<li><a href="/0.7.x/fio#event-task-scheduling">Event / Task Scheduling</a></li>
<li><a href="/0.7.x/fio#pub-sub-services">Pub/Sub Services</a></li>
<li><a href="/0.7.x/fio#the-custom-memory-allocator">Memory Allocation</a></li>
<li><a href="/0.7.x/fio#general-helpers">General Helpers</a></li>
<li><a href="/0.7.x/fio#linked-lists">Linked Lists</a></li>
<li><a href="/0.7.x/fio#string-helpers">String Helpers</a></li>
<li><a href="/0.7.x/fio#dynamic-arrays">Dynamic Arrays</a></li>
<li><a href="/0.7.x/fio#hash-maps-sets">Hash Maps / Sets</a></li>
<li><a href="/0.7.x/fio#version-and-compilation-related-macros">Compilation Macros</a></li>
<li><a href="/0.7.x/fio#weak-functions">Weak Functions</a></li>
</ul>
<h3 id="extensions"><a href="/0.7.x/extensions">Extensions</a></h3>
<!-- * [TLS (SSL)](/0.7.x/fio_tls) -->
<ul>
<li><a href="/0.7.x/http">HTTP / WebSockets</a></li>
<li><a href="/0.7.x/redis">Redis (client)</a></li>
<li><a href="/0.7.x/fio_cli">CLI (command line)</a></li>
</ul>
<h3 id="the-fiobj-types"><a href="/0.7.x/fiobj">The FIOBJ types</a></h3>
<ul>
<li><a href="/0.7.x/fiobj_core">Core Functions</a></li>
<li><a href="/0.7.x/fiobj_primitives">Primitives</a></li>
<li><a href="/0.7.x/fiobj_numbers">Numbers</a></li>
<li><a href="/0.7.x/fiobj_str">Strings</a></li>
<li><a href="/0.7.x/fiobj_ary">Array</a></li>
<li><a href="/0.7.x/fiobj_hash">HashMap</a></li>
<li><a href="/0.7.x/fiobj_data">Data Streams</a></li>
<li><a href="/0.7.x/fiobj_json">JSON</a></li>
<li><a href="/0.7.x/fiobj_mustache">Mustache</a></li>
</ul>
</nav><div id="md_container"><div class='toc'><ul>
<li>
<a href="#facil-io-fiobj-mustache-api">facil.io - FIOBJ Mustache API</a>
<ul>
<li>
<ul>
<li>
<a href="#mustache-template-initialization-api">Mustache Template Initialization API</a>
<ul>
<li>
<a href="#fiobj_mustache_load"><code>fiobj_mustache_load</code></a>
</li>
<li>
<a href="#fiobj_mustache_new"><code>fiobj_mustache_new</code></a>
</li>
<li>
<a href="#fiobj_mustache_free"><code>fiobj_mustache_free</code></a>
</li>
</ul>
</li>
<li>
<a href="#mustache-template-rendering-api">Mustache Template Rendering API</a>
<ul>
<li>
<a href="#fiobj_mustache_build"><code>fiobj_mustache_build</code></a>
</li>
<li>
<a href="#fiobj_mustache_build2"><code>fiobj_mustache_build2</code></a>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</div><h1 id="facil-io-fiobj-mustache-api">facil.io - FIOBJ Mustache API</h1>
<p><a href="http://mustache.github.io">Mustache logic-less template</a> rendering is supported using the <code>FIOBJ</code> types and the included mustache parser (which, much like the JSON parser, can be used independently).</p>
<p>Because both the JSON and the Mustache API are based on the FIOBJ type system, it&#39;s easy to render JSON data using a Mustache template.</p>
<h3 id="mustache-template-initialization-api">Mustache Template Initialization API</h3>
<p>Template data can be loaded from a file or from memory.</p>
<p>By loading the data from a file, or by providing a file name when loading the template from memory, partial templates are automatically resolved, loaded and parsed.</p>
<h4 id="fiobj_mustache_load"><code>fiobj_mustache_load</code></h4>
<pre><code class='highlight'><span class="n">mustache_s</span> <span class="o">*</span><span class="n">fiobj_mustache_load</span><span class="p">(</span><span class="n">fio_str_info_s</span> <span class="n">filename</span><span class="p">);</span>
</code></pre>
<p>Loads a mustache template from a file, converting it into an opaque instruction array.</p>
<p>This is the same as calling:</p>
<pre><code class='highlight'><span class="n">fiobj_mustache_new</span><span class="p">(.</span><span class="n">filename</span> <span class="o">=</span> <span class="n">filename</span><span class="p">.</span><span class="n">data</span><span class="p">,</span> <span class="p">.</span><span class="n">filename_len</span> <span class="o">=</span> <span class="n">filename</span><span class="p">.</span><span class="n">len</span><span class="p">);</span>
</code></pre>
<p>Returns an opaque <code>mustache_s</code> pointer to the instruction array or NULL (on error).</p>
<p>The <code>filename</code> argument should contain the template&#39;s file name.</p>
<p>Remember to call <code>fiobj_mustache_free</code> when done with the template.</p>
<h4 id="fiobj_mustache_new"><code>fiobj_mustache_new</code></h4>
<pre><code class='highlight'><span class="n">mustache_s</span> <span class="o">*</span><span class="n">fiobj_mustache_new</span><span class="p">(</span><span class="n">mustache_load_args_s</span> <span class="n">args</span><span class="p">);</span>
<span class="cp">#define fiobj_mustache_new(...) \
fiobj_mustache_new((mustache_load_args_s){__VA_ARGS__})
</span></code></pre>
<p>Loads a mustache template, either from memory or a file, converting it into an opaque instruction array.</p>
<p>Returns an opaque <code>mustache_s</code> pointer to the instruction array or NULL (on error).</p>
<p>The <code>mustache_s *</code> object can be used to render the same template multiple times concurrently.</p>
<p>The <code>fiobj_mustache_new</code> function is shadowed by the <code>fiobj_mustache_new</code> MACRO, which allows the function to accept any of the following &quot;named arguments&quot;:</p>
<ul>
<li><p><strong><code>filename</code></strong> the root template&#39;s file name.</p>
<pre><code class='highlight'><span class="c1">// argument type:</span>
<span class="kt">char</span> <span class="k">const</span> <span class="o">*</span><span class="n">filename</span>
</code></pre></li>
<li><p><strong><code>filename_len</code></strong> the file name&#39;s length.</p>
<pre><code class='highlight'><span class="c1">// argument type:</span>
<span class="kt">size_t</span> <span class="n">filename_len</span>
</code></pre></li>
<li><p><strong><code>data</code></strong> if both <code>data</code> and <code>data_len</code> are set, <code>data</code> will be used as the file&#39;s contents (instead of reading the file).</p>
<pre><code class='highlight'><span class="c1">// argument type:</span>
<span class="kt">char</span> <span class="k">const</span> <span class="o">*</span><span class="n">data</span>
</code></pre></li>
<li><p><strong><code>data_len</code></strong> if set, <code>data</code> will be used as the file&#39;s contents.</p>
<pre><code class='highlight'><span class="c1">// argument type:</span>
<span class="kt">size_t</span> <span class="n">data_len</span>
</code></pre></li>
<li><p><strong><code>err</code></strong> a container for any template load errors.</p>
<pre><code class='highlight'><span class="c1">// argument type:</span>
<span class="n">mustache_error_en</span> <span class="o">*</span><span class="n">err</span>
</code></pre></li>
</ul>
<p>Remember to call <code>fiobj_mustache_free</code> when done with the template.</p>
<p><strong>Note</strong>:</p>
<p>By setting the <code>filename</code> argument even when the <code>data</code> argument exists, it will allow path resolution for partial templates. Otherwise, there is no way to know where to find the partial templates.</p>
<p>The <code>mustache_error_en</code> type is an <code>enum</code> which, if set, will contain any of the following:</p>
<pre><code class='highlight'><span class="k">typedef</span> <span class="k">enum</span> <span class="n">mustache_error_en</span> <span class="p">{</span>
<span class="n">MUSTACHE_OK</span><span class="p">,</span>
<span class="n">MUSTACHE_ERR_TOO_DEEP</span><span class="p">,</span>
<span class="n">MUSTACHE_ERR_CLOSURE_MISMATCH</span><span class="p">,</span>
<span class="n">MUSTACHE_ERR_FILE_NOT_FOUND</span><span class="p">,</span>
<span class="n">MUSTACHE_ERR_FILE_TOO_BIG</span><span class="p">,</span>
<span class="n">MUSTACHE_ERR_FILE_NAME_TOO_LONG</span><span class="p">,</span>
<span class="n">MUSTACHE_ERR_EMPTY_TEMPLATE</span><span class="p">,</span>
<span class="n">MUSTACHE_ERR_UNKNOWN</span><span class="p">,</span>
<span class="n">MUSTACHE_ERR_USER_ERROR</span><span class="p">,</span>
<span class="p">}</span> <span class="n">mustache_error_en</span><span class="p">;</span>
</code></pre>
<h4 id="fiobj_mustache_free"><code>fiobj_mustache_free</code></h4>
<pre><code class='highlight'><span class="kt">void</span> <span class="n">fiobj_mustache_free</span><span class="p">(</span><span class="n">mustache_s</span> <span class="o">*</span><span class="n">mustache</span><span class="p">);</span>
</code></pre>
<p>Frees the mustache template pointer immediately (careful when using the template concurrently).</p>
<h3 id="mustache-template-rendering-api">Mustache Template Rendering API</h3>
<h4 id="fiobj_mustache_build"><code>fiobj_mustache_build</code></h4>
<pre><code class='highlight'><span class="n">FIOBJ</span> <span class="n">fiobj_mustache_build</span><span class="p">(</span><span class="n">mustache_s</span> <span class="o">*</span><span class="n">mustache</span><span class="p">,</span> <span class="n">FIOBJ</span> <span class="n">data</span><span class="p">);</span>
</code></pre>
<p>Creates a FIOBJ String containing the rendered template using the information in the FIOBJ <code>data</code> object (which should be a <code>FIOBJ_T_HASH</code> or data access might fail).</p>
<p>Returns FIOBJ_INVALID if an error occurred and a FIOBJ String on success.</p>
<p>Remember to call <code>fiobj_free</code> to free the String (or call <code>fiobj_send_free</code>).</p>
<p><strong>Note</strong>: The <code>mustache_s *</code> object can be used to render the same template multiple times concurrently.</p>
<h4 id="fiobj_mustache_build2"><code>fiobj_mustache_build2</code></h4>
<pre><code class='highlight'><span class="n">FIOBJ</span> <span class="n">fiobj_mustache_build2</span><span class="p">(</span><span class="n">FIOBJ</span> <span class="n">dest</span><span class="p">,</span> <span class="n">mustache_s</span> <span class="o">*</span><span class="n">mustache</span><span class="p">,</span> <span class="n">FIOBJ</span> <span class="n">data</span><span class="p">);</span>
</code></pre>
<p>Renders a template into an existing FIOBJ String (<code>dest</code>&#39;s end), using the information in the <code>data</code> object.</p>
<p>Returns FIOBJ_INVALID if an error occurred and a FIOBJ String on success.</p>
</div><a href="/" id="sign"></a><div class="hidden" id="notice"><a class="notice_close" onclick="hide_notice()">X</a><div id="notice_text"></div></div><script>function change_themes() {
if(localStorage.getItem("theme") == 'dark') {
localStorage.setItem("theme", "light");
} else {
localStorage.setItem("theme", "dark");
}
$('body')[0].className = localStorage.getItem("theme");
set_theme_link();
$('#show_nav').attr('checked', false);
return false;
};
function sidebar_name() { return window.location.pathname.slice(0, window.location.pathname.lastIndexOf("/")); }
function on_sidebar_link(e) {
sessionStorage.setItem("sidebar.expect", sidebar_name());
sessionStorage.setItem("sidebar.pos", document.getElementById("side_bar").scrollTop);
}
function load_sidebar_pos() {
var e = document.getElementById("side_bar");
if(!e) {
console.warn("No sidebar detected");
return;
}
var expect = sidebar_name();
if(sessionStorage.getItem("sidebar.expect") == expect) {
e.scrollTo(0, parseInt(sessionStorage.getItem("sidebar.pos")));
} else {
sessionStorage.setItem("sidebar.expect", false);
sessionStorage.setItem("sidebar.pos", 0);
}
if(e) {
// add link callbacks
var links = document.links;
var count = links.length;
for (var i = 0; i < count; i++) {
var href = links[i].href;
if(href.startsWith(document.location.origin)) {
href = href.slice(document.location.origin.length);
}
if(href.startsWith(expect)) {
/* add link event */
links[i].addEventListener("click", on_sidebar_link);
}
}
}
};
load_sidebar_pos();
function set_theme_link() {
$("#theme").html( ( (localStorage.getItem("theme") == 'dark') ? "Day" : "Night") + " Theme" );
}
$('body')[0].className = (localStorage.getItem("theme") == 'dark') ? "dark" : "light";
set_theme_link();
function show_notice() { document.getElementById("notice").classList.remove('hidden'); };
function hide_notice() { document.getElementById("notice").classList.add('hidden'); };
$('#toc').on("touchstart", function (e) { return true; } );
$('#toc').on("hover", function (e) { return true; } );
// hljs.initHighlighting();
// Google Analytics
// if(location.hostname != "localhost") {
// (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
// (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
// m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
// })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
// ga('create', 'UA-69931401-1', 'auto');
// ga('send', 'pageview');
// }</script></body></html>