File: simple.c

package info (click to toggle)
template-glib 3.38.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 788 kB
  • sloc: ansic: 6,774; yacc: 288; lex: 237; javascript: 16; makefile: 14; python: 12
file content (56 lines) | stat: -rw-r--r-- 1,318 bytes parent folder | download | duplicates (5)
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
50
51
52
53
54
55
56
#include <tmpl-glib.h>
#include <stdlib.h>

gint
main (gint   argc,
      gchar *argv[])
{
  g_autoptr(GFile) file = NULL;
  g_autoptr(TmplScope) scope = NULL;
  g_autoptr(TmplTemplate) tmpl = NULL;
  g_autoptr(GError) error = NULL;
  TmplSymbol *symbol = NULL;
  gchar *str;

  /*
   * First we need to create and parse our template.
   * The template can be expanded multiple times, so you can
   * keep them around and re-use them.
   */
  file = g_file_new_for_path ("simple.tmpl");
  tmpl = tmpl_template_new (NULL);

  if (!tmpl_template_parse_file (tmpl, file, NULL, &error))
    {
      g_printerr ("%s\n", error->message);
      return EXIT_FAILURE;
    }

  /*
   * Now create our scope used to expand the template,
   * and assign the "title" variable in the scope.
   */
  scope = tmpl_scope_new ();

  symbol = tmpl_scope_get (scope, "title");
  tmpl_symbol_assign_string (symbol, "My Title");

  /*
   * Expand the template based on our scope. You can also expand into a
   * GOutputStream, instead of a string.
   */
  if (!(str = tmpl_template_expand_string (tmpl, scope, &error)))
    {
      g_printerr ("%s\n", error->message);
      return EXIT_FAILURE;
    }

  g_print ("%s\n", str);
  g_free (str);

  /*
   * All our state gets cleaned up thanks to g_autoptr()!
   */

  return EXIT_SUCCESS;
}