Skip to content

Templ Component inside another component? Is it possible? #554

Answered by Blankeos
Blankeos asked this question in Q&A
Discussion options

You must be logged in to vote

Wait! I didn't read the docs enough. Lol, it was just this simple. Found it here: https://templ.guide/syntax-and-usage/template-composition :

package web

templ CountResult(newCount string) {
	<input id="" name="count" value={ newCount }/>
	<span>New Count: { newCount }</span>
	<button type="submit">
		Increment
	</button>
}

templ HelloForm(count string) {
	@Base() {
		<form hx-post="/hello" method="POST" hx-target="#hello-container">
			<input id="name" name="name" type="text"/>
			<button type="submit">Submit</button>
		</form>
		<div id="hello-container"></div>
		<form hx-post="/increment" method="POST">
			@CountResult(count)
		</form>
	}
}

templ HelloPost(name string) {
	<div>Hello,…

Replies: 2 comments 6 replies

Comment options

You must be logged in to vote
1 reply
@a-h
Comment options

Answer selected by a-h
Comment options

You must be logged in to vote
5 replies
@a-h
Comment options

@gabrielvincent
Comment options

@trileminh94
Comment options

@a-h
Comment options

@gabrielvincent
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
4 participants