Skip to content

Instantly share code, notes, and snippets.

@brandonkelly
brandonkelly / gist:8672116
Created January 28, 2014 17:26
A function for converting an entry in Craft to JSON
<?php
public function getEntryJson(EntryModel $entry)
{
$entryData = array();
foreach ($entry->getType()->getFieldLayout()->getFields() as $field)
{
$field = $field->getField();
$handle = $field->handle;
@brandonkelly
brandonkelly / templating.md
Last active February 7, 2024 15:20
Templating in EE vs. Craft
@brandonkelly
brandonkelly / gist:8149062
Last active July 30, 2018 08:26
Saving new Matrix data
<?php
// Get the entry
$entry = craft()->entries->getEntryById(100);
// Convert the existing data to what it would look like in POST
$matrixData = array();
foreach ($entry->matrixField as $block)
{
@brandonkelly
brandonkelly / relatedTo.twig
Last active December 27, 2015 00:49
New 'relatedTo' element param coming in Craft 1.3
<h3>My favorite cocktails that have gin</h3>
{% set cocktails = craft.entries.section('cocktails').relatedTo('and', {
sourceElement: currentUser,
field: 'favoriteCocktails'
}, {
targetElement: craft.entries.section('ingredients').slug('gin'),
field: 'ingredients'
}) %}
return array(
'*' => array(
'devMode' => false,
'environmentVariables' => array(
'siteUrl' => 'http://economicexperts.eu/'
),
),
'.dev' => array(
@brandonkelly
brandonkelly / gist:6532664
Created September 12, 2013 03:07
How to protect some text from getting auto-escaped by Twig, without using the raw filter
$charset = craft()->templates->getTwig()->getCharset();
$value = new \Twig_Markup($value, $charset);
@brandonkelly
brandonkelly / gist:6234999
Last active December 21, 2015 02:28
Turn a list of entries into JSON in Craft
{
entries: [
{% for entry in craft.entries.find() %}
{
title: "{{ entry.title | e('js') }}",
tags: [
{% for tag in entry.myTagsField %}
"{{ tag.name | e('js') }}" {% if not loop.last %},{% endif %}
## Getting Started
* Introduction
* Installing & Updating
- Requirements
- Installing
- Updating
* Licensing
* CP Overview
* Packages & Plugins
@brandonkelly
brandonkelly / gist:5722967
Created June 6, 2013 16:42
New getNext() / getPrev() support coming to all elements in Craft 1.1
{% set criteria = {section: 'cocktails', order: 'title'} %}
{% set prev = entry.getPrev(criteria) %}
{% set next = entry.getNext(criteria) %}
{% if prev or next %}
<div class="pane">
{% if prev %}
<p>Previous: <a href="{{ prev.url }}">{{ prev.title }}</a></p>
{% endif %}
@brandonkelly
brandonkelly / gist:5643159
Created May 24, 2013 12:27
Upcoming BaseOptionsFieldType templating options
{% if entry.drinkAttributes.contains('alcoholic') %}
Alcoholic!
{% else %}
Not alcoholic
{% endif %}
<h2>Selected Attributes</h2>
<table>
{% for attr in entry.drinkAttributes %}
<tr>