-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add snippets support #147
Add snippets support #147
Conversation
Helper defines two methods a define() method to create a snippet and an expand() method to render a snippet. A snipppet is expanded using heredoc syntax. Heredoc text behaves just like a double-quoted string, without the double quotes. This means that quotes in a heredoc do not need to be escaped.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@johanjanssens Left two inline comments. I also cannot test this as I get this error in framework:
array_merge(): Argument #1 is not an array in .../lib/submodules/joomlatools-framework/code/libraries/joomlatools/library/template/template.php:342
This is because $config becomes the snippet name and not the config. Is there a framework branch that I need?
$result = include $buffer->getPath(); | ||
|
||
//Cleanup whitespace | ||
$result = str_replace(array(' >', ' "'), array('>', '"'), $result); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@johanjanssens would this not cleanup the quotes in string literals too? say you have a string like click "subscribe" to proceed
in your snippet. This would turn it into click"subscribe" to proceed
right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
True, this needs a more advanced regex, haven’t gotten to that yet. Reason for this is because it generated clean html which was a request from Robin.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@johanjanssens Once you encounter this problem it's very hard to track it down and work around it so I would leave this out.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Optimised this with a simple regex. I’m now only stripping single whitespace before “ and > in html tags. See: 33219b3
|
||
$buffer = $this->getObject('filesystem.stream.factory')->createStream('koowa-buffer://temp', 'w+b'); | ||
$buffer->truncate(0); | ||
$buffer->write($str); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@johanjanssens Does it make sense or is it possible to cache these buffers instead of the snippets? Would save us some processing?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I’m happy to work on all these smaller changes, but right now I already lost more then a week on this ‘make html in helpers’ flexible. Starting to feel like micro-optimisations for problems we don’t immediately have.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fair enough. It should be quite fast compared to template rendering anyway so let's roll with it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ercanozkaya Not giving up that easily. I changed the implementation to use the php template engine. This way we have build in caching out of the box. See: 9a1acd9
@ercanozkaya This error you encountered is because we are pushing the config options to the helpers. This could actually be considered a bug, we shouldn’t be doing that when invoking a helper. I have fixed this here: 0df1956 => This change I would consider moving to the framework level together with the helper object pool. To prevent regression could move it into Kodekit only? |
Created related ticket here: joomlatools/joomlatools-framework#297 |
Template snippets
This PR adds support for snippets. A snippet behaves just like a double-quoted string, without the double quotes. This means that quotes in a snippet do not need to be escaped, but the escape codes can still be used. Variables are expanded, but the same care must be taken when expressing complex variables as with strings.
Snippets are for example useful to allow html code generated by helpers to be overridable, for a template, this can provide added flexibility to helpers.
Helper methods
The snippet helper exposes two methods
snippet.define
snippet.expand
snippet.exists
For example
1. To define the snippet
2. To expand the snippet
A snippet can also be defined and expanded by calling the helper directly. If the second argument being passed is a string it will be used to define the helper if it’s empty or an array it will be used to expend the helper.
For example
3. To check if a snippet exists
Notes
Trying to expand a snippet that hasn’t been defined yet will result in a
RuntimeException
being throw.Trying to define a snippet that has already been defined will return false. There is a third
overwrite
argument that you need to set totrue
example:<? helper('snippet’, 'button', '…', true ?>
If template caching is enabled snippets will be cached too, further improving their performance.