Coder Social home page Coder Social logo

youandmedigital / craft-getasset Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 5 KB

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

License: MIT License

PHP 100.00%
craft3 craft-plugin finder scandir

craft-getasset's Introduction

GetAsset

GetAsset for Craft 3.1

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

Installation

To install the plugin, follow these instructions.

  1. Open your terminal and go to your Craft project:
cd /path/to/project
  1. Then tell Composer to load the plugin:
composer require youandmedigital/craft-getasset

Introduction

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

This might be useful to you if

  • You're running Craft 3.1 or above
  • You're not using something like Asset Rev or Twigpack for Webpack
  • You've setup your local build system to hash your compiled CSS or Javascript
  • You're looking for a low-fi solution to pull in your hashed CSS or Javascript files and make them available in your templates

Examples

Output a file that was last modified in a directory

Inside /assets/css there are 3 files, with the following timestamps:

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

In our Twig templates, we set a variable and give GetAssets a folder path to search:

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

This example Twig code would output:

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

Output a file that was last modified in a directory, which matches a regex pattern

Inside /assets/css there are 4 files, with the following timestamps:

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 our Twig templates, we set a variable, give GetAssets a folder path to search and a regex pattern to match:

{% 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:

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

Configuring GetAsset

  • path (string, required): A valid folder for GetAsset to search
  • pattern (string, optional, default value '*'): A regex pattern to match

Example configuration:

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

craft-getasset's People

Contributors

jonleverrier avatar

Watchers

 avatar  avatar

craft-getasset's Issues

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.