Meld eases the process of adding a prefix or a suffix to an array of strings within your Twig templates.

That's pretty useful, but Meld goes further. It allows you to combine an array of prefixes with an array of suffixes.

Let's take a look at a quick example:

{% set prefixes = ['Hello', ['Hola', 'Adios']] %}
{% set suffixes = ['World', ['Tierra', 'Amigo']] %}
{% set melded = prefixes|meld(suffixes) %}
    
<ul>
    {% for meld in melded %}
        <li>{{ meld }}</li>
    {% endfor %}
</ul>
    
// Result:
<ul>
    <li>HelloTierra</li>
    <li>HelloAmigo</li>
    <li>HolaWorld</li>
    <li>AdiosWorld</li>
    <li>HolaTierra</li>
    <li>HolaAmigo</li>
    <li>AdiosTierra</li>
    <li>AdiosAmigo</li>
</ul>

How it works

You can throw pretty much anything at Meld, and it will do its best to make sense of the madness.

If either the prefix or the suffix is an array, Meld returns an array of results. Otherwise, Meld returns a single string.

Meld will do its best to coerce each prefix and suffix into a string (assuming it isn't already). In practical terms, this means:

  • Integers are converted to their string equivalent;
  • Floats are converted to their string equivalent;
  • Booleans are converted to "1" (true) or "0" (false);
  • Objects which implement the __toString method are converted to their string representation;
  • Everything else is converted to an empty string.

It's worth noting that even if the prefix or suffix is an empty string, Meld will still include it in the results. If you don't want this to happen, sanitise your prefixes and suffixes before passing them to Meld. Rubbish in, rubbish out.


Free

Package Name

To install this plugin, search for its package name on the Plugin Store and click “Install”.


Information

  • Version 2.0.1
  • Last Update 8/31/2018
  • Active Installs 4
  • Compatibility Craft 3
  • License MIT
  • Categories Templating

Report an issue