Skip to content

davidmaneuver/cockpit-twig

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 

Repository files navigation

Cockpit-Twig

Use Cockpit and Twig together.

Installation

  1. Download zip and unpack to 'your_cockpit_folder/modules/addons/twig'.
  2. Make sure you require 'your_cockpit_folder/bootstrap.php'.
  3. Hook up your Twig_Environment to the module.
cockpit('twig')->use($your_twig_env);
  1. Start using Cockpit in your templates.

Initialisation Example

define('DOCROOT', $_SERVER['DOCUMENT_ROOT']);
require_once('vendor/autoload.php'); // If you're using composer.
require_once('cockpit/bootstrap.php');

// Setup Twig templating engine
$loader = new Twig_Loader_Filesystem(DOCROOT . '/templates');
$twig = new Twig_Environment($loader, array(
  'cache' => DOCROOT . '/cache',
));

cockpit('twig')->use($twig);

$template = $twig->loadTemplate('index.twig'); // Make sure '/templates/index.twig' exists.
echo $template->render();

Usage

Collections

Retrieve all items for a collection:

{% for item in collection('projects') %}
  {{ item.name }}
{% endfor %}

Query a collection:

{% for item in collection('projects', {active: 1}) %}
  {{ item.name }}
{% endfor %}

Regions

Render the entire region, with template:

{{ region('info') }}

Render one field:

{{ region_field('info', 'title') }}

Parameters are: region name & field name.

Galleries & Mediamanager

Print an image tag for each image in gallery:

{% for image in gallery('projects') %}
  {{ thumbnail(image.path) }}
{% endfor %}

or just the url:

{% for image in gallery('projects') %}
  <img src="{{ thumbnail_url(image.path) }}" />
{% endfor %}

Forms

{{ form('contact') }}
  <input type="text" name="form[name]" />
  <input type="email" name="form[email]" />
  <input type="submit" value="submit" />
</form>

Authentication Methods

get_user() returns a User object representing the currently logged in user if any. has_access($resource, $action) returns true or false depending on the granted permissions for that action and resource and for the currently logged in user.

{% set user = get_user() %}
{% if user %}
  Nice to see you again {{ user.name }}!
  {% if has_access('gallery', 'create') %}
    Let's snap some pictures.
  {% endif %}
{% endif %}

Copyright and license

Copyright 2014 maneuver under the MIT license.

About

Use Cockpit (http://getcockpit.com) in your Twig projects.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages