GithubHelp home page GithubHelp logo

bryant1410 / craft_hacksaw Goto Github PK

View Code? Open in Web Editor NEW

This project forked from ehousestudio/craft_hacksaw

0.0 3.0 0.0 24 KB

A migrated version of Hacksaw (for ExpressionEngine) made especially for Craft CMS.

PHP 100.00%

craft_hacksaw's Introduction

Hacksaw 1.1.2

A simple text truncation plugin for Craft CMS. This plugin adds a Twig filter to take your entry's content and hack it down to a more manageable size. It strips the HTML and limits the excerpts by character count, word count, or cutoff marker.

Installation

  1. Move hacksaw directory to craft/plugins directory
  2. Install Hacksaw under Craft Admin › Settings › Plugins

Parameters

There are several parameters you can use to control how the content is truncated. These options are:

Parameter Type Default Description
chars int null Limit by number of characters (Note: includes chars of HTML)
chars_start int 0 Starting point for chars limit (used with chars param)
words int null Limit by number of words
cutoff string null Limit by a specific cutoff string
append string null String to append to the end of the excerpt
allow string null HTML tags you want to allow

Usage

For example, if you want to limit your excerpt to 100 words you would do this:

{{ entry.richTextField|hacksaw(words='100') }}

By default Hacksaw will strip all HTML from your excerpt. If you would like to keep some basic HTML you can use the allow parameter to keep specific HTML tags. For example, let's say you want to keep <p> and <b> tags:

{{ entry.richTextField|hacksaw(words='100', allow='<p><b>') }}

Another way to limit content is by a cutoff string. This is similar to the way WordPress's more feature works. In your content you could add a specific string to indicate the spot you want the excerpt to stop. Let's say you use, <!-- END -->; to cut the excerpt off at this location you would do this:

{{ entry.richTextField|hacksaw(cutoff='<!-- END -->') }}

The cutoff parameter can be coupled with the words parameter in case you forget to add the cutoff string to some of your entries. This way the excerpt will be truncated at the cutoff string, OR after X words.

You can add any string at the end of the excerpt using the append parameter, like this:

{{ entry.richTextField|hacksaw(words='100', append='...') }}

This would append "..." to the end of the excerpt.

Note: If you are including HTML in the append parameter, the elements must be present in the allow paramenter. If you are including a Craft variable in any parameter, it must be added using the Twig concatenation operator, ~. Example of both:

{{ entry.richTextField|hacksaw(words='100', allow='<a>', append='<a href="' ~ entry.url ~ '">Continue...</a>') }}

Roadmap

  • Add truncation by number of paragraphs

Feedback?

Contact us on Twitter: @ehousestudio

craft_hacksaw's People

Contributors

ryanshrum avatar jackmcpickle avatar jasonmccallister avatar joshangell avatar bryant1410 avatar khalwat avatar

Watchers

James Cloos avatar  avatar  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.