# Introducing GetAsset for Craft CMS

Source: https://jonleverrier.com/notes/introducing-getasset-for-craft-cms
Published: 2020-05-18
Topics: Development

This utility plugin looks inside a folder for a file that was last modified and returns the value in Twig.

## Introduction

GetAsset was first launched in February 2019. It looks inside a folder for a file that was last modified and returns the value in Twig.

### Requirements

- Craft 3.1 or above

## Examples

### Return the last modified file from a directory

Folder path /assets/css contains 3 files, with the following timestamps:

```shell
22 Apr 22:54 c.min.d6df804850.css
22 Apr 22:55 plugin.css
22 Apr 22:54 ie.min.8af08fe73b.css
```

In Twig we pass an array of settings into GetAsset:

```twig
{% set settings =
    {
        path: '/assets/css/'
    }
%}
{% set file = craft.getasset.config(settings) %}
<link rel="stylesheet" href="/assets/css/{{ file }}">
```

This would output:

```html
<link rel="stylesheet" href="/assets/css/plugin.css">
```

### Return the last modified file from a directory that matches a regex pattern

Folder path /assets/css contains 4 files, with the following timestamps:

```shell
22 Apr 22:54 c.min.d6df804850.css
22 Apr 22:50 c.min.a8xd808007.css
22 Apr 22:55 p.min.d6df804850.css
22 Apr 22:54 ie.min.8af08fe73b.css
```

In Twig we pass an array of settings into GetAsset. The settings array contains the path and regex pattern:

```twig
{% set settings =
    {
        path: '/assets/css/',
        pattern: '/^(c).min.*\\S{10}.css$/'
    }
%}
{% set file = craft.getasset.config(settings) %}  
<link rel="stylesheet" href="/assets/css/{{ file }}">
```

This example Twig code would output:

```html
<link rel="stylesheet" href="/assets/css/c.min.d6df804850.css">
```

## Configuration

#### path

```plaintext
string, required
```

A valid folder for GetAsset to search

#### pattern

```plaintext
string, optional, default value '*'
```

A regex pattern to match

```twig
{% set myVarSettings =
    {
        path: '<path>',
        pattern: '<pattern>'
    }
%}
{% set myVar = craft.getasset.config(myVarSettings) %}
```

## Installation

```twig
composer require youandmedigital/craft-getasset
```

If you have any issues, feedback or ideas, please feel free to [submit an issue](https://github.com/youandmedigital/craft-getasset/issues/new) on [Github](https://github.com/youandmedigital/craft-getasset).
