Socket
Socket
Sign inDemoInstall

@netzstrategen/twig-asset

Package Overview
Dependencies
Maintainers
7
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@netzstrategen/twig-asset

asset() function for Twig.


Version published
Maintainers
7
Created
Source

Twig asset()

A simple asset function for Twig, which appends the file modification time of the referenced asset file to ensure the browser reloads the resource when it changes. (cache invalidation)

This repository holds the extension for PHP and JS.

Installation

PHP

  1. Add the package to your project.

    $ composer require netzstrategen/twig-asset
    
  2. Add the extension and configuration to your Twig environment.

    <?php
    
    use Netzstrategen\TwigAsset\TwigExtension as TwigAsset;
    
    $twig->addGlobal('asset_path_document_root', '<path/to/templates>');
    $twig->addExtension(new TwigAsset());
    

JS

  1. Add the package to your project.

    $ npm install @netzstrategen/twig-asset --save
    
  2. Load the extension with your configuration in your project.

    const TwigAsset = require('@netzstrategen/twig-asset')({
      asset_path_document_root: __dirname,
    });
    
    
  3. Register the function in your Twig environment:

    for (const name in TwigAsset) {
      twig.extendFunction(name, TwigAsset[name]);
    } 
    

Third-party integrations

Fractal
const twigAdapter = require('@netzstrategen/twig-drupal-fractal-adapter');
const instance = fractal.components.engine(twigAdapter);
instance.twig.extendFunction('asset', TwigAsset.asset);

Configuration

  • asset_path_document_root (string, required): Path to the document root.

    Acts as a base path. The path passed to the asset() function will be appended to this base path in order to retrieve the file's modification time.

Usage

<link rel="stylesheet" href="{{ asset('/path/from/root.css') }}">

yields:

<link rel="stylesheet" href="/path/from/root.css?v=1565339299">

Parameters

  • add_version (bool, optional): Whether to append the file modification time. Defaults to true.

    Example:

    <link rel="stylesheet" href="{{ asset('/path/from/root.css', false) }}">
    

    yields:

    <link rel="stylesheet" href="/path/from/root.css">
    

Alternatives to this package

See Symfony for a more sophisticated solution (PHP only):

FAQs

Package last updated on 03 Jul 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc