Giter Club home page Giter Club logo

craft-utils's Introduction

Craft Utilities

An evolving toolkit for Craft CMS. Rather than building basic feature shims, shortcuts, and customizations into each project, we've decided to extract them into a discrete bundle and make them available publicly.

Documentation will be broken down into each plugin component, as they're implemented.

Resources

One stylesheet exists, currently with a single rule. We disable the site's name in the Control Panel when an icon is added.

Twig Extensions

Filters

A few proxies for buried or inaccessible Craft methods, and useful PHP methods.

mdline

A shortcut for Craft's built-in single-line Markdown parser.

unique

Quick access to array_unique for filtering out duplicates in an array.

truncate($limit = 160)

Crude text truncation, based on the provided character limit . If a sentence is cut off, an ellipses is added. Words are never broken.

classNames

A clean way of adding a list of classes to an HTML element, from Twig.

toList($prop = null, $separator = ', ', $separatorLast = null)

Creates a delimited list from the provided array. Accepts a nested property to pull from the constituent objects or arrays.

pluck($prop)

Pluck a property from an array of arrays or objects. Subject to failure if you have a mixed dataset, but it will gracefully handle instances of ElementCriteriaModel. Useful for situations where you want grab the same value from a number of objects and output them in a terse way:

<button data-related-ids="{{ entry.relationField | pluck('id') | json_encode }}">Activate Many Items</button>

unescape

Unescapes text with encoded HTML entities. Useful in combination with Twig's built-in raw filter to output HTML from a trusted third party's API that is double-escaped in a JSON response.

intoGroups($n)

Takes an array and splits it into $n sub-arrays. For example, [1, 2, 3, 4, 5, 6] split intoGroups(2) would produce an object like [[1, 2, 3], [4, 5, 6]]. This is useful for balancing items into multiple columns.

Tests

sequentialArray

Unfortunately, PHP makes no fundamental distinction between associative and sequential arrays. This attempts to determine if an array is associative or not, by checking whether a new array of only the original's values is identical…

{% if ['A', 'B', 'C'] is sequentialArray %}
  Yep!
{% endif %}

{% if { one: 'Do', two: 'Re', three: 'Mi' } is sequentialArray %}
  Nope!
{% endif %}

Variables

A few bits are exposed to Twig templates, via craft.utils.methodName. Check out variables/UtilsVariable.php for the full list of methods.

🌳

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.