Git Product home page Git Product logo

craftcms-shortcodes's Introduction

Shortcodes for Craft CMS

This is a port of Wordpress shortcode functions for Craft CMS.

This plugin allows you to register shortcode callbacks in your own custom plugins. Better code docs will come, but it's very simple to understand by glancing at the code. Suggestions and pull requests are very welcome.

Usage

This repo comes with two plugins: The Shortcodes plugin as well as the Myshortcodes plugin as an example of how to use.

In your own plugin simply use the registerShortcodes() hook to register a single tag, or a list of tags.

Register a single tag

class MyPlugin {

  ...

  public function registerShortcodes()
  {
    return array($this, 'foobar');
  }
  
  public function foobar($attributes, $content, $tag)
  {
    return 'Foo Bar!';
  }

}

Register multiple tags

  public function registerShortcodes()
  {
    return array(
      array($this, 'foobar'),
      array($this, 'doubleRainbow'),
    );
  }
  
  public function foobar($attributes, $content, $tag) { ...
  public function doubleRainbow($attributes, $content, $tag) { ...

}

Callback arguments

Just like WordPress callbacks you get:

  1. A key/value pair array of tag attributes,
  2. The content between the tags.
  3. The tag name, which will be the name of the method called.

If your text input is

[foobar one="two" hey="who"]This is the wrapped text[/foobar]

then

  public function foobar($attributes, $content, $tag)
  {
    $one = $attributes['one'];  // 'two'
    $hey = $attributes['hey'];  // 'who'
    $content;                   // 'This is the wrapped text'
    $tag;                       // 'foobar'
    
    return 'Replacement text.';
  }

Template tag

Add the shortcodes filter to the twig template tag.

{{ entry.body | shortcodes }}

craftcms-shortcodes's People

Contributors

samhernandez avatar brianjhanson avatar

Watchers

James Cloos avatar Webremote avatar

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.