Skip to content

Commit 7ec0ca5

Browse files
committed
Merge pull request #146 from paulvl/5.2
Function Tag
2 parents c88b2d5 + 6f35784 commit 7ec0ca5

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

src/HtmlBuilder.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,21 @@ public function meta($name, $content, array $attributes = [])
486486
return $this->toHtmlString('<meta' . $this->attributes($attributes) . '>' . PHP_EOL);
487487
}
488488

489+
/**
490+
* Generate an html tag.
491+
*
492+
* @param string $tag
493+
* @param mixed $content
494+
* @param array $attributes
495+
*
496+
* @return \Illuminate\Support\HtmlString
497+
*/
498+
public function tag($tag, $content, array $attributes = [])
499+
{
500+
$content = is_array($content) ? implode(PHP_EOL, $content) : $content;
501+
return $this->toHtmlString('<' . $tag . $this->attributes($attributes) . '>' . PHP_EOL . $this->toHtmlString($content) . PHP_EOL . '</' . $tag . '>' . PHP_EOL);
502+
}
503+
489504
/**
490505
* Transform the string to an Html serializable object
491506
*

tests/HtmlBuilderTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,27 @@ public function testMeta()
4646
$this->assertEquals('<meta name="description" content="Lorem ipsum dolor sit amet.">' . PHP_EOL, $result);
4747
}
4848

49+
public function testTag()
50+
{
51+
$result1 = $this->htmlBuilder->tag('p', 'Lorem ipsum dolor sit amet.');
52+
53+
$result2 = $this->htmlBuilder->tag('p', 'Lorem ipsum dolor sit amet.', ['class' => 'text-center']);
54+
55+
$result3 = $this->htmlBuilder->tag('div', '<p>Lorem ipsum dolor sit amet.</p>', ['class' => 'row']);
56+
57+
$content = [
58+
$this->htmlBuilder->image('http://example.com/image1'),
59+
$this->htmlBuilder->image('http://example.com/image2'),
60+
];
61+
62+
$result4 = $this->htmlBuilder->tag('div', $content, ['class' => 'row']);
63+
64+
$this->assertEquals('<p>' . PHP_EOL . 'Lorem ipsum dolor sit amet.' . PHP_EOL . '</p>' . PHP_EOL, $result1);
65+
$this->assertEquals('<p class="text-center">' . PHP_EOL . 'Lorem ipsum dolor sit amet.' . PHP_EOL . '</p>' . PHP_EOL, $result2);
66+
$this->assertEquals('<div class="row">' . PHP_EOL . '<p>Lorem ipsum dolor sit amet.</p>' . PHP_EOL . '</div>' . PHP_EOL, $result3);
67+
$this->assertEquals('<div class="row">' . PHP_EOL . '<img src="http://example.com/image1">' . PHP_EOL . '<img src="http://example.com/image2">' . PHP_EOL . '</div>' . PHP_EOL, $result4);
68+
}
69+
4970
public function testMetaOpenGraph()
5071
{
5172
$result = $this->htmlBuilder->meta(null, 'website', ['property' => 'og:type']);

0 commit comments

Comments
 (0)