Comments (8)
I think this just needs a decision on whether to implement the minor change or not.
I don't know if x-
attributes are widely used elsewhere and changing the behaviour of x-
attributes would cause a namespace clash, so I'd want to see some research that before adding it. I suspect not, but I think it's worth a check.
from templ.
Glad you have a temporary workaround.
I did notice that a lot of people use
script
components when there's no obvious need to, I think the documentation may have over-emphasized them, so I've added a new section into the docs so that this is the first thing you see on https://templ.guide/syntax-and-usage/script-templates/templ body() { <script type="text/javascript"> function handleClick(event) { alert(event + ' clicked'); } </script> <button onclick="handleClick(this)">Click me</button> }
I think of script templates as a way for bundling JS along with component libraries, and a way of passing data from Go to JS, rather than them being the standard way to write JS in templ.
i see, script templates improve readability and look nicer in my opinion. and i think generated function names that start with __templ
might prevent clashes with some other global js functions?
like you mentioned, they also allow passing data from go to javascript, which i am using (although not in the example i gave above) to pass server-side state to client-side alpinejs state with the onx-
prefix. i think script templates have the potential to be useful and improve readability in templ code.
all aside, thank you for your interest on the matter. i love my current experience with templ so far :)
from templ.
i've found a temporary workaround for this feature:
it is kind of a hacky solution, but the feature above can be achieved by changing the Alpine.js prefix to onx-
add this to your head tag:
<script>
function changeAlpinePrefix() {
Alpine.prefix("onx-");
Alpine.start();
}
window.onload = changeAlpinePrefix;
</script>
now you can use:
templ Index() {
<div onx-data={alpineState()}>
<div>time: <span onx-text="time" /></div>
</div>
}
script alpineState() {
return {
"time": Date.now()
}
}
from templ.
Glad you have a temporary workaround.
I did notice that a lot of people use script
components when there's no obvious need to, I think the documentation may have over-emphasized them, so I've added a new section into the docs so that this is the first thing you see on https://templ.guide/syntax-and-usage/script-templates/
templ body() {
<script type="text/javascript">
function handleClick(event) {
alert(event + ' clicked');
}
</script>
<button onclick="handleClick(this)">Click me</button>
}
I think of script templates as a way for bundling JS along with component libraries, and a way of passing data from Go to JS, rather than them being the standard way to write JS in templ.
from templ.
Related Issues (20)
- vscode: HTML attribute conditionals do not have proper syntax highlighting HOT 3
- Feature request: Reduce verbose output to terminal HOT 2
- Generate output differs between windows and linux HOT 2
- Formatter messing up script formatting HOT 10
- Formatter break long lines incorrectly HOT 6
- docs: In gin the status passed to the HTML func is always overwritten with 200 HOT 5
- Concurrent map read and write error HOT 3
- Is a component nested inside another component possible? HOT 5
- Nix flake modules has outdated, add a test? HOT 6
- parser: error when parsing a receiver function below a templ component
- docs: Inline method components HOT 4
- Docker Image Creation on Release HOT 3
- vscode: Syntax highlighting for inline method components HOT 6
- Overriding NopComponent HOT 6
- Latest tag broke "templ generate" HOT 2
- Component on same like as text, results in compile error HOT 2
- Why SafeURL is encoded as HTML and not as URL? HOT 7
- Formatting not working as expected HOT 5
- bug(CSS component args): impossibility of nested properties and strange value `zTemplUnsafeCSSPropertyValue` HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from templ.