Skip to content
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

Merged
merged 10 commits into from
Jul 2, 2019
Merged

Add snippets support #147

merged 10 commits into from
Jul 2, 2019

Conversation

johanjanssens
Copy link
Member

@johanjanssens johanjanssens commented Jul 1, 2019

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

<? $snippet = '
<a class="button" href="$url" $attributes>
    $icon<span>$label</span>
</a>'
?>

<? helper('snippet.define', 'button', $snippet) ?>

2. To expand the snippet

<? helper('snippet.expand’, 'button', [
'url' => 'http://exampme.com'
'icon' => /path/to/icon
'label' => 'Press here'
'attributes' => attributes([…])
]); ?>

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

<? helper('snippet’, 'button', '…' ?> //define a snippet
<? helper('snippet.expand’, 'button', […]?> //expand a snippet

3. To check if a snippet exists

<? if(helper('snippet.exists’,  'button') : ?>
….
<?endif?>

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 to true example: <? helper('snippet’, 'button', '…', true ?>

  • If template caching is enabled snippets will be cached too, further improving their performance.

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.
@johanjanssens johanjanssens added this to the 0.12.0 milestone Jul 1, 2019
@johanjanssens johanjanssens requested a review from ercanozkaya July 1, 2019 03:51
@johanjanssens johanjanssens self-assigned this Jul 1, 2019
Copy link
Member

@ercanozkaya ercanozkaya left a 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);
Copy link
Member

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?

Copy link
Member Author

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.

Copy link
Member

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.

Copy link
Member Author

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);
Copy link
Member

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?

Copy link
Member Author

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.

Copy link
Member

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.

Copy link
Member Author

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

@johanjanssens
Copy link
Member Author

@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?

@johanjanssens
Copy link
Member Author

Created related ticket here: joomlatools/joomlatools-framework#297

@johanjanssens johanjanssens merged commit 3c30e97 into master Jul 2, 2019
@johanjanssens johanjanssens deleted the feature/146-snippets branch July 2, 2019 23:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants