New issue 47 by simonkastenhuber: getGeneratorById should be public
http://code.google.com/p/piwi/issues/detail?id=47
I need Generators in my php template but can't call it because,
getGeneratorById is set to private.
I propose to change the methods signature to:
public function getGeneratorById($generatorId) {
$this->_initializeGenerator($generatorId);
return $this->generators[$generatorId];
}
Comment #1 on issue 47 by danielpalme82: getGeneratorById should be public
http://code.google.com/p/piwi/issues/detail?id=47
The method can not simply become public. To call a generator in your
template use this code:
<?php
$content = BeanFactory ::
getBeanById('generatorFactory')->callGenerator('datagrid');
$processor = new XSLTProcessor();
$processor->registerPHPFunctions();
$processor->importStyleSheet(DOMDocument ::
load($GLOBALS['PIWI_ROOT'] . "resources/xslt/GeneratorTransformation.xsl"));
$content = $processor->transformToXML($content);
echo $content ?>
This is required to execute the whole generator pipeline.
If you need this often, you could create a static helper method for this
task.
But don't make getGeneratorById($generatorId) public. It won't work in all
cases!
Why wouldn't it work in all cases?
callGenerator executes the same and just puts the content in a section tag.
but its static and should not be used on an object as your comment
suggests. We cannot make it non-static b/c its used in the XSLT templates.
A public getGeneratorById does work on a fully initialized GeneratorFactory
(created by the Bean container). it initializes the Factory if not done.
The only difference is, its not static and does not wrap the generator xml
into a section tag.
Probably this code should be doable:
echo BeanFactory ::
getBeanById('generatorFactory')->getGeneratorById('mygenerator')->generate();
What are your objections?
As you can see in my example you have to execute
the 'GeneratorTransformation.xsl' after calling 'callGenerator(...)'. This
is required to resolve LABELS and to execute custom transformations.
As long as you don't use labels, your approach will work. But it won't work
for every possible Generator, that's what i mean with 'in all cases'.
Yes, I saw this - but not in all cases somebody generates piwi tags and
transformation. Shouldn't this risk be taken by the user who calls
getGeneratorById? He should read the documentation in this case. If he
needs transformation, he could probably call "generate" on the GeneratorIf
with an parameter?
I am looking for a straightforward solution for generating stuff for the
work in templates, thats why I started with this topic again. Maybe we
should create TemplateGenerator or something; just think its important to
have this or a similar Generator concept available. Reusing standard
generators in templates would be a cool feature. Maybe a wrapper class as
you suggested at the beginning of this discussion?
The problem is, that the lifecycle is different. A generator within a
content file is transformed after execution. If you call it from a
template, the transformation is missing.
I would suggest a helper method/class to make clear what is happening.
That would avoid duplicated code within the templates and can be changed
more easily.
Yes understood the transformation problem. But we haven't defined that a
Generator is only producing piwi xml. It might be ok for a Generator to
produce plain html which might not be transformed