Block
Placeholder.
Something looks broken? We accept bounties to fix them faster.
^ | 318 Downloads |
---|---|
v1.8.0 | 231 Downloads |
v1.7.3 | 226 Downloads |
v1.7.2 | 105 Downloads |
Block is a kind of placeholder that can be embedded within the page content which normally can be written like HTML code in general, but with custom enclosing characters. Each block pattern then can be converted into something else.
By default, a block syntax can be defined as follow. It uses [[
and ]]
as the enclosing characters:
- Void blocks →
[[block]]
or[[block/]]
- Container blocks →
[[block]]content[[/block]]
Block attribute syntax is the same as the attribute syntax in HTML.
Blocks are configurable. Even, you can specify your own block markup by editing the configuration file:
[
0 => ['[[', ']]', '/'],
1 => ['"', '"', '=']
]
- The
0
parts are configuration for the block tag. - The
1
parts are configuration for the block tag’s attribute.
Following is an example of configuring block pattern as regular HTML (custom HTML tags):
<block>content</block>
[
0 => ['<', '>', '/'],
1 => ['"', '"', '=']
]
As WordPress shortcodes:
[block]content[/block]
[
0 => ['[', ']', '/'],
1 => ['"', '"', '=']
]
As Twig embed code:
{% block %}content{% endblock %}
[
0 => ['{% ', ' %}', 'end'],
1 => ['"', '"', '=']
]
As of Mecha version 1.x.x
shortcodes:
{{block}}content{{/block}}
[
0 => ['{{', '}}', '/'],
1 => ['"', '"', '=']
]
Usage
Set
Block::set(string $id, callable $fn, float $stack = 10);
Block::set(string $id, string $value, float $stack = 10);
Define a new block:
Block::set('mark', function($content) {
return '<mark>' . $content . '</mark>';
});
The code above will change this page content:
Lorem ipsum [[mark]]dolor[[/mark]] sit amet.
… to this:
Lorem ipsum <mark>dolor</mark> sit amet.
Get
Block::get(string $id = null);
Return the mark
block data if any, otherwise, return null
or empty array:
if ($data = Block::get('mark')) {
// Do something with `$data`…
return call_user_func($data['fn'], $content);
}
Let
Block::let(string $id = null);
Disable the mark
block pattern:
Block::let('mark');
Alter
Block::alter(string $id, callable $fn, string $content);
Block::alter(string $id, string $value, string $content);
Replace mark
block in $content
with advance pattern:
$content = Block::alter('mark', function($a, $b) {
$c = "";
if (isset($b['color'])) {
$c = ' style="background: ' . $b['color'] . ';"';
}
return '<mark' . $c . '>' . $a . '</mark>';
}, $content);
The code above will change this $content
value:
Lorem [[mark]]ipsum[[/mark]] [[mark color="#f00"]]dolor[[/mark]] sit amet.
… to this:
Lorem <mark>ipsum</mark> <mark style="background: #f00;">dolor</mark> sit amet.
Using Files
Block name can be defined as plain text file with extension data
stored in .\lot\block
folder. Each file name acts as the block name, while the file content acts as a substitution value for the block.
.\
└── lot\
└── block\
├── mark.data
└── …
The following is an example of the mark.data
file content:
<mark style="background: %color;">%1</mark>
The %1
pattern is a parameter that will be converted into any text that is inside the container block markup. Some other parameters include:
%0
→ Return the block name.%1
→ Return the block content.%2
→ Return the block attributes as literal JSON.%color
→ Return the block’scolor
attribute value.