link helper, the code.
The PHP Code:
require_once 'vendor/autoload.php';
use LightnCandy\LightnCandy;
$template = '{{link "foo" foo="bar"}}';
$options = array(
"flags" => LightnCandy::FLAG_NOESCAPE | LightnCandy::FLAG_NAMEDARG,
"helpers" => array(
"link" => "link_helper",
)
);
$phpStr = LightnCandy::compile($template, $options);
$renderer = LightnCandy::prepare($phpStr);
echo $renderer(null);
function link_helper ($name,$options){
$links = get_predefined_links(); ; // not the right way to do it, its a list of pre-defined links.
$attrs = array();
//add data from lin
if (isset($links[$name])){
$attrs['href'] = isset($links[$name]['url'])?$links[$name]['url']:'';
$text = isset($links[$name]['text'])?$links[$name]['text']:'';
}
else {
$text = $name;
}
if (isset($options['hash'])){
foreach ($options['hash'] as $index => $value){
$attrs[$index] = $value;
}
}
$attrs_str = '';
foreach ($attrs as $index => $value){
$attrs_str .= $index . '="' . $value . '" ';
}
return "<a ".$attrs_str.">".$text."</a>";
}
function get_predefined_links(){
return array(
'foo' => array(
'url' => '/foo',
'text' => 'Foo'
),
'bar' => array(
'url' => '/bar',
'text' => 'Bar'
)
);
}
output: <a href="/foo" foo="bar">Foo</a>
The Issue:
The above code is for a link helper.
examples of usage:
{{link "foo"}} will output <a href="/foo">Foo</a>
{{link "bar"}} will output <a href="/bar">Bar</a>
{{link "foo" class="bold"}} will output <a href="/foo" class="bold">Foo</a>
{{link "foo" href="/some/path" class="bar" title="foobar"}} will output <a href="/some/path" class="bar" title="foobar">foo</a>
Thank you for your contribution! It's will be better if you provide some example of template with link_helper. :)
sorry, I rewrote the text above with all data. :)
Maybe more examples:
{{link "foo"}} will output <a href="/foo">Foo</a>
{{link "bar"}} will output <a href="/bar">Bar</a>
{{link "foo" class="bold"}} will output <a href="/foo" class="bold">Foo</a>
Are these right?
correct, added one more.