custom helper not doing its job
- richard
- Member | 60
Hello,
I need to run strings with custom macros through macro filter, that works,
but helpers do not.
It all happens in a separate class which is extension of Macroset:
<?php
public static function parseString($string)
{
$latte = new Nette\Latte\Engine;
$latte->parser = new Nette\Latte\Parser;
$template = new Nette\Templating\Template;
$template->registerHelper('big', 'strtoupper');
CustomMacros::install($latte->parser);
$template->setSource($string);
$template->registerFilter($latte);
return = (string)$template;
}
?>
If I set non-existent function, it takes it without error.
Do I need to register Filters only in presenter?
- richard
- Member | 60
Thanks HosipLan,
with a reference to your previous example:https://forum.nette.org/…e-a-template#… it works only on default macros:
<?php
class CustomMacros extends Nette\Latte\Macros\MacroSet
{
public static function parseString($string)
{
$latte = new Nette\Latte\Engine;
//$latte->parser = new Nette\Latte\Parser;
$template = new Nette\Templating\Template;
$template->registerHelper('big', callback('strtoupper'));
CustomMacros::install($latte->parser);
$template->setSource($string);
$template->registerFilter($latte);
return = (string)$template;
}
// -------------
$string = CustomMacros::parseString('{var $check="var"} {$check|big} {box|big}box{/box}');
echo $string;
// -------------
?>
This prints “VAR <div>box</div>” instead of expected “VAR <div>BOX</div>”
- Patrik Votoček
- Member | 2221
I don't think what {box} macro is but you may add space
{box |big}box{/box}
to your code.
- richard
- Member | 60
Hello,
I have installed {box}{/box} as per previous advice from HosipLan. It prints the div around the string, it just does not apply macro on it.
<?php
$string = CustomMacros::parseString('{var $check="var"} {$check|nonExistentMacro} {box |big}box{/box}');
?>
the above screams: Call to undefined method:
Nette\Templating\Template::nonExistentMacro()
which is OK.
<?php
$string = CustomMacros::parseString('{var $check="var"} {$check|big} {box |nonExistentMacro}box{/box}');
?>
This one prints “VAR <div>box</div>”, no error.
If I add default handlers:
<?php
public static function parseString($string)
{
$latte = new Nette\Latte\Engine;
//$latte->parser = new Nette\Latte\Parser;
$template = new Nette\Templating\Template;
$template->registerHelper('big', callback('strtoupper'));
$template->registerHelperLoader('Nette\Templating\DefaultHelpers::loader'); // NOTE THIS
CustomMacros::install($latte->parser);
$template->setSource($string);
$template->registerFilter($latte);
return = (string)$template;
}
// -----------
$string = CustomMacros::parseString('{var $check="var"} {$check|upper} {box |upper}box{/box}');
?>
It applies default helpers only on default macros: “VAR
<div>box</div>”.
It never applies helper on custom macro even it does apply macro (the div
here).
What's wrong with me?
Last edited by richard (2011-10-17 08:30)
- Patrik Votoček
- Member | 2221
so i think use something like this
{var $check="var"} {block |upper}{$check} {box}box{/box}{/block}
- richard
- Member | 60
Ok, thanks. This was quite simplified example and the solution does not solve my aim. So to be a bit more specific, this is more like it:
Blah blah {user pepa|email} blah.
The macro should get class::user($attribute,$name = false) or class class::$user[$attribute] or something similar – so that helper is always trying to get a key of an array… anything.
I have touched this topic here already, I just hope I will be able to do
it with Nette parsers.
My problem is, that anytime I try to apply helper on custom macro in template
outside a presenter and then cast to a string it does not work or error…
Last edited by richard (2011-10-17 18:11)
- HosipLan
- Moderator | 4668
Helper is not a function (in context of template). Helper can wrap a paired
macro {box |helper}lala{/box}
, variable
{$user |helper}
, but not single-standing macro
{user pepa}
.
You're just trying to program in templates. That is not the right thing to do. Do your programming outside of the template.
{$user->email}
Last edited by HosipLan (2011-10-17 18:59)