lightncandy icon indicating copy to clipboard operation
lightncandy copied to clipboard

link helper, the code.

Open ymeiner opened this issue 9 years ago • 4 comments

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>

ymeiner avatar May 03 '16 19:05 ymeiner

Thank you for your contribution! It's will be better if you provide some example of template with link_helper. :)

zordius avatar May 04 '16 01:05 zordius

sorry, I rewrote the text above with all data. :)

ymeiner avatar May 04 '16 20:05 ymeiner

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?

zordius avatar May 05 '16 01:05 zordius

correct, added one more.

ymeiner avatar May 07 '16 14:05 ymeiner