Hey there. Thank you very much for this great library.
It's fun to use and pretty flexible. However, I'm struggling to convert one aspect of my JS implementation: Compiling a new template from within a helper function.
For example, I am using a custom "partial" helper instead of using partials directly. This not only allows passing additional arguments into the partial (in case you need to overwrite some variables in the scope of the partial), it also allows responsive use of different templates.
I might call {{partial 'article'}}, but the helper will also look for 'article-phone' or 'article-tablet' depending on the screen size.
This works beautifully with handlebars.js, but does not seem to be possible with this library right now
LightnCandy::compile($template,$options);
The options array is applied every time you compile a template and there is no way around that. Since helpers are defined within the options array, I'd need access to the array while declaring it.
I hacked around this brain-melting detail by adding a new static method that applies the options array once, and removed the code from the compile() method This seemed to work at first. For reference, this is the helper I am using:
$helpers = array(
'partial' => function ( $name, $context ) {
$template = file_get_contents( TPL_DIR . '/' . $name . '.html' );
$php = LightnCandy::compile( $template );
$renderer = LightnCandy::prepare( $php );
echo $renderer( $context[ 'data' ][ 'root' ] );
});
But then I noticed that the generated HTML was not "nested" as it should have been.
Consider the following structure:
/* main template */
<div id="main">
{{partial 'body'}}
</div>
/* body partial */
<div id="body">
{{partial 'article'}}
This is the body
</div>
/* article partial */
<div id="article">
I am the article
</div>
In my tests, this resulted in the following HTML:
<div id="main">
<div id="article">
I am the article
</div>
<div id="body">
This is the body
</div>
</div>
While it should have been
<div id="main">
<div id="body">
<div id="article">
I am the article
</div>
This is the body
</div>
</div>
Is there a more sane approach to achieve what I am trying to do. Is it possible at all?
Since I am heavily relying on a custom partial helper for the responsive functionality of this project, this is an absolute must have for me.
Thank you for your time (and sorry for the wall of text)