Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
Pages: 1
etc_script - to please PageSpeed
The scenario: you write about web and need to include a chunk of JS in your article, for illustration purposes. Your code actually depends on some external library, like jQuery. The straightforward inclusion inside your article
<script src="https://code.jquery.com/jquery-latest.min.js"></script>
<script>
// my stuff
</script>
is obviously suboptimal, since it blocks the page rendering while the scripts are loaded/executed. So you finish with something like
<script defer src="https://code.jquery.com/jquery-latest.min.js"></script>
<script>
window.addEventListener("load", () => {
// my stuff
]);
</script>
which requires some attention and would be better put at the very page end anyway. That’s where etc_script comes at help. Just write
<etc::script src="https://code.jquery.com/jquery-latest.min.js" />
<etc::script>
// my stuff
</etc::script>
and put an attributeless <etc::script /> somewhere at the end of your page template where you want all your scripts to be output. The plugin will do the necessary job, you will get a perfect score and come boast of it on the forum ;-)
The plugin is suitable for basic code only, use with caution.
# Name: etc_script v0.1
# Type: Public site only plugin
# Postpone scripts
# Author: Oleg Loukianov
# URL: http://localhost/textpattern/
# Recommended load order: 5
# .....................................................................
# This is a plugin for Textpattern CMS - https://textpattern.com/
# To install: textpattern > admin > plugins
# Paste the following text into the 'Install plugin' box:
# .....................................................................
H4sIAAAAAAAACqVUTW/bMAw9J0D/gyYEswxkSdx8tFHQFjv0VmDDkNs6BIosO9ocyZDktkGQ
/z5KlpsceuvFEB/Jx0eKMqNZRo+WzijmOhd4Zelyckvx+q2mtBSOJM9r8eZq5pww6nnNyudf
opTWmUOSfrs34SwMSYTjG8uNrF2Srq76V/2iUdxJrdDZg8gAeOwQDdxOqhLdIdVUVYqOV/2e
dcxJjgac8Z0ADzOGHUgKsVY3hl9Agb0HogzjjlTfgZG0LoB7OBeFMPju3plGDANiDQfbl/K2
p/Qq0kg00I2LSlpAFohIa6H3AWSmrbyADgJ32kn6/QfynJH7NnDlw0RlBYqUjpWkYAAMURJH
M4xNJJCQoLt75DPBHYgDECYSuE7Ik/m6X6KcMLVOUKGNgFmBqjggZtHXIKT19/z58yp6J/+J
Hb3XekByX1ewMES931CKKEqSdOTjSbzHB9SW3Py1JHmVKtevI5bnjy9CuSe/OQp2B1ea5XiI
SOpLH5ORChy9yxKBLo2e5JSuoAl/w7FoUPq+Ox+tzMU4u2s8npMurrId8jkjhrUD+EQ3l+wf
9BEKhv0zwjVGhSUCEDD/KG8ofhHGwnvy1pTiySjzJ3i37lDDu5V0AuacYm1y2H+w52AvKGaN
22njYzMI/lGJEj3p5p9kSr8EdNLFbBojPXK9pHjnXE3H40pzVu20dWN3/guMQ1pG4am184iq
Mqj2E2JrrUSclMWtqKJipe1EggzF9qIrfv4/dIif4Cb2AQjF89j0Pm9P1xRPiyKb5YvZYrmd
8G0x3ebTYilu+HIhBL/e3uLV6T9wviQJ3AQAAA==
Offline
Re: etc_script - to please PageSpeed
Clever.
Can we optionally make \Textpattern\UI\Script work this way too, somehow? Pretty please… 😎😬
The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.
Txp Builders – finely-crafted code, design and Txp
Offline
Re: etc_script - to please PageSpeed
Bloke wrote #341306:
Can we optionally make \Textpattern\UI\Script work this way too, somehow? Pretty please… 😎😬
You mean completely getting rid of its script_js() wrapper (which works already this way)? Must be doable, but script_js() will need to stay for a moment anyway, for plugins sake.
Offline
Re: etc_script - to please PageSpeed
Slightly updated v.0.2 to avoid accidental duplicates:
<etc::script src="https://code.jquery.com/jquery-latest.min.js" />
<!-- then elsewhere -->
<etc::script src="https://code.jquery.com/jquery-latest.min.js" />
...
<etc::script />
will output only one <script src="https://code.jquery.com/jquery-latest.min.js"></script> tag.
# Name: etc_script v0.2
# Type: Public site only plugin
# Postpone scripts
# Author: Oleg Loukianov
# URL: http://localhost/textpattern/
# Recommended load order: 5
# .....................................................................
# This is a plugin for Textpattern CMS - https://textpattern.com/
# To install: textpattern > admin > plugins
# Paste the following text into the 'Install plugin' box:
# .....................................................................
H4sIAAAAAAAACqVUSYvbMBQ+ZyD/QRWmtiHNNpNNIRl66G2gpeQ2GYJqy45aRzKSnJkQ8t/7
tGRpmUOhF6P3ve17mykZDMhRkweCM5kzPNdkNhwRvHqrCSmZSeL1ir2ZmhrDlFivaLn+zkqu
jTrE6aelcm+mkpiZbKMzxWsTp/P2XfuuaERmuBToqkFJBHF0B0Vmy0WJFkg0VZWiY/uupQ01
PENRRrMtAw1Vih6SFGy1bFR2A7noLSClaGaS6jNETLwK4BbOWcEUXiyNaljHIVplINtUVrYh
LYs0BIpkYwITD/ACJVxrqD0Cz9TTc2jkYgegZbXgaBTfect5gB3f510+8vALGNmHU58QqzRD
IamhZVJQADooDs3rhDJj8IjRYok8CevsfS2RD4Gfa+OZYSEVg+YBzdAxqtFH5/0X4X9MCmpX
rwPcoHyFJ/sJBVxyPSK+qyvYoERcRpYiguI47Vr7JAz2EfmUm586iV+5yOVrl+b5lz0T5smu
koBlwpWkOe6gJLWpj3FXuBit2xQuXBo08SmdQxF25CGpY3pZpvd26Kadf8zVOz2/XEbrm3z1
CGa+Af9RzW30d+pwCd1CKmYaJdzOAAiYvdIJwXumNByYle4J7neH9gWHbA41HDInfRDhlKXK
4SBAHoE8Jpg2ZiuVtR2A8deKlehJNr84FXLv0P7ZZtMobpHhjOCtMTXp9SqZ0WortemZ62+h
59wGBG7P9yOwGkC2b2BbS8FCpzT2pIqKlvpMEmgIumPn5NcfxhmxHdyEOgAheBSKhiNzryHB
9302onnxY0wnD8Mpzfuz2WSS0+l4WtBiMizw/PQbvDBWAu0EAAA=
Offline
Re: etc_script - to please PageSpeed
etc wrote #341307:
You mean completely getting rid of its
script_js()wrapper
No, I didn’t realise it worked that way. I thought as soon as you put script_js() on the page, it output its content immediately. Been.a while since I used it.
I meant if you invoked the class directly it currently always calls the ->render() method, which outputs content. Just wondered if there might be a way to signify “don’t spit it out now” so that, later, you can call render yourself and it’ll output everything that’s been buffered.
But, thinking about it, the echo behaviour is baked in because of the magic methods in the base Tag class, so it probably won’t be overridable easily.
Ignore me.
This is a cool idea for a plugin.
The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.
Txp Builders – finely-crafted code, design and Txp
Offline
Pages: 1