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 to the Template Interface
  • Configuration
  • Application Meta Options
  • Working with Templates
  • Creating a Template Handler
  1. Core Foundation

Templating

PreviousApplication PluginsNextUtilities

Last updated 6 years ago

Introduction to the Template Interface

Cement defines a , as well as a default that implements the interface as a placeholder but does not actually do anything.

Cement often includes multiple handler implementations of an interface that may or may not have additional features or functionality than the interface requires. The documentation below only references usage based on the interface and default handler (not the full capabilities of an implementation).

****

Cement Extensions That Provide Template Handlers:

  • ​

API References:

Configuration

Application Meta Options

The following options under modify configuration handling:

Option

Description

template_handler

The handler that implements the template interface.

Working with Templates

The template handler can be used to render content in-line, as well as copy render source directories before copying them to their destination.

from cement import App

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

with MyApp() as app:
    # create a data dictionary for templating
    data = {
        'foo': 'bar'
    }

    # render content as template
    app.template.render('Foo => {{ foo }}', data)

    # render and copy a source directory
    src = '/path/to/source/dir'
    dst = '/path/to/destination/dir'
    app.template.copy(src, dst, data)

When copying a source directory, both the file/directory path names themselves are rendered as templates as well as the contents of files.

Creating a Template Handler

myapp.py
from cement import App
from cement.core.template import TemplateHandler

class MyTemplateHandler(TemplateHandler):
    class Meta:
        label = 'my_template_handler'

    # do something to implement the interface

class MyApp(App):
    class Meta:
        label = 'myapp'
        template_handler = 'my_template_handler'
        handlers = [
            MyTemplateHandler,
        ]

All interfaces in Cement can be overridden with your own implementation. This can be done either by sub-classing itself, or by sub-classing an existing extension's handlers in order to alter their functionality.

Template Interface
DummyTemplateHandler
Jinja2
Mustache
Dummy
​Cement Core Template Module​
App.Meta
TemplateHandler