2017 © Pedro Peláez
 

library embedded-svg

Latte macro to embed SVG from file into HTML

image

milo/embedded-svg

Latte macro to embed SVG from file into HTML

  • Thursday, May 31, 2018
  • by milonemilo
  • Repository
  • 1 Watchers
  • 5 Stars
  • 80 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 2 Versions
  • 1500 % Grown

The README.md

Example

<h1>
    Publications
    {embeddedSvg 'icons/help.svg',
        class => 'help-icon',
        data-toggle => 'popover',
        data-content => 'This is a list of your publications for last 5 years.'
    }
</h1>

Result HTML code may look like:, (*1)

<h1>
    Publications
    <svg xmlns="..." class="help-icon" ...>
        ... content of help.svg file ...
    </svg>
</h1>

Purpose

This is a single purpose helper library with a macro definition for Latte, the PHP templating engine. It loads SVG source file and embed it into HTML code in compile time., (*2)

Motivation for this is possibility to stylize SVG by CSS then. It is not (easily) possible with SVG linked as an image like <img src="icons/help.svg">., (*3)

Installation

Require library by Composer:, (*4)

composer require milo/embedded-svg

Register extension in your config.neon and configure it:, (*5)

extensions:
    embeddedSvg: Milo\EmbeddedSvg\Extension

embeddedSvg:
    baseDir: %wwwDir%/img

Configuration

There are some other optional options:, (*6)

embeddedSvg:
    # change name of the macro
    macroName: svgIcon

    # pretty format SVG content (indent tags)
    prettyOutput: yes

    # default <svg> tag attributes, for example
    defaultAttributes:
        class: embedded
        height: 30px
        width: null

    # callbacks called when SVG loaded from file
    onLoad:
        - [SVGSanitizer, sanitize]

    # bitmask of LIBXML_* flags for DOMDocument::load() method
    libXmlOptions: (integer)

You can load the extension more then once. In this case, change macro name by macroName option., (*7)

Option defaultAttributes is a XML attributes list for generated <svg> tag. These are merged. The precedence is (higher to lower): - macro tag attributes - default attributes - attributes of <svg> tag loaded from file, (*8)

If the attribute value is null, it is not rendered. You can unset attributes from SVG file in that way., (*9)

Callback added into onLoad event is called when SVG contents is successfully loaded into DOM. Its signature is:, (*10)

function (DOMDocument $dom, Milo\EmbeddedSvg\MacroSetting $setting) {
    ...
}

Caveats & Limitations

Because embeddedSvg is a macro, it is compiled into PHP only once and then is cached. So, when you change the macro configuration, probably in NEON, you have to purge Latte cache., (*11)

The Versions

31/05 2018

dev-master

9999999-dev

Latte macro to embed SVG from file into HTML

  Sources   Download

MIT

The Requires

 

nette latte macro svg embedded

11/05 2018

v1.0.0

1.0.0.0

Latte macro to embed SVG from file into HTML

  Sources   Download

MIT

The Requires

 

nette latte macro svg embedded