Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
gorriecoe committed Mar 2, 2018
0 parents commit 8cf69e5
Show file tree
Hide file tree
Showing 10 changed files with 419 additions and 0 deletions.
17 changes: 17 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# For more information about the properties used in this file,
# please see the EditorConfig documentation:
# http://editorconfig.org

[*]
charset = utf-8
end_of_line = lf
indent_size = 4
indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true

[{*.yml,*.json}]
indent_size = 2

[{*.ss}]
indent_style = tab
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Auto detect text files and perform LF normalization
* text=auto
14 changes: 14 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Thumbs.db
web.config
.log
.buildpath
.project
.settings
.idea
.DS_Store
._.DS_Store
.codekit
.sublime-*
.sass-cache
prepros.cfg
node_modules
29 changes: 29 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
BSD 3-Clause License

Copyright (c) 2017, gorriecoe
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

* Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 changes: 36 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Silverstripe html tag


## Installation
Composer is the recommended way of installing SilverStripe modules.
```
composer require gorriecoe/silverstripe-embed
```

## Requirements

- silverstripe/framework ^4.0

## Maintainers

- [Gorrie Coe](https://github.com/gorriecoe)

# Template usage

```
{$h1($Title).setClass('title').setPrefix($Class)}
```
Is the equivalent of
```
<% if Title %>
<h1 class="{$class}__title title">
{$Title}
</h1>
<% end_if %>
```
And returns
```html
<h1 class="title content-section__title">
This sections title
</h1>
```
3 changes: 3 additions & 0 deletions _config/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
Name: htmltagconfig
---
25 changes: 25 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "gorriecoe/silverstripe-htmltag",
"description": "Provides a simply method for wrapping strings with html tags.",
"type": "silverstripe-vendormodule",
"license": "BSD-3-Clause",
"authors": [
{
"name": "Gorrie Coe",
"email": "[email protected]"
}
],
"keywords": [
"silverstripe",
"html",
"templates",
"markup"
],
"homepage": "http://github.com/gorriecoe/silverstripe-htmltag",
"require": {
"silverstripe/framework": "^4"
},
"extra": {
"installer-name": "htmltag"
}
}
12 changes: 12 additions & 0 deletions contributing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Contributing
- Maintenance on this module is a shared effort of those who use it
- To contribute improvements to the code, ensure you raise a pull request and discuss with the module maintainers
- Please follow the SilverStripe [code contribution guidelines](https://docs.silverstripe.org/en/contributing/code/) and [Module Standard](https://docs.silverstripe.org/en/developer_guides/extending/modules/#module-standard)
- Supply documentation that followS the [GitHub Flavored Markdown](https://help.github.com/articles/markdown-basics/) conventions
- When having discussions about this module in issues or pull request please adhere to the [SilverStripe Community Code of Conduct](https://docs.silverstripe.org/en/contributing/code_of_conduct/)
## Contributor license agreement
By supplying code to this module in patches, tickets and pull requests, you agree to assign copyright
of that code to gorriecoe, on the condition that these code changes are released under the
same BSD license as the original module. We ask for this so that the ownership in the license is clear
and unambiguous. By releasing this code under a permissive license such as BSD, this copyright assignment
won't prevent you from using the code in any way you see fit.
155 changes: 155 additions & 0 deletions src/view/HTMLTag.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
<?php

namespace gorriecoe\HTMLTag\View;

use SilverStripe\View\ViewableData;

/**
* HTMLTag
*
* @package silverstripe
* @subpackage silverstripe-htmltag
*/
class HTMLTag extends ViewableData
{
protected $string = null;

protected $tag = null;

protected $attributes = [];

protected $classes = [];

protected $classPrefix = null;

public function __construct($string = null, $tag = null)
{
$this->string = $string;
$this->tag = $tag;

parent::__construct($string);
}

/**
* Defines a HTML tag type
* @param string $value HTML tag type
* @return HTMLTag $this
*/
public function setTag($value)
{
$this->tag = $value;
return $this;
}

/**
* Defines an HTML attribute to add
* @param [type] $attribute Attribute
* @param [type] $value Attribute value
* @return HTMLTag $this
*/
public function setAttribute($attribute = null, $value)
{
if ($value) {
$this->attributes[$attribute] = $value;
} else {
unset($this->attributes[$attribute]);
}
return $this;
}

/**
* Defines a CSS classes
* @param string $value CSS class
* @return HTMLTag $this
*/
public function setID($value)
{
$this->setAttribute('id', $value);
return $this;
}

/**
* Defines CSS classes
* @param string $value CSS class
* @return HTMLTag $this
*/
public function setClass($value)
{
$classes = [];
foreach (explode(' ', $value) as $class) {
$classes[$class] = $class;
}
$this->classes = $classes;
return $this;
}

/**
* Defines a CSS classes to add
* @param string $value CSS class
* @return HTMLTag $this
*/
public function addClass($value)
{
foreach (explode(' ', $value) as $class) {
$this->classes[$class] = $class;
}
return $this;
}

/**
* Defines a CSS classes to remove
* @param string $value CSS class
* @return HTMLTag $this
*/
public function removeClass($value)
{
foreach (explode(' ', $value) as $class) {
unset($this->classes[$class]);
}
return $this;
}

/**
* Defines a CSS class prefix
* @param string $value CSS class
* @return HTMLTag $this
*/
public function setPrefix($value)
{
$this->classPrefix = $value;
return $this;
}

public function forTemplate()
{
$string = $this->string;
if (!isset($string) || !$string || $string == '') {
return null;
}
$tag = $this->tag;
$attributes = [];
foreach ($this->attributes as $key => $value) {
$attributes[] = $key . "='$value'";
}

$classes = $this->classes;
if (Count($classes)) {
if ($classPrefix = $this->classPrefix) {
foreach ($classes as $key => $value) {
$prefixedClass = $classPrefix . '__' . $value;
$classes[$prefixedClass] = $prefixedClass;
}
}
$classes = implode(' ', $classes);
$attributes[] = "class='$classes'";
}

$attributes = Count($attributes) ? ' ' . implode(' ', $attributes) : '';
return "<$tag$attributes>$string</$tag>";
}

public function Raw()
{
return $this->string;
}
}
Loading

0 comments on commit 8cf69e5

Please sign in to comment.