Home / Class/ Template Class — svelte Architecture

Template Class — svelte Architecture

Architecture documentation for the Template class in template.js from the svelte codebase.

Entity Profile

Dependency Diagram

graph TD
  e1ee663e_9a6d_c084_3617_f6741c138bb7["Template"]
  c3ec4c82_ce35_6219_c232_12ebeea91443["template.js"]
  e1ee663e_9a6d_c084_3617_f6741c138bb7 -->|defined in| c3ec4c82_ce35_6219_c232_12ebeea91443
  af756561_34dc_505a_86e2_23aef4b7c333["push_element()"]
  e1ee663e_9a6d_c084_3617_f6741c138bb7 -->|method| af756561_34dc_505a_86e2_23aef4b7c333
  ea12a7f6_c99c_57b6_9bec_c01de6086aa8["push_comment()"]
  e1ee663e_9a6d_c084_3617_f6741c138bb7 -->|method| ea12a7f6_c99c_57b6_9bec_c01de6086aa8
  73d4b48c_4b16_9566_b101_b18e8a38aef4["push_text()"]
  e1ee663e_9a6d_c084_3617_f6741c138bb7 -->|method| 73d4b48c_4b16_9566_b101_b18e8a38aef4
  58d8098f_7692_f2fb_4a0f_6b486490cd6d["pop_element()"]
  e1ee663e_9a6d_c084_3617_f6741c138bb7 -->|method| 58d8098f_7692_f2fb_4a0f_6b486490cd6d
  034e8348_972b_e1ae_32c8_9476392e9799["set_prop()"]
  e1ee663e_9a6d_c084_3617_f6741c138bb7 -->|method| 034e8348_972b_e1ae_32c8_9476392e9799
  cf1f72f8_6714_5af1_7131_9890e1c7c14a["as_html()"]
  e1ee663e_9a6d_c084_3617_f6741c138bb7 -->|method| cf1f72f8_6714_5af1_7131_9890e1c7c14a
  3300d018_c33d_f853_eacf_f84c33c83c78["as_tree()"]
  e1ee663e_9a6d_c084_3617_f6741c138bb7 -->|method| 3300d018_c33d_f853_eacf_f84c33c83c78

Relationship Graph

Source Code

packages/svelte/src/compiler/phases/3-transform/client/transform-template/template.js lines 9–84

export class Template {
	/**
	 * `true` if HTML template contains a `<script>` tag. In this case we need to invoke a special
	 * template instantiation function (see `create_fragment_with_script_from_html` for more info)
	 */
	contains_script_tag = false;

	/** `true` if the HTML template needs to be instantiated with `importNode` */
	needs_import_node = false;

	/** @type {Node[]} */
	nodes = [];

	/** @type {Node[][]} */
	#stack = [this.nodes];

	/** @type {Element | undefined} */
	#element;

	#fragment = this.nodes;

	/**
	 * @param {string} name
	 * @param {number} start
	 */
	push_element(name, start) {
		this.#element = {
			type: 'element',
			name,
			attributes: {},
			children: [],
			start
		};

		this.#fragment.push(this.#element);

		this.#fragment = /** @type {Element} */ (this.#element).children;
		this.#stack.push(this.#fragment);
	}

	/** @param {string} [data] */
	push_comment(data) {
		this.#fragment.push({ type: 'comment', data });
	}

	/** @param {AST.Text[]} nodes */
	push_text(nodes) {
		this.#fragment.push({ type: 'text', nodes });
	}

	pop_element() {
		this.#stack.pop();
		this.#fragment = /** @type {Node[]} */ (this.#stack.at(-1));
	}

	/**
	 * @param {string} key
	 * @param {string | undefined} value
	 */
	set_prop(key, value) {
		/** @type {Element} */ (this.#element).attributes[key] = value;
	}

	as_html() {
		return b.template([b.quasi(this.nodes.map(stringify).join(''), true)], []);
	}

	as_tree() {
		// if the first item is a comment we need to add another comment for effect.start
		if (this.nodes[0].type === 'comment') {
			this.nodes.unshift({ type: 'comment', data: undefined });
		}

		return b.array(this.nodes.map(objectify));
	}
}

Domain

Frequently Asked Questions

What is the Template class?
Template is a class in the svelte codebase, defined in packages/svelte/src/compiler/phases/3-transform/client/transform-template/template.js.
Where is Template defined?
Template is defined in packages/svelte/src/compiler/phases/3-transform/client/transform-template/template.js at line 9.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free