Mecha CMS

Deep learning on using the API.

Class Union

Author: Taufik Nurrohman · 132 Views

Tweet

Universal markup constructor.

Constructor 

Union($union = []);

Start by creating a new class construct:

$union = new Union();

Unite 

->unite($unit = 'html', $content = "", $data = [], $dent = 0);

Generate markup:

$test_1 = $union->unite();
$test_2 = $union->unite('div', 'test content');
$test_3 = $union->unite('a', 'a link', ['href' => '/']);
$test_4 = $union->unite('input', false, ['type' => 'text']);

Result:

$test_1 = '<html></html>';
$test_2 = '<div>test content</div>';
$test_3 = '<a href="/">a link</a>';
$test_4 = '<input type="text">';

Apart 

->apart($input, $eval = true);

Inverse version of $union->unite():

__test__(
    $union->apart('<a></a>'),
    $union->apart('<a>test content</a>'),
    $union->apart('<a href="/">test content</a>'),
    $union->apart('<input>'),
    $union->apart('<input type="text">'),
    $union->apart('<input type="text" disabled readonly="">'),
    $union->apart('<foo a="1" b=".5" c="true" d="null" e="abc" f="{
        &quot;foo&quot;: &quot;bar&quot;,
        &quot;baz&quot;: &quot;1&quot;
    }">'),
    $union->apart('<foo a="1" b=".5" c="true">', false),
    $union->apart('#')
);

Result:

[
    0 => 'a',
    1 => "",
    2 => []
]
[
    0 => 'a',
    1 => 'test content',
    2 => []
]
[
    0 => 'a',
    1 => 'test content',
    2 => [
        'href' => '/'
    ]
]
[
    0 => 'input',
    1 => false,
    2 => []
]
[
    0 => 'input',
    1 => false,
    2 => [
        'type' => 'text'
    ]
]
[
    0 => 'input',
    1 => false,
    2 => [
        'type' => 'text',
        'disabled' => 'disabled',
        'readonly' => ""
    ]
]
[
    0 => 'foo',
    1 => false,
    2 => [
        'a' => 1,
        'b' => .5,
        'c' => true,
        'd' => null,
        'e' => 'abc',
        'f' => [
            'foo' => 'bar',
            'baz' => '1'
        ]
    ]
]
[
    0 => 'foo',
    1 => false,
    2 => [
        'a' => '1',
        'b' => '.5',
        'c' => 'true'
    ]
]
false

Begin 

->begin($unit = 'html', $data = [], $dent = 0);
echo $union->unite('!DOCTYPE html', false) . N;
echo $union->begin('html', ['dir' => 'ltr']) . N;
echo $union->begin('head', [], 1) . N;
echo $union->begin('title', [], 2) . N;
echo $union->dent(3) . 'test title' . N;

Result:

<!DOCTYPE html>
<html dir="ltr">
  <head>
    <title>
      test title

End 

->end($unit = null, $dent = null);

Close the opened <title> tag:

echo $union->end('title');
echo $union->end('title', 0);

Result:

<!DOCTYPE html>
<html dir="ltr">
  <head>
    <title>
      test title
    </title>
<!DOCTYPE html>
<html dir="ltr">
  <head>
    <title>
      test title
</title>

Close all of the opened tags:

echo $union->end(true);

Result:

<!DOCTYPE html>
<html dir="ltr">
  <head>
    <title>
      test title
    </title>
  </head>
</html>

Close with broken tag:

echo $union->end('foo');

Result:

<!DOCTYPE html>
<html dir="ltr">
  <head>
    <title>
      test title
    </foo>

Comments 

->__($content, $dent = 0, $block = N);

Create markup comments:

$test_1 = $union->__('test comment');
$test_2 = $union->__('test comment', 1);
$test_3 = $union->__('test comment', 0, ' ');
$test_4 = $union->__('test comment', 1, ' ');

Result:

$test_1 = '<!--
test comment
-->';
$test_2 = '  <!--
test comment
  -->';
$test_3 = '<!-- test comment -->';
$test_4 = '  <!-- test comment -->';

Advance 

Create Custom Markup 

->union = [
    1 => [
        0 => ['<', '>', '/', '[\w:.-]+'],
        1 => ['=', '"', '"', ' ', '[\w:.-]+'],
        2 => ['<!--', '-->']
    ]
];
$union = new Union([
    1 => [
        0 => ['{{', '}}', 'end', '[\w:.-]+'],
        1 => ['=', '"', '"', ' ', '[\w:.-]+'],
        2 => ['{{!', '!}}']
    ]
]);

$test_1 = $union->unite('foo', 'content', ['bar' => 'baz']);
$test_2 = $union->unite('bar', false, ['qux' => '1']);
$test_3 = $union->__('test comment', 0, ' ');
$test_4 = $union->apart('{{foo bar}}');
$test_5 = $union->apart('<foo bar>');

Result:

$test_1 = '{{foo bar="baz"}}content{{endfoo}}';
$test_2 = '{{bar qux="1"}}';
$test_3 = '{{! test comment !}}';
$test_4 = [
    0 => 'foo',
    1 => false,
    2 => [
        'bar' => 'bar'
    ]
];
$test_5 = false;

0 Comments

No comments yet.