npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@webrgp/laravel-mix-twig

v1.0.5

Published

A Laravel Mix extension to convert twig files to html

Downloads

6

Readme

laravel-mix-twig

npm npm GitHub issues GitHub license

Laravel Mix extension to compile Twig templates to HTML

Usage

Install the extension:

npm install @webrgp/laravel-mix-twig --save-dev

Then require and configure the extension within your webpack.mix.js.

Simple configuration

const mix = require('laravel-mix')
require('@webrgp/laravel-mix-twig')

// mix.twig('your source folder', 'your dist or public folder', {your advance configuration})
mix.twig('src/templates', 'public')

Underscore to ignore

Files or folders prefixed with an underscore are ignored from html output. This is a handy feature for ignoring component and layout files.

Ignored files:
/_components/header.twig
/_layouts/base.twig
/_include.twig

Options

| Name | Type | Default | Description | | ----------- | -------- | ------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------- | | files * | string | | Paths to your twig source files (supports [minimatch](https://github.com/isaacs/minimatch#usage))<br>OR<br> An array of objects to pass to html-webpack-plugin | | fileBase \* | `string` | | The base path to your template folder | | options | object | {} | Customizations options |

* = Required

These are the folowing options you can pass:

Data

Set global variables like this:

mix.twig('src/templates', 'public', {
  data: {
    msg: 'Hello World'
  }
})

That will be available to all templates:

<h1>{{ msg }}<h1/>

Debug & Trace

Enables debug or trace info logging (defaults to false)

mix.twig('src/templates', 'public', {
  debug: true,
  trace: true
})

Functions

Custom functions can be added through { functions: [ [String, Callback] ] }

For example, a function that repeats a string could look like:

mix.twig('src/templates', 'public', {
  functions: [
    [
      'repeat',
      function (value, times) {
        return new Array(times + 1).join(value)
      }
    ]
  ]
})

And you can use it in a template like:

{{ repeat("_.", 10) }}
{# output: _._._._._._._._._._. #}

Filters

Custom filters can be added through { filters: [ [String, Callback] ] }

For example, if you wanted to add a filter that reversed words in a sentence, you could do the following:

mix.twig('src/templates', 'public', {
  filters: [
    [
      'backwords',
      function (stringValue) {
        return stringValue.split(' ').reverse().join(' ')
      }
    ]
  ]
})

Then, in your templates you can use:

{{ "a quick brown fox"|backwords }}
{# outputs: fox brown quick a #}

Custom filters with arguments are also supported:

mix.twig('src/templates', 'public', {
  filters: [
    [
      'catify',
      function (value, args) {
        return args.reduce(
          (newString, toCatify) => newString.replace(toCatify, 'cat'),
          value
        )
      }
    ]
  ]
})
{{ "the quick brown fox jumps over the lazy dog"|catify('fox', 'dog') }}
{# outputs: the quick brown cat jumps over the lazy cat #}

Extends Twig with new tags types

Custom tags can be added through { extend: [ function(Twig) ] } where the Twig argument is Twig.js's internal object. Read more here.

Formatting

You can format the rendered output by passing the format option with either minify, pretty or unstyled (default).

mix.twig('src/templates', 'public', {
  format: 'minify'
})

Built in helpers

For pratical purposes, I've included the following custom functions and filters that can be overwritten using the functions and filters options.

| Name | Description | | ----- | --------------------------------------------------------------------------------------- | | mix | The {{ mix(string) }} function returns the related entry in mix-manifest.json file. |