LogoLogo
Official SiteAPI Reference
stable/3.0
stable/3.0
  • Cement Developer Guide
  • Release Information
    • What's New!
    • Upgrading
    • ChangeLog
    • Deprecations
  • Getting Started
    • Installation
    • Developer Tools
    • Framework Overview
    • Beginner Tutorial
      • Part 1: Creating Your First Project
      • Part 2: Adding Features
      • Part 3: Extending a Project
      • Part 4: Making Everything Legit
  • Core Foundation
    • Interfaces and Handlers
    • Hooks
    • Configuration Settings
    • Arguments
    • Logging
    • Controllers
    • Output Rendering
    • Caching
    • Mail Messaging
    • Framework Extensions
    • Application Plugins
    • Templating
  • Utilities
    • Filesystem
    • Shell
    • Miscellaneous
  • Extensions
    • Alarm
    • Argparse
    • Colorlog
    • ConfigParser
    • Daemon
    • Dummy
    • Generate
    • Jinja2
    • Json
    • Logging
    • Memcached
    • Mustache
    • Plugin
    • Print
    • Redis
    • Scrub
    • SMTP
    • Tabulate
    • Yaml
    • Watchdog
  • Additional Topics
    • Extending The App Object
    • Unit Testing
    • Cleanup
    • Signal Handling
    • Pipenv
    • Autocomplete
    • Profiling with cProfile
    • Debugging with VSCode
  • Environment Variables
  • Terminology
  • Contributing
  • Privacy Policy
Powered by GitBook
On this page
  • Introduction
  • Requirements
  • Configuration
  • Application Configuration Settings
  • Application Meta Options
  • Usage
  • Output Handler
  • Template Handler
  • Loading Partials
  1. Extensions

Mustache

PreviousMemcachedNextPlugin

Last updated 2 years ago

Introduction

The Mustache Extension provides output and file templating based on the .

Documentation References:

API References

Requirements

  • Pystache

Cement 3.0.8+:

pip install cement[mustache]

Applications using Cement <3.0.8 should continue to include pystache in their dependencies.

Configuration

Application Configuration Settings

This extension honors the following settings under the primary namespace (ex: [myapp]) of the application configuration:

Setting

Description

template_dir

Directory path of a local template directory.

Application Meta Options

Option

Description

template_handler

A template handler to use as the backend for templating

template_dirs

A list of data directories to look for templates

template_module

A python module to look for templates

Usage

Output Handler

from cement import App

class MyApp(App):
    class Meta:
        label = 'myapp'
        extensions = ['mustache']
        output_handler = 'mustache'

with MyApp() as app:
    app.run()

    # create some data
    data = {
        'foo': 'bar',
    }

    # render the data to STDOUT (default) via a template
    app.render(data, 'my_template.mustache')

Template Handler

from cement import App

class MyApp(App):
    class Meta:
        label = 'myapp'
        extensions = ['mustache']
        template_handler = 'mustache'

with MyApp() as app:
    app.run()

    # create some data
    data = {
        'foo': 'bar'
    }

    # copy a source template directory to destination
    app.template.copy('/path/to/source/', 
                      '/path/to/destination/', 
                      data)

    # render any content as a template
    app.template.render('foo -> {{ foo }}', data)

Loading Partials

Mustache supports partials, or in other words template includes. These are also loaded by the output handler, but require a full file name. The partials will be loaded in the same way as the base templates.

templates/base.mustache
Inside base.mustache
{{> partial.mustache}}
templates/partial.mustache
Inside partial.mustache

The above would output:

Inside base.mustache
Inside partial.mustache

This extension honors the following options:

Mustache Templating Language
Output Rendering
Templating
Cement Mustache Extension
Mustache Library
App.Meta