GithubHelp home page GithubHelp logo

twig-heroicons's Introduction

Twig Heroicons

tests

This package provides an Heroicons integration for Twig.

Install

Use composer:

composer require marcw/twig-heroicons

Symfony

You do not need to do anything more to use the extension.

Twig

If you use Twig directly, register the extension before using it:

<?php

use MarcW\Heroicons\Twig\HeroiconsExtension;
use Twig\Environment;

$twig = new Environment(/* ... */);
$twig->addExtension(new HeroiconsExtension());

Usage

This extension provides a heroicon function that outputs the icon SVG.

{# function signature #}
{{ heroicon(icon, class, style) }}

{# the default style is 'solid' #}
{{ heroicon('academic-cap') }}

{# use the 'outline' style #}
{{ heroicon('academic-cap', '', 'outline') }}

{# Add a custom class to the SVG #}
{{ heroicon('academic-cap', 'text-green-200', 'outline') }}

License

This library is MIT licensed.

twig-heroicons's People

Contributors

aimproxy avatar marcw avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

twig-heroicons's Issues

Make icons cacheable inside opcache

Currently file_get_contents is everytime used to load the icon. This means it need to load and read the icon from the filesystem. This can be an overhead.
I would suggest to create a script which will create a PHP class per icon e.g.:

namespace MarcW\Heroicons\Icons\Solid

use MarcW\Heroicons\IconInterface;

class AcademicCap implements IconInterface {
    public static function getIcon(): string
    {
         return '<svg ... >';
    }
}

Then we can get the icon with:

return call_user_func_array(['MarcW\\Heroicons\\Icons\\' . $solidOrOutline . '\\' . $icon], ['getIcon']);

The advantage of this is that the icon are cacheable inside the opcache of php and don't need to be loaded everytime when being accessed. The svg I would keep in the repository, the classes could be generated over a script inside this repository and so the classes are easy to update if you update the resources directory with new icons. What do you think?

Future deprecation notice

Method "Twig\Extension\ExtensionInterface::getFunctions()" might add "array" as a native return type declaration in the future. Do the same in implementation "MarcW\Heroicons\Twig\HeroiconsExtension" now to avoid errors or add an explicit @return annotation to suppress this message.

Working with Symfony 5

How this exactly is supposed to work? Do I have to register something in Symfony Services to get it working?

An exception has been thrown during the rendering of a template ("Heroicon "academy-cap" in style "outline" cannot be found or is not readable.").
{% extends '@EasyAdmin/page/content.html.twig' %}
{% block page_content %}
    <div class="px-4 py-5 my-5 text-center">
        {{ heroicon('academy-cap', '', 'outline') }}

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.