n:src – the missing twin brother of n:href

As you have probably guessed by now, I love Nette. The more it hurts when I see the documentation lag behind the quality of the framework. Although I don’t understand why Latte built-in macros don’t include n:src when the oh-so-useful n:href is there my main concern however, is that nowhere in the documentation you’ll find detailed information on how to add it yourself. You’re condemned to endlessly search through forum again and crawl through various outdated subdomains of nette.org.

Here’s my solution to the n:src alias based on a post in the cookbook and the built-in n:href macro. You can also take a peek an another post of mine on how to tweak your forms using macros. As always, hopefully it’ll save you some time.

This goes into your BasePresenter:

  2. public function templatePrepareFilters($template) {
  3.   $latte = new NLatteFilter;
  4.   $set = new NMacroSet($latte->parser);
  6.   $set->addMacro(‘@src’, create_function(
  7.     ‘NMacroNode $node, $writer’,
  8.     ‘extract(NCFix::$vars[‘.NCFix::uses(array(‘me’=>new NUIMacros($latte->parser))).‘], EXTR_REFS);
  9.     return \’ ?> src="<?php \’ . $me->macroLink($node, $writer) . \’ ?>"<?php \’;
  10.    ‘));
  12.   $template->registerFilter($latte);
  13. }

Yes, the code does has a high wtf factor. Just look at the n:href macro, this isn’t my invention :-) I still don’t think I fully understand it but it does the trick and does so reusing the built-in macro as much as possible.


Leave a Reply