-
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
Changes from 5 commits
ff3716e
99bc02f
739e75e
d0e2b6d
32367e8
0df1956
9a1acd9
33219b3
dbc115a
39c92e4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
class ComPagesTemplateHelperSnippet extends ComPagesTemplateHelperAbstract | ||
{ | ||
private $__snippets = array(); | ||
|
||
public function define(string $name, string $snippet, $overwrite = false) | ||
{ | ||
if(!isset($this->__snippets[$name]) || $overwrite) { | ||
$this->__snippets[$name] = $snippet; | ||
} | ||
} | ||
|
||
public function expand(string $name, array $variables = array()) | ||
{ | ||
$result = false; | ||
|
||
if(isset($this->__snippets[$name])) | ||
{ | ||
$snippet = $this->__snippets[$name]; | ||
|
||
//Use the stream buffer to evaluate the partial | ||
$str = "<?php \n return <<<SNIPPET\n$snippet\nSNIPPET;\n"; | ||
|
||
$buffer = $this->getObject('filesystem.stream.factory')->createStream('koowa-buffer://temp', 'w+b'); | ||
$buffer->truncate(0); | ||
$buffer->write($str); | ||
|
||
extract($variables, EXTR_OVERWRITE); | ||
|
||
$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 commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe 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 commentThe 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 |
||
} | ||
|
||
return $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 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