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.
Via Composer
$ composer require taylornetwork/make-html
TaylorNetwork\MakeHTML
will install TaylorNetwork\LaravelHelpers. Laravel should auto discover that package, but if you run into problems you may need to include the provider manually.
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.
If auto discovery doesn't work...
Add the service provider to the providers array in config/app.php
.
'providers' => [
TaylorNetwork\MakeHTML\MakeHTMLServiceProvider::class,
];
Run
php artisan vendor:publish
This will add makehtml.php
to your config directory.
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.
Import the trait into your class.
use TaylorNetwork\MakeHTML\MakeHTML;
class DummyClass
{
use MakeHTML;
// Code
}
The MakeHTML trait includes two functions, usage examples below.
Accepts one parameter, the text you want the generator to parse.
Given this example variable $text
$text = 'Example text
with line
breaks. And a
link: http://example.com/page/1/2?q=This&that=other';
Calling makeHTML
$this->makeHTML($text);
Returns
'Example text<br>with line<br>breaks. And a<br>link: <a href="http://example.com/page/1/2?q=This&that=other">example.com</a>'
Accepts no parameters and returns an instance of HTMLGenerator class, or creates one.
$this->getHTMLGeneratorInstance();
You can chain any methods from the HTMLGenerator class onto that to return functionality.
See class usage below for examples.
Instantiate the class.
use TaylorNetwork\MakeHTML\HTMLGenerator;
$instance = new HTMLGenerator();
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:
$textWithLink = 'I have a link http://example.com/page/1/2/3?query=string';
Call makeLinks($textWithLink)
$instance->makeLinks($textWithLink);
Returns
'I have a link <a href="http://example.com/page/1/2/3?query=string">example.com</a>'
Converts all line endings to HTML <br>
tags.
$textWithLineBreaks = 'This
is
text
with
line
breaks.';
Call convertLineEndings($textWithLineBreaks)
$instance->convertLineEndings($textWithLineBreaks);
Returns
'This<br>is<br>text<br>with<br>line<br>breaks.'
Calls convertLineEndings($text)
and makeLinks($text)
and returns the converted text.
Will generate an HTML tag with any attributes given.
To generate a <div>
tag with no attributes
$instance->generateTag('div', []);
Returns
'<div></div>'
To generate a <div>
tag with attributes
$attributes = [ 'class' => 'example-class second-class third' 'data-attr' => 'value' ];
Call function
$instance->generateTag('div', $attributes);
Returns
'<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.
$attributes = [
'class' => 'example-class',
'id' => 'div1',
'name' => 'some-name',
'external' => 'This is external data!',
];
Call function
$instance->generateTag('div', $attributes);
Returns
'<div class="example-class" id="div1" name="some-name">This is external data!</div>'
To generate a <div>
tag and keep it open, to process more external data for example.
$instance->generateTag('div', $attributes, false);
Returns
'<div class="example-class" id="div1" name="some-name">This is external data!'
Closes a given tag
$instance->closeTag('div');
Returns
'</div>'
You can also call generateTag($tag, $attributes)
by calling the tag you want followed by Tag
To generate a <div>
tag this way
$instance->divTag($attributes);
Would call generateTag('div', $attributes)
for you.
The config file once published is in config/makehtml.php
- Main Author: Sam Taylor
The MIT License (MIT). Please see License File for more information.