MakeHTML
This is a class and trait for Laravel that will convert text with line breaks and links into HTML. It also adds simple HTML tag generation as well as a helper function for imploding associative arrays., (*1)
Install
Via Composer, (*2)
``` bash
$ composer require taylornetwork/make-html, (*3)
### Dependencies
`TaylorNetwork\MakeHTML` will install [TaylorNetwork\LaravelHelpers][link-helpers]. Laravel should auto discover that package, but if you run into problems you may need to include the provider manually.
## Setup
This package is set up with auto discovery so you should be good to go out of the box. Check Manual Setup below if something isn't working.
### Manual Setup
If auto discovery doesn't work...
Add the service provider to the providers array in `config/app.php`.
``` php
'providers' => [
TaylorNetwork\MakeHTML\MakeHTMLServiceProvider::class,
];
Publishing
Run, (*4)
``` php
php artisan vendor:publish, (*5)
This will add `makehtml.php` to your config directory.
## Usage
You can use this package either by including the trait in a class you want to implement the functionality or by creating a new instance of the class.
### Trait
Import the trait into your class.
``` php
use TaylorNetwork\MakeHTML\MakeHTML;
class DummyClass
{
use MakeHTML;
// Code
}
The MakeHTML trait includes two functions, usage examples below., (*6)
makeHTML (string $text)
Accepts one parameter, the text you want the generator to parse., (*7)
Given this example variable $text
, (*8)
``` php
$text = 'Example text
with line
breaks. And a
link: http://example.com/page/1/2?q=This&that=other';, (*9)
Calling makeHTML
``` php
$this->makeHTML($text);
Returns, (*10)
``` php
'Example text
with line
breaks. And a
link: example.com', (*11)
#### getHTMLGeneratorInstance ()
Accepts no parameters and returns an instance of HTMLGenerator class, or creates one.
``` php
$this->getHTMLGeneratorInstance();
You can chain any methods from the HTMLGenerator class onto that to return functionality., (*12)
See class usage below for examples., (*13)
Class
Instantiate the class., (*14)
``` php
use TaylorNetwork\MakeHTML\HTMLGenerator;, (*15)
$instance = new HTMLGenerator();, (*16)
#### Available Methods
##### makeLinks (string $text)
Converts any found links in the string to clickable links with `<a>` tag.
Will take the base URL as the caption for the link.
For example:
``` php
$textWithLink = 'I have a link http://example.com/page/1/2/3?query=string';
Call makeLinks($textWithLink)
, (*17)
``` php
$instance->makeLinks($textWithLink);, (*18)
Returns
``` php
'I have a link <a href="http://example.com/page/1/2/3?query=string">example.com</a>'
convertLineEndings (string $text)
Converts all line endings to HTML <br>
tags., (*19)
``` php
$textWithLineBreaks = 'This
is
text
with
line
breaks.';, (*20)
Call `convertLineEndings($textWithLineBreaks)`
``` php
$instance->convertLineEndings($textWithLineBreaks);
Returns, (*21)
``` php
'This
is
text
with
line
breaks.', (*22)
##### makeHTML (string $text)
Calls `convertLineEndings($text)` and `makeLinks($text)` and returns the converted text.
##### generateTag (string $tag, array $attributes, boolean $closeTag = true)
Will generate an HTML tag with any attributes given.
To generate a `<div>` tag with no attributes
``` php
$instance->generateTag('div', []);
Returns, (*23)
``` php
', (*24)
'
---
To generate a `<div>` tag with attributes
``` php
$attributes = [ 'class' => 'example-class second-class third' 'data-attr' => 'value' ];
Call function, (*25)
``` php
$instance->generateTag('div', $attributes);, (*26)
Returns
``` php
'<div class="example-class second-class third" data-attr="value"></div>'
To generate a <div>
tag with attributes and also data between the tags, add the external
attribute and everything there will be added between the tags., (*27)
``` php
$attributes = [
'class' => 'example-class',
'id' => 'div1',
'name' => 'some-name',
'external' => 'This is external data!',
];, (*28)
Call function
``` php
$instance->generateTag('div', $attributes);
Returns, (*29)
``` php
', (*30)
This is external data!
'
---
To generate a `<div>` tag and keep it open, to process more external data for example.
``` php
$instance->generateTag('div', $attributes, false);
Returns, (*31)
``` php
', (*32)
This is external data!'
#### closeTag(string $tag)
Closes a given tag
``` php
$instance->closeTag('div');
Returns, (*33)
``` php
', (*34)
'
#### Magic Method
You can also call `generateTag($tag, $attributes)` by calling the tag you want followed by `Tag`
To generate a `<div>` tag this way
``` php
$instance->divTag($attributes);
Would call generateTag('div', $attributes)
for you., (*35)
Config
The config file once published is in config/makehtml.php
, (*36)
Credits
License
The MIT License (MIT). Please see License File for more information., (*37)