<p>Hope this helps:</p>
<p>I needed a hash that only changed if the resource file is modified thus allowing reliable caching.</p>
<p>Usage:
&lt;img src=&quot;{{ hash('/images/my_image.jpg') }}&quot;</p>
<p>Can be used for javascript, css or an image resource.</p>
<pre>
// src/Twig/appExtension.php
namespace App\Twig;

use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;

class AppExtension extends AbstractExtension
{
    public function getFunctions()
    {
        return [
            new TwigFunction('hash', [$this, 'hash'])
        ];
    }

    // add hash to resource
    public function hash($filename)
    {
        $filepath = getcwd() . $filename;
        $md5 = md5_file($filepath);
        return $filename . "?hash=" . $md5;
    }
}
</pre>

CC BY-SA 4.0
2021-04-20T08:15:38.447Z
2021-04-20T08:24:08.977Z
2021-04-20T08:24:08.977Z
Frankos
<p>sounds like a simple <a href="https://buildwithcraft.com/docs/plugins/introduction" rel="nofollow">plugin</a> will do the trick for you. You will need to set up your <a href="http://buildwithcraft.com/docs/plugins/setting-things-up" rel="nofollow">main plugin class</a> and a <a href="http://buildwithcraft.com/docs/plugins/variables" rel="nofollow">variable class</a>. </p>

<p>Your variable class will look something like this:</p>

<pre><code>&lt;?php
namespace Craft;

class YourPluginNameVariable
{
    public function makeHash($string)
    {
        $hashed = md5($string);
        return $hashed;
    }
}
</code></pre>

<p>You can call this in your templates like this:</p>

<pre><code>{{craft.yourpluginname.makeHash('my phrase here')}}
</code></pre>

<p>Hope this helps!</p>

CC BY-SA 3.0
2015-02-14T19:05:15.430Z
That's what I thought I might need to do. Just didn't want to reinvent the wheel if I didn't need to. Thank you!
John Morton
CC BY-SA 3.0
2015-02-14T19:45:10.740Z
I wrote one quickly and have posted it to my GitHub page: https://github.com/johnfmorton/Craft-MD5-Twig-Filter-Plugin You use it like {{ "my phrase here" | toMd5 }}
John Morton
CC BY-SA 3.0
2015-02-14T19:48:06.137Z
Nice! Seems a cleaner than using the variable route in my answer.
Aaron Berkowitz
CC BY-SA 3.0
2015-02-14T18:59:56.720Z
2015-02-14T18:59:56.720Z
Aaron Berkowitz
<p>I've been digging around and haven't found an existing Twig filter for what I'm trying to do. Basically, I'd like a filter that would provide a hash of a phrase.</p>

<p>Pseudo-template code would be something like this:</p>

<pre><code>{{ "my phrase here"|hashwithmd5 }} 
</code></pre>

<p>And would generate this in my rendered template:</p>

<pre><code>6e6d5618f8ab8a302a236eac07c59300
</code></pre>

<p>Does something like this exist? Am I going about this in a bad way?</p>

<filter><hash>
Craft or Twig filter for MD5 or SHA3 hash creation?
# Craft or Twig filter for MD5 or SHA3 hash creation?

(Asked by: username_0 on 2015-02-14)

I've been digging around and haven't found an existing Twig filter for what I'm trying to do. Basically, I'd like a filter that would provide a hash of a phrase.
Pseudo-template code would be something like this:
{{ "my phrase here"|hashwithmd5 }} 

And would generate this in my rendered template:
6e6d5618f8ab8a302a236eac07c59300

Does something like this exist? Am I going about this in a bad way?

**Tags:** filter, hash


---

## 2 answers

### Answer by username_2 on 2015-02-14. Score: 4 | Accepted answer

sounds like a simple plugin will do the trick for you. You will need to set up your main plugin class and a variable class. 
Your variable class will look something like this:

You can call this in your templates like this:
{{craft.yourpluginname.makeHash('my phrase here')}}

Hope this helps!

> **Comments:**
> **username_0**: That's what I thought I might need to do. Just didn't want to reinvent the wheel if I didn't need to. Thank you!  
> **username_0**: I wrote one quickly and have posted it to my GitHub page: https://github.com/johnfmorton/Craft-MD5-Twig-Filter-Plugin You use it like {{ "my phrase here" | toMd5 }}  
> **username_2**: Nice! Seems a cleaner than using the variable route in my answer.  

---

### Answer by username_1 on 2021-04-20. Score: 1

Hope this helps:
I needed a hash that only changed if the resource file is modified thus allowing reliable caching.
Usage:

Can be used for javascript, css or an image resource.

// src/Twig/appExtension.php
namespace App\Twig;

use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;

class AppExtension extends AbstractExtension
{
    public function getFunctions()
    {
        return [
            new TwigFunction('hash', [$this, 'hash'])
        ];
    }

    // add hash to resource
    public function hash($filename)
    {
        $filepath = getcwd() . $filename;
        $md5 = md5_file($filepath);
        return $filename . "?hash=" . $md5;
    }
}


---

