Skip to content

Commit

Permalink
INITIAL: initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
stevie-mayhew committed Nov 23, 2014
1 parent b9e1ff3 commit 73669b8
Show file tree
Hide file tree
Showing 5 changed files with 245 additions and 0 deletions.
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# SilverStripe SVG

Basic SVG support for SilverStripe templates

## Requirements
SilverStripe 3.1 or higher

## Installation
```composer require stevie-mayhew/silverstripe-svg:1.0.*```

## Configuration

You can set the base path for where your SVG's are stored. You can also add extra default classes to the SVG output

```yml
SVGTemplate:
base_path: 'themes/base/production/svg/'
default_extra_classes:
- 'svg-image'
```
## Usage
```html
<!-- add svg -->
{$SVG('name')}
<!-- add svg with id 'testid' -->
{$SVG('with-id', 'testid')}
```
### Example Output
```html
<svg xmlns="http://www.w3.org/2000/svg" viewBox="248.5 0 464.8 560" enable-background="new 248.5 0 464.8 560" class="svg-name"><path d="M550.9 0H248.5v560h464.8V154.9L550.9 0zM648 149.3H534.1V41.1L648 149.3zm22.4 369.6H289.6V41.1h205.3v149.3h177.3v328.5h-1.8zM343.7 272.5h272.5v41.1H343.7zM343.7 369.6h272.5v41.1H343.7z"></path></svg>
<svg xmlns="http://www.w3.org/2000/svg" viewBox="248.5 0 464.8 560" enable-background="new 248.5 0 464.8 560" class="svg-with-id svg-test-id" id="test-id"><path d="M550.9 0H248.5v560h464.8V154.9L550.9 0zM648 149.3H534.1V41.1L648 149.3zm22.4 369.6H289.6V41.1h205.3v149.3h177.3v328.5h-1.8zM343.7 272.5h272.5v41.1H343.7zM343.7 369.6h272.5v41.1H343.7z"></path></svg>

```
Empty file added _config/.gitignore
Empty file.
167 changes: 167 additions & 0 deletions code/SVGTemplate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
<?php

/**
* Class SVGTemplate
*/
class SVGTemplate extends ViewableData
{
/**
* The base path to your SVG location
*
* @config
* @var string
*/
private static $base_path = 'mysite/svg/';
/**
* @config
* @var string
*/
private static $extension = 'svg';
/**
* @config
* @var array
*/
private static $default_extra_classes = array();
/**
/**
* @var string
*/
private $path;
/**
* @var string
*/
private $fill;
/**
* @var string
*/
private $width;
/**
* @var string
*/
private $height;
/**
* @var array
*/
private $extraClasses;

/**
* @param string $name
* @param string $id
*/
public function __construct($name, $id = '')
{
$this->name = $name;
$this->id = $id;
$this->extra_classes = $this->stat('default_extra_classes');
$this->extra_classes[] = 'svg-'.$this->name;
$this->out = new DOMDocument();
$this->out->formatOutput = true;
}

/**
* @param $color
* @return $this
*/
public function fill($color)
{
$this->fill = $color;
return $this;
}

/**
* @param $width
* @return $this
*/
public function width($width)
{
$this->width = $width;
return $this;
}

/**
* @param $height
* @return $this
*/
public function height($height)
{
$this->height = $height;
return $this;
}

/**
* @param $width
* @param $height
* @return $this
*/
public function size($width, $height)
{
$this->width($width);
$this->height($height);

return $this;
}

/**
* @param $class
* @return $this
*/
public function extraClass($class)
{
$this->extra_classes[] = $class;
return $this;
}


/**
* @param $filePath
* @return string
*/
private function process($filePath)
{
$out = new DOMDocument();
$out->load($filePath);

$root = $out->documentElement;
if ($this->fill) {
$root->setAttribute('fill', $this->fill);
}

if ($this->width) {
$root->setAttribute('width', $this->width . 'px');
}

if ($this->height) {
$root->setAttribute('height', $this->height . 'px');
}

if ($this->extra_classes) {
$root->setAttribute('class', implode(' ', $this->extra_classes));
}

foreach ($out->getElementsByTagName('svg') as $element) {
if ($this->id) {
$element->setAttribute('id', $this->id);
} else {
if ($element->hasAttribute('id')) {
$element->removeAttribute('id');
}
}
}

$out->normalizeDocument();
return $out->saveHTML();
}

/**
* @return string
*/
public function forTemplate()
{
$path = BASE_PATH . DIRECTORY_SEPARATOR .
$this->stat('base_path') . DIRECTORY_SEPARATOR .
$this->name .
'.' . $this->stat('extension');

return $this->process($path);
}
}
28 changes: 28 additions & 0 deletions code/SVGTemplateProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

/**
* Class SVGTemplateProvider
*/
class SVGTemplateProvider implements TemplateGlobalProvider
{

/**
* @return array
*/
public static function get_template_global_variables()
{
return array(
'SVG'
);
}

/**
* @param $path
* @return SVGTemplate
*/
public static function SVG($path)
{
return new SVGTemplate($path);
}

}
15 changes: 15 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "stevie-mayhew/silverstripe-svg",
"type": "silverstripe-module",
"description": "Basic SVG support for SilverStripe",
"keywords": ["silverstripe", "svg", "template"],
"authors": [
{
"name": "Stevie Mayhew",
"email": "[email protected]"
}
],
"require": {
"silverstripe-framework": "~3.1"
}
}

0 comments on commit 73669b8

Please sign in to comment.